﻿ 17074218-黄晓峰-作业9 5+6 - 计算数学达人 - 专，学者，数值代数，微分方程数值解

### 17074218-黄晓峰-作业9 5+6

5.1 Exercise  P252

2.Use the three-point centered-difference formula to approximate f(0)where f(x)=e^x, for  (a)h=0.1 (b)h=0.01 (c)h=0.001

Solution

(a) the three-point centered-difference formula evaluates to

f(x)=(f(x+h)-f(x-h))/2*h=(exp(0.1)-exp(-0.1))/0.2=1.001667500198441

The difference between this approximation and the correct derivative f(x)=e^x at x=0 is the error1.001667500198441 -1=0.001667500198441

(b)the three-point centered-difference formula evaluates to

f(x)=(f(x+h)-f(x-h))/2*h=(exp(0.01)-exp(-0.01))/0.02=1.000016666749992

The difference between this approximation and the correct derivative f(x)=e^x at x=0 is the error 1.000016666749992 -1=0.000016666749992

(c)the three-point centered-difference formula evaluates to

f(x)=(f(x+h)-f(x-h))/2*h=(exp(0.001)-exp(-0.001))/0.002=1.000000166666681

The difference between this approximation and the correct derivative

f (x)=e^x at x=0 is the error

1.000000166666681 -1=0.000000166666681

Computer problem

1.Make a table of the error of the three-point centered-difference formula for f’(0),where f(x)=sin(x)-cos(x),with h=10^(-1).....,10^(-12),as in the table in Section 5.1.2.Draw a plot of the results.Does the minimum error correspond to the theoretical expectation?

>> h=10^(-1);

Y=zeros(12,1);

>> i=1;while h>=10^(-12)

f1=[(sin(h)-cos(h))-(sin(-h)-cos(-h))]/(2*h);

Y(i,1)=f1;

vpa(Y,15)

h=h*10^(-1);

i=i+1;

end

>> Y

Y =

0.998334166468282

0.999983333416665

0.999999833333376

0.999999998332890

0.999999999984346

0.999999999973244

0.999999999473644

0.999999999473644

1.000000027229219

1.000000082740371

1.000000082740370

1.000033389431109

>> syms x

>> f=sin(x)-cos(x);

>> f1=diff(f)

f1 =

cos(x)+sin(x)

>> x=0;

>> f1(x)

ans =

1

>> Y1=ones(12,1);

error= Y-Y1

error =

-0.001665833531718

-0.000016666583335

-0.000000166666624

-0.000000001667110

-0.000000000015654

-0.000000000026756

-0.000000000526356

-0.000000000526356

0.000000027229219

0.000000082740371

0.000000082740370

0.000033389431109

Results:

H          f’(0)                        error

10(-1)      0.998334166468282     0.001665833531718

10(-2)      0.999983333416665     0.000016666583335

10(-3)      0.999999833333376     -0.000000166666624

10(-4)      0.999999998332890     -0.000000001667110

10(-5)      0.999999999984346     -0.000000000015654

10(-6)      0.999999999973244     -0.000000000026756

10(-7)      0.999999999473644     -0.000000000526356

10(-8)      0.999999999473644     -0.000000000526356

10(-9)      1.000000027229219     0.000000027229219

10(-10)     1.000000082740371     0.000000082740371

10(-11)     1.000000082740370     0.000000082740370

10(-12)     1.000033389431109     0.000033389431109

5.2 Exercises P263

1.Apply the composite Trapezoid Rule with m=1,2,and 4 panels to approximate the integral.Compute the error by comparing with the exact value from calculus.

(1)m=1,h=(b-a)/m=(π/2-0)/1=π/2

=(π/2)/2*(y0+y1)=π/4(1+0)=π/4

error=1-π/4≈0.2146

(2)m=2, h=(b-a)/m=(π/2-0)/2=π/4

=(π/4)/2*(y0+y2+2y1)=π/8(1+0+2^(1/2))≈0.9481

error=1-0.9481=0.0519

(3)m=4, h=(b-a)/m=(π/2-0)/4=π/8

=(π/8)/2*(y0+y4+2(y1+y2+y3))

=π/16*[1+0+2(cos(π/8)+ cos(2π/8)+ cos(3π/8))]

≈0.9871

error=1-0.9781=0.0129.

2.     Apply the Composite Midpoint Rule with m=1,2,and 4 panels to approximate the integrals the intergrals in Exercise 1,and report the errors.

(1)  .m=1

h=(b-a)/m=π/2 so,π/2*sin(π/4)=1.2337005501361698273543113749845

Error=0.233700550

(2)  m=2

h=(b-a)/2

π/4*(sin(π/8)+sin(3π/8))=0.0215309565

Error=0.9784690435

(3)  m=4

H=(b-a)/4=π/8

Π/8*(sin(π/16)+sin(3π/16)+sin(5π/16)+sin(7π/16))=0.0122757987

Error=0.9877242013

3. Apply the composite Simpson’s Rule with m=1,2,and 4 panels to approximate the integrals in exercise 1,and report the errors.

(1)m=1,h=(b-a)/2m=(π/2-0)/2=π/4

=(π/4)/3*(y0+y2+4y1)=π/12(1+0+4*(2)^(1/2)/2)≈1.0022799

error=|1-1.0022799|=0.0022799

(2)m=2, h=(b-a)/2m=(π/2-0)/4=π/8

=(π/8)/3*(y0+y4+4(y1+y3)+2y2)≈1.0001346

