# MATLAB: Am I not getting different values for the absolute value percent error

numerical methods for engineers 7th edition series converging backwards forwards single precision

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

• Hello,
I'll begin by explaining why you're getting the same value for both.
First, you are creating vectors y and y2 backwards but identically (also, no need to start the index at i+1, but I guess it doesn't hurt). Looking at your code you can see that it will compute the same thing for y and y2, it's just storing it backwards, so the sum of identical elements will be the same. Another issue is that you were asked to use single precision, which could change the sum as it is computed. So you need to compute the sum as you go, not store it in a vector (although you could work with this, but it's an unnecessary step), and make sure it's in single precision:
clearclcformat longwhile (1)    tru = ((pi^4)/90);    p = input('Enter number of iterations (n): ' );    z = single(0);    for i = 1:p        z = z + single(1/(i^4));    end    disp ('The approximation value is ');    disp (z);    absval = ((tru - z)/tru);    disp ('The absolute value percent error from 1 to 10000 is ');    disp (absval);    z2 = single(0);    for i = p:-1:1        z2 = z2 + single(1/(i^4));    end    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    endend
Running this yields,
>> PeterPhungsCodeEnter number of iterations (n): 10000The approximation value is    1.0823221The absolute value percent error from 1 to 10000 is    1.0283846e-06The approximation value backwards is    1.0823232The absolute value percent error from 10000 to 1 is    3.7106314e-08Do you want to continue, Y/N [Y]:N
Hope this helps and good luck!