Please help! rt Programming 1 It is easy to verify that 3G+ 1) Write a n + 1 Mat
ID: 3112040 • Letter: P
Question
Please help!
rt Programming 1 It is easy to verify that 3G+ 1) Write a n + 1 Matlab program to compute the summation on the left side (for any n) in two different ways: 1) from the smallest to the largest; 2) from the largest to the smallest. For each value of n 10000, 5000000, 50000000, compute the errors of both ways. Explain your result. (Note: Double precision is used in Matlab by default. The errors may be well smaller than 106. Put 'format long in the beginning of your code to see the actual errors. Also make sure to print out enough digits. Otherwise, you may think the error is zero, even though it is NOT.)Explanation / Answer
clc;
clear all;
n=10000;
sum=0;
for i=1:n
sum=sum+(1/(i*(i+1)));
end
sum
err=abs(sum-(n/(n+1)))
sum =
0.999900009999001
err =
6.661338147750939e-016
clc;
clear all;
n=5000000;
sum=0;
for i=1:n
sum=sum+(1/(i*(i+1)));
end
sum
err=abs(sum-(n/(n+1)))
sum =
0.999999800000086
err =
4.618527782440651e-014
n=50000000
sum =
0.999999980030403
err =
3.040268037324267e-011