﻿ 17074207 作业九 5.1 5.2 5.3 6.1 6.2 - 计算数学达人 - 专，学者，数值代数，微分方程数值解

### 17074207 作业九 5.1 5.2 5.3 6.1 6.2

Title: 5.1 Exercises and Computer Problems

Exercises

1.      Use the three-point centered-difference formula to approximate f'(0),where f(x)=e^x, for (c)h=0.001.

Sol:

f'(x)=(f(x+h)-f(x-h))/2h

f'(0)=(f(h)-f(-h))/2h

f'(0)=(f(0.001)-f(-0.001))/0.002=(exp(0.001)-exp(-0.001))/0.002=1.000000166666681

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?

Analysis:

Code:

function [r]=ND(n)

for i=1:n

h=10^(-i);

f=sin(h)/h;

e=f-1;

r=[h,f,e]

end

test:

clear

clc

n=12;

[r]=ND(n)

Result:

r =

0.100000000000000   0.998334166468282  -0.001665833531718

r =

0.010000000000000   0.999983333416666  -0.000016666583334

r =

0.001000000000000   0.999999833333342  -0.000000166666658

r =

0.000100000000000   0.999999998333333  -0.000000001666667

r =

0.000010000000000   0.999999999983333  -0.000000000016667

r =

0.000001000000000   0.999999999999833  -0.000000000000167

r =

0.000000100000000   0.999999999999998  -0.000000000000002

r =

0.000000010000000   1.000000000000000                   0

r =

0.000000001000000   1.000000000000000                   0

r =

0.000000000100000   1.000000000000000                   0

r =

0.000000000010000   1.000000000000000                   0

r =

0.000000000001000   1.000000000000000                   0

f'(0)=(f(h)-f(-h))/2h=sinh/h.

h                       f                       e

0.100000000000000   0.998334166468282  -0.001665833531718

0.010000000000000   0.999983333416666  -0.000016666583334

0.001000000000000   0.999999833333342  -0.000000166666658

0.000100000000000   0.999999998333333  -0.000000001666667

0.000010000000000   0.999999999983333  -0.000000000016667

0.000001000000000   0.999999999999833  -0.000000000000167

0.000000100000000   0.999999999999998  -0.000000000000002

0.000000010000000   1.000000000000000                   0

0.000000001000000   1.000000000000000                   0

0.000000000100000   1.000000000000000                   0

0.000000000010000   1.000000000000000                   0

0.000000000001000   1.000000000000000                   0

Title: 5.2 Exercises and Computer Problems

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.

int(cos(x),0,pi/2)

Sol:

f =cos(x),int(f,0,pi/2)=1;

m=1,h=pi/2;

int(cos(x),0,pi/2)=h/2*(y0+y1)=pi/4*(cos(0)+cos(pi/2))=0.785398163397448

error=0.214601836602552

m=2,h=pi/4;

int(cos(x),0,pi/2)=h/2*(y0+y2+2*y1)=pi/8*(cos(0)+cos(pi/2)+2*cos(pi/8))=1.118312369733582

error=-0.118312369733582

m=4;h=pi/8;

int(cos(x),0,pi/2)=h/2*(y0+y4+2*(y1+y2+y3))=pi/16*(cos(0)+cos(pi/2)+2*(cos(pi/8)+cos(pi/4)+cos(pi*3/8)))=0.987115800972776

error=0.012884199027224

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:

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:

f =cos(x),int(f,0,pi/2)=1;

m=1,h=pi/4;

int(cos(x),0,pi/2)=h/3*(y0+y2+4*y1)=pi/12*(cos(0)+cos(pi/2)+4*cos(pi/4))=1.002279877492210

error=-0.002279877492210

m=2,h=pi/8;

int(cos(x),0,pi/2)=h/3*(y0+y4+4*(y1+y3)+2*y2)=pi/24*(cos(0)+cos(pi/2)+4*(cos(pi/8)+cos(pi*3/8))+2*cos(pi/4))=1.000134584974194

error=-1.345849741940519e-04

m=4;h=pi/16;

int(cos(x),0,pi/2)=h/3*(y0+y8+4*(y1+y3+y5+y7)+2*(y2+y4+y6))=pi/48*(cos(0)+cos(pi/2)+4*(cos(pi/16)+cos(pi*3/16)+cos(pi*5/16)+cos(pi*7/16))+2*(cos(pi/8)+cos(pi/4)+cos(pi*3/8)))=1.000008295523968

error=-8.295523967971619e-06

4.      Apply the composite Simpson’s Rule with m = 1,2, and 4 panels to the integrals, and report the errors. (b).ʃ10dx/(1+x^2)dx

Sol:

Computer problems

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)f=x^2*lnx,int(f,1,3);

(h)f=x/((x^4+1)^1/2),int(f,0,1).

Analysis:

Code:

function F = ND( f, a, b, m )

h = (b - a) / m;

F = 0;

if m >= 2

for i = 1 : m-1

F = F + 2 * f(a + h * i);

end

end

F = F + f(a) + f(b);

F = F * h / 2;

end

Result:

(d).

clear

clc

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

F16 = ND(f, 1, 3, 16);

F32 = ND(f, 1, 3, 32);

correct = quadgk(f, 1, 3);

E16 = abs(correct - F16);

E32 = abs(correct - F32);

fprintf('F16 = %gn', F16);

fprintf('F32 = %gn', F32);

fprintf('correct = %gn', correct);

fprintf('E16 = %gn', E16);

fprintf('E32 = %gn', E32);

F16 = 7.00981

F32 = 7.00142

correct = 6.99862

E16 = 0.0111875

