A question about how to plot exponential function with Horner method in MatLab

124 Views Asked by At

I am at the beginning of learning how to use MatLab, and I am trying to plot the original $e^x$ and $e^x$ through Horner Method together in one plot. I consulted one instructor at the math department and he gave me some hints saying that I should follow the two pictures of codes below:

plot code

Horner method for exponential

After trying them, I still met many errors in MatLab, so many that I didn't take records of them. So I am wondering that whether someone here can help me with writing the codes or just give me some hints. Thanks very much!

1

There are 1 best solutions below

1
On BEST ANSWER

Just write

 clear all;
 x=1;
 H=1;
 for k=8:-1:1;
    H=1+x*H/k;
 end;
 [H,exp(x)], % [found value, theoretical value]

giving the answer $2.718278...$ to be compared to the exact value $e^{1}=2.7182818$ : a $4 \times 10^{-5}$ discrepancy.

Please note the construction k=8:-1:1 with step -1. Besides, no need for points in front of * and / symbols because we operate on numbers, not lists.

This formula works as well for complex numbers : if, instead of $x=1$, one sets $x=i*\pi/2$, the result is

$0.0000247 + 0.9998431 i$, very close to the exact value $e^{i \pi/2}=i$.

The same program can also be used for computing exponential of matrices. Try it and compare your result with expm(x).