How do I save each value for this loop?

1 view (last 30 days)
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?

Accepted Answer

Star Strider
Star Strider on 23 Sep 2018
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 Comments
Soungeon Park
Soungeon Park on 24 Sep 2018
Thanks for the help! It seems to be working :)

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!