1 / 37

Lecture 3 Taylor Series Expansion

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

Download Presentation

Lecture 3 Taylor Series Expansion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 3 Taylor Series Expansion • Inline function • Taylor series • Matlab codes for Taylor series expansion 數值方法2008, Applied Mathematics NDHU

  2. inline function • How to createa 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 • Apply 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 • Get a string for representing an expression • Apply inline.m to translate a string to an inline function • Apply linspace.m to form a linear partition to [-5 5] • Determine function outputs of all elements in a partition vector • Plot a figure for given function 數值方法2008, Applied Mathematics NDHU

  6. Input a string, fs f=inline(fs) Use ‘inline’ to generatean inline function x=linspace(-5,5) form a uniform partition to [-5 5] y=f(x) plot(x,y) 數值方法2008, Applied Mathematics NDHU

  7. Demo Source codes % This example illustrates creation % of an inline function fprintf('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); 數值方法2008, Applied Mathematics NDHU

  8. cos(x) 數值方法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

  10. A sample from function surface f(x,y)=x.^2+3*y.^2 數值方法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

  12. 3d plot 3D plot t = 0:pi/50:10*pi; plot3(sin(t),cos(t),t); 數值方法2008, Applied Mathematics NDHU

  13. x=linspace(0,1); mesh x=linspace(-1,1); y=linspace(-1,1)'; X=repmat(x,100,1); Y=repmat(y,1,100); Z=X.^2+Y.^2 mesh(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 interp light lighting phong 數值方法2008, Applied Mathematics NDHU

  15. demo mesh • demo_mesh.m Key in a 2D function:cos(x+y) 數值方法2008, Applied Mathematics NDHU

  16. Taylor series 數值方法2008, Applied Mathematics NDHU

  17. Taylor’s Theorem 數值方法2008, Applied Mathematics NDHU

  18. Taylor’s Theorem • Approximate f(x) by first n+1 terms of Taylor series • Truncating error: 數值方法2008, Applied Mathematics NDHU

  19. Taylor series expansion 數值方法2008, Applied Mathematics NDHU

  20. c=1,n=3 數值方法2008, Applied Mathematics NDHU

  21. Normalfunction function y=f_normal(x) y=1/sqrt(2*pi)*exp(-x.^2); return source code x=linspace(-5,5); >> plot(x,f_normal(x)) 數值方法2008, Applied Mathematics NDHU

  22. Taylor expansion at c=1 with n=0 數值方法2008, Applied Mathematics NDHU

  23. Taylor expansion at c=1 with n=1 數值方法2008, Applied Mathematics NDHU

  24. Taylor expansion at c=1 with n=2 數值方法2008, Applied Mathematics NDHU

  25. Taylor expansion at c=1 with n=3 數值方法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; end plot(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 expression • Output: the derivative of a given expression 數值方法2008, Applied Mathematics NDHU

  28. >> x=sym('x'); >> diff(x.^2) ans = 2*x 數值方法2008, Applied Mathematics NDHU

  29. Derivative Form a string >> ss='x.^2'; >> inst=['diff(' ss ')']; >> eval(inst) ans = 2*x Form an instruction Evaluate the Instruction by calling eval 數值方法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 derivative ss=input('function:','s'); fx=inline(ss); ss=['diff(' ss ')']; ss1=eval(ss); fx1=inline(ss1) 數值方法2008, Applied Mathematics NDHU

  32. Example >> f_diff function:cos(x) 數值方法2008, Applied Mathematics NDHU

  33. Derivative % input a string to specify a function % plot its derivative ss=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

  34. 2nd derivative source codes f_diff3.m 數值方法2008, Applied Mathematics NDHU

  35. Example >> f_diff3 function 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_diff3 function 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) 數值方法2008, Applied Mathematics NDHU

  37. Exercise • ex3.pdf 數值方法2008, Applied Mathematics NDHU

More Related