I am beginner in matlab, and i struggly to resolve some exercice. can I have some advice according to this task?
1-1/3+1/5-1/7+1/9-..-1/1003

 採用された回答

David Hill
David Hill 2022 年 10 月 29 日

1 投票

n=1:502;
s=sum((-1).^(n+1)./(2*n-1))
s = 0.7849

7 件のコメント

Alonso Mukendi Kalonji
Alonso Mukendi Kalonji 2022 年 10 月 29 日
how did you get 502 for the loop
VBBV
VBBV 2022 年 10 月 29 日
The highest term in the fraction of series is 1003. So it can obtained by this way 2*502-1
David Hill
David Hill 2022 年 10 月 29 日
2*502 -1 = 1003 (last element). 2*n-1 is an expression for odd numbers.
Alonso Mukendi Kalonji
Alonso Mukendi Kalonji 2022 年 10 月 29 日
This cod are not working
>> a = 1;
k = 502;
for x = 1:k
s = sum(((-1).^(x+1))./(2*(x-1)));
end
disp(s)
-9.9800e-04
David Hill
David Hill 2022 年 10 月 29 日
No need for a loop. Just run the below code.
x=1:502;
s=sum((-1).^(x+1)./(2*x-1))
s = 0.7849
Torsten
Torsten 2022 年 10 月 29 日
Maybe of interest:
syms n
s = simplify(symsum((-1)^n/(2*n+1),0,Inf))
s = 
Alonso Mukendi Kalonji
Alonso Mukendi Kalonji 2022 年 10 月 30 日
I get it!
Thank you very much!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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