E32 = 0.0027968

(h).

clear

clc

f = @(x) x./((x.^4+1).^0.5);

F16 = ND(f, 0, 1, 16);

F32 = ND(f, 0, 1, 32);

correct = quadgk(f, 0, 1);

E16 = abs(correct - F16);

E32 = abs(correct - F32);

fprintf('F16 = %gn', F16);

fprintf('F32 = %gn', F32);

fprintf('correct = %gn', correct);

fprintf('E16 = %gn', E16);

fprintf('E32 = %gn', E32);

F16 = 0.440361

F32 = 0.440605

correct = 0.440687

E16 = 0.000325611

E32 = 8.13858e-05

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

Analysis:

Code:

Result:

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

(a).f=e^(x^2),int(f,0,1);

(f).f=cos(e^x),int(f,0,pi).

Analysis:

Code:

function F = ND( f, a, b, m )

h = (b - a) / m;

F = 0;

if m >= 2

for i = 1 : m-1

F = F + 2 * f(a + h * i);

end

end

F = F + f(a) + f(b);

F = F * h / 2;

end

Result:

(a).

clear

clc

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

F16 = ND(f, 0, 1, 16);

F32 = ND(f, 0, 1, 32);

fprintf('F16 = %gn', F16);

fprintf('F32 = %gn', F32);

F16 = 1.46442

F32 = 1.46309

(f).

clear

clc

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

F16 = ND(f, 0, pi, 16);

F32 = ND(f, 0, pi, 32);

fprintf('F16 = %gn', F16);

fprintf('F32 = %gn', F32);

F16 = -0.278013

F32 = -0.35679

4.      Apply the composite Simpson’s Rule to the integrals in the Computer Problem 3. Use m =16 and 32.

Analysis:

Code:

Result:

Title: 5.3 Exercises and Computer Problems

Exercises

1.      Apply Pomberg Integration to find R33 for the integrals.

(c). ʃ10e^xdx

Sol:  (c)1.71828269

Title: 6.1 &6.2 Exercises and Computer Problems

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

(a)y'=t;    (b)y’=t^2*y;     (c)y’=2(t+1)y;     (d)y'=5*t^4*y    (e)y’=1/y^2;     (f)y’=t^3/y^2.

Sol

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

(b)y=e^(t^3/3).

(c)y=e^(t^2+2t).

(d)|y|=e^(t^5) or y=0.

(e)y=(3t+1)^(1/3).

(f)y=(3t^4/4+1)^(1/3).

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.

Sol

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

(b)w=[1.0000,1.0000,1.0156,1.0791,1.2309],误差=0.1648.

(c)w=[1.0000,1.5000,2.4375,4.2656,7.9980],误差=12.0875.

(d)w=[1.0000,1.0000,1.0049,1.0834,1.5119],误差=1.2064.

(e)w=[1.0000,1.2500,1.4100,1.5357,1.6417],误差=0.0543.

(f)w=[1.0000,1.0000,1.0039,1.0349,1.1334],误差=0.0717.

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.

Sol

(a)w=[1.0000,1.0313,1.1250,1.2813,1.5000],误差=0.

(b)w=[1.0000,1.0078,1.0477,1.1587,1.4054],误差=0.0097.

(c)w=[1.0000,1.7188,3.3032,7.0710,16.7935],误差=3.2920.

(d)w=[1.0000,1.0024,1.0442,1.3077,2.7068],误差=0.0115.

(e)w=[1.0000,1.2050,1.3570,1.4810,1.5871],误差=0.0003.

(f)w=[1.0000,1.0020,1.0193,1.0823,1.2182],误差=0.0132.

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.Dose the minimum error correspond to the theoretical expectation

Analysis

Code

a=[];

b=[];

for i=1:12

y=@(x)sin(x)-cos(x);

x=10^(-i)

y1=(y(x)-y(-x))/(2*x)

a(i)=x;

b(i)=y1;

end

Result

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)ʃ((x^2)*lnx,1,3)

(h)ʃ(x/sqrt((x^4)+1))

Analysis

Code

function y = traint(a,b,n,f)

h = (b - a) / n;

x = linspace(a,b,n+1);

y1 = h * feval(f,x);

y1(1) = y1(1) / 2;

y1(n+1) = y1(n+1) / 2;

y = sum(y1);

Result

>> f=inline('(x.*x).*log(x)','x');

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

ans =

7.009809235924680

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

ans =

7.001418506166504

>> syms x;

>> int(x^2*log(x),1,3)

ans =

9*log(3) - 26/9=6.99862

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

ans =

1.005896390281055

>> f=inline('x./sqrt((x.*x.*x.*x)+1)');

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

ans =

0.440605407679783

>> syms x

>> int(x/sqrt((x^4)+1),0,1)

ans =

log(2^(1/2) + 1)/2=0.440687

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

Analysis

Code:

Result

3.      Use the composite Trapezoid Rule with m=16 and 32 panels to approximate the definite integral. (a)ʃ(exp(x^2),0,1)  (f)ʃ(cos(exp(x)),0,pi)

Analysis

Code

function y = traint(a,b,n,f)

h = (b - a) / n;

x = linspace(a,b,n+1);

y1 = h * feval(f,x);

y1(1) = y1(1) / 2;

y1(n+1) = y1(n+1) / 2;

y = sum(y1);

Result

(a)>> f=inline('exp(x.*x)');

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

ans =

1.464420310149482

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

ans =

1.463094102606428

(f)>> f=inline('cos(exp(x.*x))');

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

ans =

-0.109110932137825

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

ans =

-0.130624277032587

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

Analysis

Code:

Result: