how to write a Matlab code to sum 10 terms of rational numbers?
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Omar B.
am 23 Jul. 2021
Kommentiert: Omar B.
am 23 Jul. 2021
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 Kommentare
James Tursa
am 23 Jul. 2021
What is the formula for the terms? Do you want a double precision answer, or an exact fraction (i.e. symbolic) answer?
Akzeptierte Antwort
Debarati Bhattacharyya
am 23 Jul. 2021
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!
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!