Upload
demetrius-duran
View
71
Download
2
Embed Size (px)
DESCRIPTION
Lecture 3 Taylor Series Expansion. Inline function Taylor series Matlab codes for Taylor series expansion. inline function. How to create a function ? Such as f(x)=sin(x)*cos(x) Two steps for creation of an inline function Get a string for representing a valid expression - PowerPoint PPT Presentation
Citation preview
數值方法2008, Applied Mathematics NDHU
1
Inline function Taylor series Matlab codes for Taylor series expansion
Lecture 3 Taylor Series Expansion
數值方法2008, Applied Mathematics NDHU
2
inline function
How to create a function ?Such as
f(x)=sin(x)*cos(x)Two steps for creation of an inline function
Get a string for representing a valid expressionApply inline.m to translate a string to an inline
function
數值方法2008, Applied Mathematics NDHU
3
>> g=inline('x^2+1')
g =
Inline function: g(x) = x^2+1
>> g(2)
ans =
5
數值方法2008, Applied Mathematics NDHU
4
>> g=inline('x^2+1')
g =
Inline function: g(x) = x^2+1
>> g(2)
ans =
5
數值方法2008, Applied Mathematics NDHU
5
Function plot
1. Get a string for representing an expression2. Apply inline.m to translate a string to an
inline function3. Apply linspace.m to form a linear partition
to [-5 5]4. Determine function outputs of all elements
in a partition vector5. Plot a figure for given function
數值方法2008, Applied Mathematics NDHU
6
Input a string, fs
f=inline(fs)
x=linspace(-5,5)
y=f(x)plot(x,y)
Use ‘inline’ to generate an inline function
form a uniform partition to [-5 5]
數值方法2008, Applied Mathematics NDHU
7
Demo
% This example illustrates creation% of an inline functionfprintf('input a string to specify a desired fun\n');fs=input(‘ex. x.^2+2*x+1:','s');f=inline(fs);range=5;x=-range:0.01:range;y=f(x);plot(x,y);
Source codes
數值方法2008, Applied Mathematics NDHU
9
Input a string, fs
f=inline(fs)
N=800;d=2;A=rand(2,N);
x=A(1,:); y=A(2,:);
z=f(x1,x2)plot3(x,y, z, '. ')
數值方法2008, Applied Mathematics NDHU
11
Demo
Source codes
% Inline function creation% The created inline function has two input arguments% Plot a sample from the function surface fs='x.^2+3*y.^2';f=inline(fs);a=rand(2,800)*2-1;x=a(1,:);y=a(2,:);z=f(x,y);plot3(x,y,z,'.');
數值方法2008, Applied Mathematics NDHU
13
mesh
x=linspace(0,1);x=linspace(-1,1);y=linspace(-1,1)';X=repmat(x,100,1);Y=repmat(y,1,100);Z=X.^2+Y.^2mesh(x,y,Z)
數值方法2008, Applied Mathematics NDHU
14
surface
x=linspace(-1,1);y=linspace(-1,1)';X=repmat(x,100,1);Y=repmat(y,1,100);surface(x,y,X.^2+Y.^2)shading interplightlighting phong
數值方法2008, Applied Mathematics NDHU
16
Taylor series
0,1,kfor exists )(
)(!
)(
)(!2
)(''))((')()(
near Expand
tionRepresenta Polynomial
0
2
cfif
cxk
cf
cxcf
cxcfcfxf
cxf
k
k
k
k
數值方法2008, Applied Mathematics NDHU
17
Taylor’s Theorem
c)(x, )(
b], [a cx,
b][a, intervalat exists
)()!1(
))(()(
!
)(
)(!2
)(''))((')()(
1
1)1(
0
2
x
fif
cxn
xfcx
k
cf
cxcf
cxcfcfxf
n
nn
kn
k
k
數值方法2008, Applied Mathematics NDHU
18
Taylor’s Theorem
Approximate f(x) by first n+1 terms of Taylor series
Truncating error:
kn
k
k
cxk
cf)(
!
)(
0
1)1(
)()!1(
))((
nn
cxn
xf
數值方法2008, Applied Mathematics NDHU
19
Taylor series expansion
)812)((
)(8)(8)(4)(
)24)((
)(4)(2)(2)(2)(
)(2)(
)(
)exp(2
1)(
3
3'''
2
2'''
'
2
xxxf
xfxxxfxxfxf
xxf
xfxxfxxfxfxf
xxfdx
xdfxf
xxf
數值方法2008, Applied Mathematics NDHU
20
c=1,n=3
k
k
k
cxk
cfxf )(
!
)()(
3
0
3'''
2 )(!3
)()(
!2
)(''))((')( cx
cfcx
cfcxcfcf
)exp(2
1)( 2xxf
數值方法2008, Applied Mathematics NDHU
21
Normal function
source codefunction y=f_normal(x) y=1/sqrt(2*pi)*exp(-x.^2);return
x=linspace(-5,5);>> plot(x,f_normal(x))
數值方法2008, Applied Mathematics NDHU
26
Taylor expansion
Source code Taylor_nor% Taylor expansion % f(x)=1/sqrt(2pi)*exp(-x^2);x=linspace(0.6,1.4,500);n=length(x);c=1;for i=1:n y1(i)=f_nm(c); y2(i)=-2*c*f_nm(c)*(x(i)-c); y3(i)=(4*c^2-2)*f_nm(c)/2*(x(i)-c)^2; y4(i)=(12*c-8*c^3)*f_nm(c)/6*(x(i)-c)^3;endplot(x,f_nm(x));hold on;plot(x,y1,'r'); title('one term');figure; plot(x,f_nm(x));hold on;plot(x,y1+y2,'r');title('two terms');figure; plot(x,f_nm(x));hold on;plot(x,y1+y2+y3,'r');title('three terms');figure; plot(x,f_nm(x));hold on;plot(x,y1+y2+y3+y4,'r');title('four terms');
數值方法2008, Applied Mathematics NDHU
27
Symbolic differentiation
Use diff.m to find the derivative of a function
diff Input: a string for representing a valid
expressionOutput: the derivative of a given expression
數值方法2008, Applied Mathematics NDHU
29
>> ss='x.^2';>> inst=['diff(' ss ')'];>> eval(inst) ans = 2*x
Form a string
Form an instruction
Evaluate the Instruction by
calling eval
Derivative
數值方法2008, Applied Mathematics NDHU
30
eval
Execute a string that collects MATLAB instructions
Ex: >> ss='x=linspace(-2*pi,2*pi);plot(x,cos(x))';>> eval(ss)
數值方法2008, Applied Mathematics NDHU
31
diff
source codes f_diff
% input a string to specify a function% find its derivativess=input('function:','s');fx=inline(ss);ss=['diff(' ss ')'];ss1=eval(ss);fx1=inline(ss1)
數值方法2008, Applied Mathematics NDHU
33
Derivative
% input a string to specify a function% plot its derivativess=input('function of x:','s');fx=inline(ss);x=sym('x');ss=['diff(' ss ')'];ss1=eval(ss);fx1=inline(ss1)x=linspace(-1,1);plot(x,fx(x),'b');hold on;plot(x,fx1(x),'r');
source code : f_diff2
數值方法2008, Applied Mathematics NDHU
35
Example
>> f_diff3function of x:x.^3-2*x+1
fx1 =
Inline function: fx1(x) = 3.*x.^2-2
fx2 =
Inline function: fx2(x) = 6.*x
數值方法2008, Applied Mathematics NDHU
36
Example
>> f_diff3function of x:tanh(x)
fx1 =
Inline function: fx1(x) = 1-tanh(x).^2
fx2 =
Inline function: fx2(x) = -2.*tanh(x).*(1-tanh(x).^2)