How do I save each value for this loop?

1 回表示 (過去 30 日間)
Soungeon Park
Soungeon Park 2018 年 9 月 23 日
コメント済み: Star Strider 2018 年 9 月 24 日
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 :)
Star Strider
Star Strider 2018 年 9 月 24 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by