﻿ 17074226-姜子怡-作业9-5.1+5.2+6.1+6.2 - 计算数学达人 - 专，学者，数值代数，微分方程数值解

# 5.1 Exercises

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

SolUse the three-point centered-difference formulawe can know

f’(x)=(f(x+h)-f(x-h))/2h-h2*f’’’(c)/6(f(x+h)-f(x-h))/2h

(a)  h=0.1 f’(0)=5*(e1/10-e-1/10)

(b) h=0.01 f’(0)=50*(e1/100-e-1/100)

(c)  h=0.001 f’(0)=500*(e1/1000-e-1/1000)

# 5.1 Computer Problems

1. Make a table of the error of the three-point centered-difference formula for f(0), where f(x)=sinx − cosx, 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?

Sol: 代码：function E=error(f,x)

for i=1:12

h(i)=1/(10.^i);

F(i)=(f(x+h(i))-f(x-h(i)))/(2*h(i));

e(i)=F(i)-1;

end

E(1:12,1)=h(1:12);

E(1:12,2)=F(1:12);

E(1:12,3)=e(1:12);

E=vpa(E,14);

x0=h(1:12);

y0=e(1:12);

plot(x0,y0)

E=error(f,0)

E =

[            0.1, 0.99833416646828,         -0.0016658335317179]

[           0.01, 0.99998333341667,       -0.000016666583334768]

[          0.001, 0.99999983333338,      -0.0000001666666240574]

[         0.0001, 0.99999999833289,   -0.0000000016671100055987]

[        0.00001, 0.99999999998435, -0.000000000015653367491097]

[       0.000001, 0.99999999997324, -0.000000000026755486715047]

[      0.0000001, 0.99999999947364,  -0.00000000052635584779637]

[     0.00000001, 0.99999999947364,  -0.00000000052635584779637]

[    0.000000001,  1.0000000272292,     0.000000027229219767833]

[   0.0000000001,  1.0000000827404,      0.00000008274037099909]

[  0.00000000001,  1.0000000827404,      0.00000008274037099909]

[ 0.000000000001,  1.0000333894311,        0.000033389431109754]

# 5.2 Exercises

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.

Solthe exact value is 1.

When m=1,

h=π/2Use the composite Trapezoid Rule,the value isπ/4*(y0+y1)= π/4;

so the error is 1-π/4;

when m=2,

h=π/4,use the composite Trapezoid Rule,the value isπ/8*(y0+y2+2y1)=(1+21/2)* π/8

so the error is 1-((1+21/2)* π/8)

when m=4

h=π/8,use the composite Trapezoid Rule ,the value isπ/16*(y0+y4+2y1+2y2+2y3)=(2*(cos(π/8+3π/8))+1+21/2)* π/16

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

Sol:(1)m=1,h=(b-a)/m=π/2,the value is 1,error=1-0=1

(2)m=2,h=π/4,the value is 1.0262,error=0.0262

(3)m=4,h=π/8,the value is 1.0065,error=0.0065

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

Sol:When m=1

h=π/4Use the composite Simpson’s Rule,the value is s1=(π/4)/3(y0+y2+4y1)=π/12(1+0+4*(2)^(1/2)/2;

so the error is 1-s1;

when m=2,

h=π/8,use the composite Simpson’s Rule,the value is s2= (π/8)/3(y0+y4+4(y1+y3)+2y2)

so the error is 1-s2

when m=4

h=π/16,use the composite Simpson’s Rule ,the value is s3=(π/16)/3(y0+y8+4(y1+y3+y5+y7)+2(y2+y4+y6))

the error is 1-s3

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

Sol: the exact value is π/2

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

Apply the composite Simpson’s Rule ,the value is 1/6*(y0+y2+4y1)=47/60

error=|π/2-47/60|

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

Apply the composite Simpson’s Rule ,the value is (1/4)/3(y0+y4+4(y1+y3)+2y2)0.78539167

error=|π/2-0.78539167|

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

Apply the composite Simpson’s Rule ,the value is (1/8)/3(y0+y8+4(y1+y3+y5+y7)+2(y2+y4+y6))

error=|π/2-|.

# 5.2 Computer Problems

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

Sol:

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

d>> 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

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

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

ans =

0.4404

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

ans =

0.4406

2. Apply the composite Simpson’s Rule to the integrals in Computer Problem 1. Use m=16 and 32, and report errors.

Sol：代码：

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

simpson=0;

d=0;

c=0;

for i=1:m-1

if (mod(i,2)==0)

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

else

d=d+f(a+(2*i-1)*(b-a)/2*m)

end

end

simpson=(b-a)/(6*m)*(f(a)+f(b)+2*c+4*d);

end

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

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

ans =

4.5194e+05

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

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

ans =

9.5482e+06

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

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

ans =

0.0172

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

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

ans =

0.0065

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

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

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

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

ans =

1.4644

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

ans =

1.4631

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

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

ans =

-0.2780

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

ans=

-0.3568

4. Apply the composite Simpson’s Rule to the integrals of Computer Problem 3, using m=16 and 32.

Sol:代码：

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

simpson=0;

d=0;

c=0;

for i=1:m-1

if (mod(i,2)==0)

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

else

d=d+f(a+(2*i-1)*(b-a)/2*m)

end

end

simpson=(b-a)/(6*m)*(f(a)+f(b)+2*c+4*d);

end

a

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

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

ans =

Inf

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

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

ans =

Inf

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

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

d =

-0.2292

d =

NaN

ans =

NaN

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

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

d =

2.9098

d =

NaN

ans =

NaN

# 5.3Exercises

1. Apply Romberg Integration to ﬁnd R33 for the integrals.

SolR11=e+1/2

R21=(e+1+2e^(1/2))

R31=(e+1+2e^(1/2))/8+(e^(1/4)+e^(3/4))/4

R32=(4R31-R21)/3

R33=(16R32-R22)/15=[3.5e+3.5+6e^*1/2)+16(e^1/4+e^3/4)]/45=1.7183

# 6.1 Exercises

3. Use separation of variables to ﬁnd solutions of the IVP given by y(0)=1 and the following differential equations:

(a) y=t          (b) y=t2 y      (c) y=2(t + 1)y

(d) y=5t4y        (e) y=1/y2      (f) y=t3/y2

Sol

(a)     cause y=t ,y=t2/2 +c,y(0)=1,y=t2/2+1

(b)     cause y’= t2 y   ,y=cet^3/3,y(0)=1,y= et^3/3

(c)     cause y=2(t + 1)y,y=cet^2+2,y(0)=1,y=et^2+2

(d)     cause y=5t4y   ,y=cet^5,y(0)=1,y= et^5

(e)     cause y=1/y2 ,t=y3/3+c,y(0)=1,y=(3t+1)1/3

(f)      cause y=t3/y2,y3/3=t4/4+c,y(0)=1,y=y3/3-t4/4-1/3=0

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 ﬁnd the error at t =1 by comparing with the correct solution.

Sol:Euler’s Method

w0=y0=1

wi+1=wi+hf(ti,wi)

(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.2 Exercises

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 comparing with the correct solution found in Exercise 6.1.3.

(a) y=t          (b) y=t2 y      (c) y=2(t + 1)y

(d) y=5t4y        (e) y=1/y2      (f) y=t3/y2

Sol:w0=y0=1

wi+1=wi+h/2(f(ti,wi)+f(ti+h,hf(ti,wi)))

(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.5817],error=0.0003

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