Example: confidence

Taylor Series in MATLAB - Texas A&M University

Taylor Series in MATLABF irst, let s review our two main statements on Taylor polynomials with 1.( Taylor polynomial with integral remainder) Suppose a functionf(x) and itsfirstn+ 1 derivatives are continuous in a closed interval [c, d] containing the pointx= for any valuexon this intervalf(x) =f(a) +f (a)(x a) +..+f(n)(a)n!(x a)n+1n! xaf(n+1)(y)(x y) 2.(Generalized Mean Value Theorem) Suppose a functionf(x) and its firstnderivatives are continuous in a closed interval [a, b], and thatf(n+1)(x) exists on this there exists somec (a, b) sof(b) =f(a) +f (a)(b a) +..+f(n)(a)n!(b a)n+f(n+1)(c)(n+ 1)!(b a)n+ s clear from the fact thatn! grows rapidly asnincreases that for sufficiently differen-tiable functionsf(x) Taylor polynomials become more accurate the Taylor polynomials of orders 1, 3, 5, and 7 nearx= 0 forf(x) = sinx.

Taylor Series in MATLAB First, let’s review our two main statements on Taylor polynomials with remainder. Theorem 1. (Taylor polynomial with integral remainder) Suppose a function f(x) and its

Tags:

  Series, Taylor, Matlab, Taylor series in matlab

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Taylor Series in MATLAB - Texas A&M University

1 Taylor Series in MATLABF irst, let s review our two main statements on Taylor polynomials with 1.( Taylor polynomial with integral remainder) Suppose a functionf(x) and itsfirstn+ 1 derivatives are continuous in a closed interval [c, d] containing the pointx= for any valuexon this intervalf(x) =f(a) +f (a)(x a) +..+f(n)(a)n!(x a)n+1n! xaf(n+1)(y)(x y) 2.(Generalized Mean Value Theorem) Suppose a functionf(x) and its firstnderivatives are continuous in a closed interval [a, b], and thatf(n+1)(x) exists on this there exists somec (a, b) sof(b) =f(a) +f (a)(b a) +..+f(n)(a)n!(b a)n+f(n+1)(c)(n+ 1)!(b a)n+ s clear from the fact thatn! grows rapidly asnincreases that for sufficiently differen-tiable functionsf(x) Taylor polynomials become more accurate the Taylor polynomials of orders 1, 3, 5, and 7 nearx= 0 forf(x) = sinx.

2 (Even orders are omitted because Taylor polynomials for sinxhave no evenorder terms.)The MATLAB command for a Taylor polynomial istaylor(f,n+1,a), wherefis thefunction,ais the point around which the expansion is made, andnis the order of thepolynomial. We can use the following code:>>syms x>>f=inline( sin(x) )f =Inline function:f(x) = sin(x)>> Taylor (f(x),2,0)ans =x>> Taylor (f(x),4,0)ans =x-1/6*x 3>> Taylor (f(x),6,0)ans =x-1/6*x 3+1/120*x 5>> Taylor (f(x),8,0)ans =x-1/6*x 3+1/120*x 5-1/5040*x 71 Example the Taylor polynomials of orders 1, 2, 3, and 4 nearx= 1 forf(x) = MATLAB :>>syms x>>f=inline( log(x) )f =Inline function:f(x) = log(x)>> Taylor (f(x),2,1)ans =x-1>> Taylor (f(x),3,1)ans =x-1-1/2*(x-1) 2>> Taylor (f(x),4,1)ans =x-1-1/2*(x-1) 2+1/3*(x-1) 3>> Taylor (f(x),5,1)ans =x-1-1/2*(x-1) 2+1/3*(x-1) 3-1/4*(x-1) 4 Example [0, ], plotf(x) = sinxalong with Taylor approximations aroundx= 0 withn= 1,3,5, can now solve Example 1 with the following MATLAB functionM-file, taylorplot(f,a,left,right,n)%TAYLORPLOT: MATLAB function M-file that takes as input%a function in inline form, a center point a, a left endpoint,%a right endpoint, and an order, and plots%the Taylor polynomial along with the function at the given xp = vectorize( Taylor (f(x),n+1,a));x=linspace (left,right,100);f=f(x);p=eval(p).

3 Plot(x,f,x,p, r )The plots in Figure 1 can now be created with the sequence of commands>>f=inline( sin(x) )f =Inline function:f(x) = sin(x)2>>taylorplot(f,0,0,pi,1)>>taylorp lot(f,0,0,pi,3)>>taylorplot(f,0,0,pi,5)> >taylorplot(f,0,0,pi,7) 2 1 1: Taylor approximations off(x) = sinxwithn= 1,3,5,7. Example a Taylor polynomial aroundx= 0 to approximate the natural baseewithan accuracy of . , we observe that forf(x) =ex, we havef(k)(x) =exfor allk= 0,1, .. Conse-quently, the Taylor expansion forexisex= 1 +x+12x2+16x3+..If we take annthorder approximation, the error isf(n+1)(c)(n+ 1)!xn+1,3wherec (a, b). Takinga= 0 andb= 1 this is less thansupc (0,1)ec(n+ 1)!=e(n+ 1)! 3(n+ 1)!.Notice that we are using a crude bound one, because if we are trying to estimate it, weshould not assume we know its value with much accuracy.

4 In order to insure that our erroris less than .0001, we need to findnso that3(n+ 1)!< . different values fornin MATLAB , we eventually find>>3/factorial(8)ans = implies that the maximum error for a 7thorder polynomial witha= 0 andb= 1 That is, we can approximateewithe1= 1 + 1 +12+16+14!+15!+16!+17!= ,which we can compare with the correct value to five decimal placese= error is =.00003. Partial Sums in MATLABFor the infinite Series k=1ak, we define thenthpartial sum asSn:=n k= say that the infinite Series converges toSiflimn Sn= the seriesn k=11k3/2= 1 +123/2+133/2+.. ,compute the partial sumsS10,S10000,S1000000, andS10000000and make a conjecture as towhether or not the Series converges, and if it converges, what it converges can solve this with the following MATLAB code:4>>k=1:10;>>s=sum( (3/2))s = >>k=1:10000;>>s=sum( (3/2))s = >>k=1:1000000;s=sum( (3/2))s = >>k=1:10000000;s=sum( (3/2))s = Series seems to be converging to a value near Example the harmonic Series k=11k= 1 +12+13+.

5 ,compute the partial sumsS10,S10000,S1000000, andS10000000, and make a conjecture as towhether or not the Series converges, and if it converges, what it converges to. We can solvethis with the following MATLAB code:>>k=1:10;>>s=sum( )s = >>k=1:10000;>>s=sum( )s = >>k=1:1000000;>>s=sum( )s = >>k=1:10000000;>>s=sum( )s = we will show in class, this Series diverges. 5 Assignments1. Forx [0, ], plotf(x) = cosxalong with Taylor approximations aroundx= 0 withn= 0,2,4, Use an appropriate Taylor Series to write a MATLAB function M-file that approximatesf(x) = sin(x) for allxwith a maximum error That is, your M-file should takevalues ofxas input, return values of sinxas output, but shouldnotuse MATLAB sbuilt-in (Hint. You might find MATLAB s built-in filemoduseful indealing with the periodicity.)

6 3. Use numerical experiments to arrive at a conjecture aboutwhether or not the infiniteseries k=21k(lnk)


Related search queries