error=|1-1.0001346|=0.0001346.

(3)m=4, h=(b-a)/2m=(π/2-0)/8=π/16

=(π/16)/3*(y0+y8+4(y1+y3+y5+y7)+2(y2+y4+y6))≈1.0000083

error=|1-1.0000083|=0.0000083.

Computer

1.Use the composite Trapezoid Rule with m=16 and 32 panels to approximate the definite integral.Compare with the correct integral and report the two errors.

(d)     (h)

(d)

The definite integral is 7.0098 and 7.0014.

(2)Code:

function trapezoid = trapezoid(a,b,m,f)

trapezoid=0;

for i=1:m-1

trapezoid=trapezoid+f(a+i*(b-a)/m);

end

trapezoid=(b-a)/(2*m)*(f(a)+f(b)+2*trapezoid);

end

(3)Result:

>> f=@(x)(x^2)*(log(x));

>> trapezoid(1,3,16,f)

ans =

7.0098

>> f=@(x)(x^2)*(log(x));

>> trapezoid(1,3,32,f)

ans =

7.0014

m=16,error=9ln3-26/9-7.0098;

m=32,error=9ln3-26/9-7.0014.

(h)

The definite integral is 0.4404 and 0.4406.

(2)Code:

function trapezoid = trapezoid(a,b,m,f)

trapezoid=0;

for i=1:m-1

trapezoid=trapezoid+f(a+i*(b-a)/m);

end

trapezoid=(b-a)/(2*m)*(f(a)+f(b)+2*trapezoid);

end

(3)Result:

>> f=@(x)x/((x^4+1)^(1/2));

>> trapezoid(0,1,16,f)

ans =

0.4404

>> trapezoid(0,1,32,f)

ans =

0.4406

m=16,error=1/2(ln(2^(1/2))+1)-0.4404

m=32, error=1/2(ln(2^(1/2))+1)-0.4406.

3. Use the composite Trapezoid Rule with m=16 and 32 panels to approximate the definite integral.

(a)    (f)

(a)

The definite integral is 1.4644 and 1.4631.

(2)Code:

function trapezoid = trapezoid(a,b,m,f)

trapezoid=0;

for i=1:m-1

trapezoid=trapezoid+f(a+i*(b-a)/m);

end

trapezoid=(b-a)/(2*m)*(f(a)+f(b)+2*trapezoid);

end

(3)Result:

>> f=@(x)exp(x^2);

>> trapezoid(0,1,16,f)

ans =

1.4644

>> trapezoid(0,1,32,f)

ans =

1.4631

(f)

The definite integral is -0.2780 and -0.3568.

(2)Code:

function trapezoid = trapezoid(a,b,m,f)

trapezoid=0;

for i=1:m-1

trapezoid=trapezoid+f(a+i*(b-a)/m);

end

trapezoid=(b-a)/(2*m)*(f(a)+f(b)+2*trapezoid);

end

(3)Result:

>> f=@(x)cos(exp(x));

>> trapezoid(0,pi,16,f)

ans =

-0.2780

>> trapezoid(0,pi,32,f)

ans=

-0.3568

5.3 P268

1.Apply Romberg Integration to find R33 for the intergrals.

(c).1.71828269

P291 6.1

Exercise

3.Use separation of variables to find solutions of the IVP given by y(0)=1 and the

following differential equations:

(a) y'=t;   (d) y'=5*(t.^4)*y.

(a):

dy/dt=t,

dy=tdt,

dy=tdt,

y=(t.^2)/2+c,

y(0)=1, c=1,

y=(t.^2)/2+1.

(d):

dy/dt=5*(t.^4)*y,

dy/y=5*(t.^4)dt,

dy/y=5*(t.^4)dt,

ln|y|=t.^5+c,

|y|=e.^(t.^5+c)=(e.^c)*(e.^(t.^5)),

y(0)=1,

y=e.^(t.^5).

5.     Apply Euler’s Method with step size h=1/4 to the IVPS in Exercise 3 on the interval[0,1]/List the wi,i=0,...,4,and find the error at t=1 by comparing with the correct solution.

. (a) w = [1.0000,1.0000,1.0625,1.1875,1.3750], error = 0.1250

(b) w = [1.0000,1.0000,1.0156,1.0791,1.2309], error = 0.1648

(c) w = [1.0000,1.5000,2.4375,4.2656,7.9980], error = 12.0875

(d) w = [1.0000,1.0000,1.0049,1.0834,1.5119], error = 1.2064

(e) w = [1.0000,1.2500,1.4100,1.5357,1.6417], error = 0.0543

(f) w = [1.0000,1.0000,1.0039,1.0349,1.1334], error = 0.0717

6.2P301

1.     Using initial condition y(0)=1 and step size h=1/4,calculate the Trapezoid Method approximation w0,...,w4 on the interval [0,1].Find the error at t=1 by comaring with the correct solution found in Exercise 6.1.3

(a) w = [1.0000,1.0313,1.1250,1.2813,1.5000], error = 0 (b) w = [1.0000,1.0078,1.0477,1.1587,1.4054], error

= 0.0097 (c) w = [1.0000,1.7188,3.3032,7.0710,16.7935], error = 3.2920

(d) w = [1.0000,1.0024,1.0442,1.3077,2.7068], error = 0.0115

(e) w = [1.0000,1.2050,1.3570,1.4810,1.5871], error = 0.0003

(f) w = [1.0000,1.0020,1.0193,1.0823,1.2182], error = 0.0132