Page 231 - Applied Numerical Methods Using MATLAB
P. 231
220 NUMERICAL DIFFERENTIATION/ INTEGRATION
ž taylor(f,n + 1,a) gives the n th-order Taylor series expansion of f about
default variable =a.
ž taylor(f,n + 1,a,y) gives the nth-order Taylor series expansion of f(y)
about y=a.
(cf) The target function f must be a legitimate expression given directly as the first
input argument.
(cf) Before using the command “taylor()”, one should declare the arguments of the
function as symbols by putting the statement like “syms x t”.
(cf) In the case where the function has several arguments, it is a good practice to put the
independent variable as the last input argument of “taylor()”, though taylor()
takes one closest (alphabetically) to ‘x’ as the independent variable by default only
if it has been declared as a symbolic variable and is contained as an input argument
of the function f.
(cf) One should use the MATLAB command “sym2poly()” if he wants to extract the
coefficients from the Taylor series expansion obtained as a symbolic expression.
The following MATLAB program “nm5e01” finds us the coefficients of fifth-
order Taylor series expansion of e −x about x = 0 by using the two methods.
%nm5e01:Nth-order Taylor series expansion for e^-x about xo in Ex 5.1
f=inline(’exp(-x)’,’x’);
N=5;xo=0;
%Numerical computation method
T(1) = feval(f,xo);
h = 0.005 %.01 or 0.001 make it worse
tmp=1;
for i = 1:N
tmp = tmp*i*h; %i!(factorial i)*h^i
c = difapx(i,[-i i]); %coefficient of numerical derivative
dix = c*feval(f,xo + [-i:i]*h)’; %/h^i; %derivative
T(i+1) = dix/tmp; %Taylor series coefficient
end
format rat, Tn = fliplr(T) %descending order
%Symbolic computation method
syms x; Ts = sym2poly(taylor(exp(-x),N + 1,xo))
%discrepancy
format short, discrepancy=norm(Tn - Ts)
5.4 INTERPOLATING POLYNOMIAL AND NUMERICAL
DIFFERENTIAL
The difference approximation formulas derived in the previous sections are appli-
cable only when the target function f(x) to differentiate is somehow given. In
this section, we think about how to get the numerical derivatives when we are