how to write a Matlab code to sum 10 terms of rational numbers?

6 ビュー (過去 30 日間)
Omar B.
Omar B. 2021 年 7 月 23 日
コメント済み: Omar B. 2021 年 7 月 23 日
How to write a matlab code to find the sum of 10 terms of the follwing series:
(39/2)-(36/5)+(31/10)-(24/17)+...
  2 件のコメント
James Tursa
James Tursa 2021 年 7 月 23 日
What is the formula for the terms? Do you want a double precision answer, or an exact fraction (i.e. symbolic) answer?
Omar B.
Omar B. 2021 年 7 月 23 日
I could not find the formula. I want a double precision answer.

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

採用された回答

Debarati Bhattacharyya
Debarati Bhattacharyya 2021 年 7 月 23 日
Hi Omar,
Please find the function below which calculates the sum of the series as you are looking for:
function calculateSum(num_of_terms)
% To calculate the sum of the below series upto n terms
% 'n' is provided by the user
% (39/2)-(36/5)+(31/10)-(24/17)+...
sum = 0;
first_num = 39;
first_denom = 2;
for k = 1:num_of_terms
disp([num2str(first_num), '/', num2str(first_denom)]);
sum = sum + (-1)^(k+1)*((first_num)/(first_denom));
first_num = (first_num-(2*k+1));
first_denom = (first_denom+(2*k+1));
end
disp(sum);
end
Please let me know if this works for you!
  1 件のコメント
Omar B.
Omar B. 2021 年 7 月 23 日
Thank you so much.
I also tried
sum=0;
for k=1:10
sum = sum + (-1)^(k-1)*((40-k^2)/(k^2+1));
end
>> disp(sum);

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

その他の回答 (1 件)

dpb
dpb 2021 年 7 月 23 日
編集済み: dpb 2021 年 7 月 23 日
N1=39; d1=8;
N2=36; d2=12;
D1=2; D2=5;
S=N1/D1-N2/D2;
for i=2:10
N1=N1-d1;
N2=N2-d2;
D1=D1+d1;
D2=D2+d2
S=S+N1/D1-N2/D2;
end
It could be written with vectors and colon more concisely; above is "dead-ahead" brute approach...

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by