(a)
format long
x = input('Input x >> ');
num = input('Input x >> ');
s = 1;
xp = 1;
e = exp(x);
disp(' approximation s difference d = s − e')
for p = 1:num
xp = xp*x/p;
s = s + xp;
d = s - e;
disp([p s d])
end;
(b)
Input x >> 0.5
Input x >> 10
approximation s difference d = s − e
1.000000000000000 1.500000000000000 -0.148721270700128
2.000000000000000 1.625000000000000 -0.023721270700128
3.000000000000000 1.645833333333333 -0.002887937366795
4.000000000000000 1.648437500000000 -0.000283770700128
5.000000000000000 1.648697916666667 -0.000023354033462
6.000000000000000 1.648719618055555 -0.000001652644573
7.000000000000000 1.648721168154762 -0.000000102545366
8.000000000000000 1.648721265035962 -0.000000005664166
9.000000000000000 1.648721270418251 -0.000000000281877
10.000000000000000 1.648721270687366 -0.000000000012763
(c)
x = 0.5;
s = 1;
xp = 1;
e = exp(x);
for p = 1:20
xp = xp*x/p;
s = s + xp;
d = s - e;
if abs(d)<1e-14
fprintf('the smallest value of p is %g\n',p)
break;
end;
end;
Ouput
the smallest value of p is 13
So 13 is the smallest value of p for which the difference s − e
0.5 is less than
10−14 in absolute value.
Comments
Leave a comment