Why am I not getting different values for the absolute value percent error?

1 Ansicht (letzte 30 Tage)
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

Akzeptierte Antwort

John Chilleri
John Chilleri am 23 Sep. 2017
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:
clear
clc
format long
while (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
end
end
Running this yields,
>> PeterPhungsCode
Enter number of iterations (n): 10000
The approximation value is
1.0823221
The absolute value percent error from 1 to 10000 is
1.0283846e-06
The approximation value backwards is
1.0823232
The absolute value percent error from 10000 to 1 is
3.7106314e-08
Do you want to continue, Y/N [Y]:N
Hope this helps and good luck!
  2 Kommentare
John Chilleri
John Chilleri am 23 Sep. 2017
Also, in case you're interested, you can print the number of iterations they input using fprintf or sprintf within display, for example:
p = 10000;
disp(sprintf('The absolute value percent error from %d to 1 is ', p))
p = 10;
disp(sprintf('The absolute value percent error from %d to 1 is ', p))
Results in,
The absolute value percent error from 10000 to 1 is
The absolute value percent error from 10 to 1 is

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by