Summation with FOR Loop

80 ビュー (過去 30 日間)
Me29
Me29 2017 年 4 月 17 日
コメント済み: Cameron Cooke 2021 年 10 月 19 日
Hello, Super noob question, but how do I do the attached picture in MatLab using a FOR loop. I just need to know how to set up the loop, not the actual values. Thanks for any help!

採用された回答

Torsten
Torsten 2017 年 4 月 18 日
omega=...;
t=...;
N=...;
summe = 0.0;
for k=1:N
summe = summe + sin(omega*t*(2*k-1))/(2*k-1);
end
Best wishes
Torsten.
  2 件のコメント
Me29
Me29 2017 年 4 月 18 日
I don't think that is correct.. Given t=0:20, N=1000, omega=1, it should look like a square wave. The function basically sums a series of sine functions and approximates a square wave. Using your code, it doesn't do that... Any other ideas? Thanks!
Torsten
Torsten 2017 年 4 月 19 日
Try
t=0:2/pi/100:2/pi
omega=1;
N=1000;
summe = 0.0;
for k=1:N
summe = summe + sin(omega*t*(2*k-1))/(2*k-1);
end
plot(t,4/pi*summe)
Best wishes
Torsten.

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

その他の回答 (3 件)

Jan
Jan 2017 年 4 月 17 日
You cannot run a loop from 1 to infinity in Matlab. Either solve the summation symbolically or find out, if this sum converges and you can use a certain number of elements to get the result with a wanted accuracy.
  1 件のコメント
Me29
Me29 2017 年 4 月 17 日
Sorry failed to mention, assume the limit of the summation is a real value input from the user. I just need the setup for the FOR loop.

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


Arzu Ahmadova
Arzu Ahmadova 2019 年 5 月 22 日
Hi,
Could you explain please how to code more than 2 different inifinite sums in loop?
  1 件のコメント
Cameron Cooke
Cameron Cooke 2021 年 10 月 19 日
no

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


daniel domshlak
daniel domshlak 2020 年 3 月 29 日
i=0;
x = input ('choose the initial value : ');
y = input ('choose final value : ');
n=(x:y);
summe=0;
for i=x:y
a=(i*2+1).^3;
summe = summe + a;
end
summe
  3 件のコメント
daniel domshlak
daniel domshlak 2020 年 3 月 29 日
if someone finds a way to shorten this, don't hassitate to sahare.
Arzu Ahmadova
Arzu Ahmadova 2020 年 3 月 29 日
Thanks

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

カテゴリ

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