Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a piece of code (a script) which calculates the following sum. This sum co

ID: 3695110 • Letter: W

Question

Write a piece of code (a script) which calculates the following sum.

This sum converges to 2 as n goes to infinity (see https://en.wikipedia.org/wiki/Convergent_series for other examples). In MATLAB, as you continue to add the terms to the total sum, at some point the total sum will stop increasing due to machine precision. In other words, when you add 2.41 x 10-17 to 1, the answer will exactly equal 1 (this is, of course, mathematically incorrect)! This peculiarity occurs because the term you add is too small compared to 1, and MATLAB cannot resolve such a small value when it is added to a large number. Therefore, your code should stop calculating the summation when it realizes adding new terms does not change the total sum. Additionally, you should display the total sum and the last term added to the total sum on command window.

Explanation / Answer

t = 1;
s = 0;
prev = -1;
while true
a = s+t;
if(s==a)
break;
end
s = a;
prev = t;
t=t/2;
end
disp("The total sum is:")
disp(s);
disp("The last value added is:")
disp(prev);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote