User Tools

Site Tools


supp:octave:contrib:taylor:start

Octave: Taylor Series of exp(x)

Play. Plot the approximations for exp(x) as well as sin(x) and cos(x).

taylor_exp_series.m
% Exponential Series: Taylor Series of exponential function.
% 2015-04-22, Rolf Becker
 
x=[-3:0.1:3]';
 
% factors in the sum. f is a matrix. Each column is a factor evaluated for all x
 
f(:,1)=1*x.^0; % f0
f(:,2)=1*(1)*x.^1; % f1 
f(:,3)=1/(1*2)*x.^2; % f2
f(:,4)=1/(1*2*3)*x.^3;
f(:,5)=1/(1*2*3*4)*x.^4;
f(:,6)=1/(1*2*3*4*5)*x.^5;
f(:,7)=1/(1*2*3*4*5*6)*x.^6;
f(:,8)=1/(1*2*3*4*5*6*7)*x.^7;
f(:,9)=1/(1*2*3*4*5*6*7*8)*x.^8;
f(:,10)=1/(1*2*3*4*5*6*7*8*9)*x.^9;
 
% Taylor polynomials of exponential
T(:,1) = f(:,1);
 
for i = 2:10
T(:,i) = T(:,i-1)+f(:,i);
endfor 
 
%fe=f0+f1+f2+f3+f4+f5+f6+f7+f8+f9; % + ... + fn n->inf
%fc=f0-f2+f4-f6+f8; % cosine
%fs=f1-f3+f5-f7+f9; % sine
%plot(x,exp(x),x,fe)
%plot(x,fc,x,fs,x,sin(x),x,cos(x))
 
figure(1)
plot(x,exp(x),"k-+",x,T(:,1),x,T(:,2),x,T(:,3))
grid on
axis([-1 1 0 2])
 
figure(2)
plot(x,exp(x),"k-+",x,T)
grid on
axis([-3 3 -1 5])
supp/octave/contrib/taylor/start.txt · Last modified: 2018/06/07 05:44 by admin