How do I save each value for this loop?

6 次查看(过去 30 天)
N = 5;
A = 0;
w = 1; %where omega is equals to 2pi/tau and tau is equals to 2pi
a_0 = pi/4;
for i = 1:1:N
a_n = 2*(-1)^N-1 / pi*N^2;
A(i) = A + a_n;
end
I'm trying to make a fourier series code and this is part of it. I'm trying to save each value I get from that for loop and use it later for the calculation and plotting. My problem is that whenever I run this, I get an error message saying
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in test (line 9)
A(i) = A + a_n;
So as far as I understand, The first loop is going to be okay because A(1) will be a value of A+a_n, which is a sum of scalar, but from second loop, variable A becomes vector but a_n is still scalar so it's causing this error.
How do I solve this error and make it run?

采纳的回答

Star Strider
Star Strider 2018-9-23
I am not certain what you want.
Try this:
N = 5;
A = 0;
w = 1; %where omega is equals to 2pi/tau and tau is equals to 2pi
a_0 = pi/4;
for i = 1:1:N
a_n = 2*(-1)^N-1 / pi*N^2;
A(i+1) = A(i) + a_n;
end
  6 个评论
Soungeon Park
Soungeon Park 2018-9-24
Thanks for the help! It seems to be working :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by