Answer to Question #95809 in MatLAB for Ahmed

Question #95809
Consider the function f(x) = e
x where e = 2.718 . . . is the base of the natural logarithm
(ln = loge
), sometimes known as Napier’s constant. For x close to 0, e
x
can be approximated by a
finite Taylor series:
e
x ≈ s = 1 + x +
x
2
2! +
x
3
3! +
x
4
4! + · · · +
x
p
p!
where p is a positive integer.
(a) Write a MATLAB program with a for loop to compute (and display) the approximation s of
e
x and the difference d = s − e
x
for p = 1, 2, . . . , num where x and num are entered by the user.
Your program should use format long e and display the values s and d in two columns
with appropriate headings. (Recall that e
x
can be computed in MATLAB using exp(x).)
(b) Run your program with x = 0.5 and num = 10.
(c) Use your program to find the smallest value of p for which the difference s − e
0.5 is less than
10−14 in absolute value.
1
Expert's answer
2019-10-07T08:37:56-0400

(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.



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS