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?
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!