The question that is in the textbook that I have states:
The infinite series converges on a value of f (n) = π4/90 as n approaches infinity. Write a program in single precision to calculate f (n) for n = 10,000 by computing the sum from i = 1 to 10,000. Then repeat the calculation but in reverse order—that is, from i = 10,000 to 1 using increments of −1. In each case, compute the true percent relative error. Explain the results. The bottom image is the series.
My code that I have gives the exact same error values for forwards and backwards. 🙁 What is wrong with my code?
clear clc while (1) tru = ((pi^4)/90); p = input('Enter number of iterations (n): ' ); for i = 1:p y(i+1) = 1/(i^4); end z = sum (y); disp ('The approximation value is '); disp (z); absval = ((tru - z)/tru); disp ('The absolute value percent error from 1 to 10000 is '); disp (absval); for i = p:-1:1 y2(i+1) = 1/(i^4); end z2 = sum(y2); disp ('The approximation value backwards is '); disp (z2) absval2 = ((tru - z2)/tru); disp ('The absolute value percent error from 10000 to 1 is '); disp (absval2); m = input('Do you want to continue, Y/N [Y]:','s'); if m == 'N'; break elseif m == 'n'; break end end
Best Answer