How do you write this program in MATLAB? Mamthematically, computing the sum as s
ID: 3691396 • Letter: H
Question
How do you write this program in MATLAB?
Mamthematically, computing the sum
as
should be equal to computing it as
Additionally, note that the closed form of this sum is
Mathematically, computing the sumWrite a computer program to take in an input variable n, evaluate A and B (with the sum computed in the order from left to right as shown above), and compute the absolute errors S ? A and S ? B using the closed form of S. Run your program 6 times to provide input values of n of 10, 50, 100, 500, 1000, and 5000; for example:
>> sum in different orders( 10 )
>> sum in different orders( 50 )
>> sum in different orders( 100 )
>> sum in different orders( 500 )
>> sum in different orders( 1000 )
>> sum in different orders( 5000 )
Explanation / Answer
function [x,y] = sumWrite(n)
a = 0;
for i = 1:n
a += 1.0/(i*(i+1));
end
b=0;
for j = n:-1:1
b += 1.0/(j*(j+1));
end
s = n/(n+1);
x = s-a;
y=s-b;
end
[x,y]=sumWrite(10);
disp(x);
disp(y);
[x,y]=sumWrite(50);
disp(x);
disp(y);
[x,y]=sumWrite(100);
disp(x);
disp(y);
[x,y]=sumWrite(500);
disp(x);
disp(y);
[x,y]=sumWrite(1000);
disp(x);
disp(y);
[x,y]=sumWrite(5000);
disp(x);
disp(y);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.