Need help with the code for MATLAB Write a MATLAB program to estimate the sum of
ID: 1842348 • Letter: N
Question
Need help with the code for MATLAB
Write a MATLAB program to estimate the sum of the infinite series:f(N) = sigma_n^N 1 1/n^4 (a) Use N = 100,000. The exact value of f(N) = pi^4/90 as N rightarrow infinity. Compute the sum two ways. First sum from 0 to N. Then repeat the calculation but in reverse order - that is, from N to 1 in increments of -1. Find the true error for each case. (b) Do the same exercise as above but using single precision. You can convert MATLAB variables to single precision as shown in the following example: sum = 0.; sum1 = single(sum); This creates a single-precision variable, sum1. which has the same value as the double-precision variable, sum, except that it is stored in 32 bits rather than in 64 bits. Operations done on sum1 will automatically be carried out in single precision, and results of those calculations will be stored in 32-bit format. So the trick is that you need to declare your variables as single precision BEFORE you begin your calculations. (c) Comment on your results, and see if you can explain them. You should present your solution to this problem formally:Explanation / Answer
(A)
N = 100000;
format long
true = pi^4/90
sum_forward = 0;
for i = 1:1:N
sum_forward = sum_forward + (1/(i^4));
end
sum_forward
sum_backward = 0;
for i = N:-1:1
sum_backward = sum_backward + (1/(i^4));
end
sum_backward
error_forward = (true-sum_forward)
error_backward = (true-sum_backward)
output:
>> sum_infinte
true =
1.082323233711138
sum_forward =
1.082323233710861
sum_backward =
1.082323233711138
error_forward =
2.768896223415140e-13
error_backward =
0
(B)
N = 100000;
true = pi^4/90;
true1 = single(true)
sum_forward = 0;
sum_forward1 = single(sum_forward);
for i = 1:1:N
sum_forward1 = sum_forward1 + (1/(i^4));
end
sum_forward1
sum_backward = 0;
sum_backward1 = single(sum_backward);
for i = N:-1:1
sum_backward1 = sum_backward1 + (1/(i^4));
end
sum_backward1
error_forward1 = (true-sum_forward1)
error_backward1 = (true-sum_backward1)
output:
>> sum_infinte
true1 =
1.0823232
sum_forward1 =
1.0823221
sum_backward1 =
1.0823232
error_forward1 =
1.1130446e-06
error_backward1 =
4.0161027e-08
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.