Sie sind auf Seite 1von 16

1. Solution.

a) The following is the program of a).

clear; close;
format long
N=2;err=1;x0=0;x1=1;s1=0;
f=@(x) sin(x)./(1+x.^4)+sin(1./x).* x.^2./(1+x.^4);
disp(' Step approximation')
while err>=10^(-4)/2
h=(x1-x0)/N;
s=0*h/2;
for i=1:N-1
s=s+h*f(x0+h*i);
end
s=s+f(x1)*h/2;
err=abs(s-s1)/(2^4-1);
s1=s;
disp([N, s])
N=N*2;
end

The following is the result:

Step approximation

2.000000000000000 0.542955932230503

4.000000000000000 0.554587142041583

8.000000000000000 0.568766288145437

16.000000000000000 0.569553726654222

32.000000000000000 0.569624349130263
b) The following is the program of b).

clear; close;
format long
N=2;err=1;s1=1;
f=@(x) exp(-x)./(1+x).^(5)+x.^3.*exp(-1./x)./(x+1).^5;
disp('Step approximation')
while err>=10^(-4)/2
x0=0;x1=1; h=(x1-x0)/N;
s=1*h/2;
for i=1:N-1
s=s+h*f(x0+h*i);
end
s=s+f(x1)*h/2;
err=abs(s1-s)/(2^4-1);
s1=s;
disp([N, s])
N=N*2;
end

The following is the result:

Step approximation

2.000000000000000 0.296798162037338

4.000000000000000 0.221110701061077

8.000000000000000 0.199193261253196

16.000000000000000 0.193453482148226

32.000000000000000 0.192000466701357

64.000000000000000 0.191636049869488

1.0e+002 *

1.280000000000000 0.001915448724313
c) The following the program of c).

clear; close;
format long
N=2;err=1;s1=0;
f=@(x) exp(-x.^2).*cos(x)+exp(-1./x.^2).*cos(1./x).*(1./x.^2);
disp('Step approximation')
while err>=10^(-4)/2
x0=0;x1=1; h=(x1-x0)/N;
s=1*h/2;
for i=1:N-1
s=s+h*f(x0+h*i);
end
s=s+f(x1)*h/2;
err=2*abs(s1-s)/(2^4-1);
s1=s;
disp([N, 2*s])
N=N*2;
end

Step approximation

2.000000000000000 1.351740116024899

4.000000000000000 1.374766532917430
8.000000000000000 1.379371257478457

16.000000000000000 1.380129499702764

32.000000000000000 1.380323735398339

64.000000000000000 1.380372270845669

Trapezoidal Rule:

clear; close;
format long
N=2;err=1;x0=0;x1=1;s1=0;r=1.1;
g=@(x) exp(x).*log(x);
f=@(x) r^2*exp(x.^r).*x.^(r-1).*log(x);
disp('Step approximation')
while err>=10^(-5)/2
h=(x1-x0)/N;
s=0*h/2;
for i=1:N-1
s=s+h*f(x0+h*i);
end
s=s+f(x1)*h/2;
err=abs(s-s1)/(2^2-1);
s1=s;
disp([N, s])
N=N*2;
end

r=1.1, the result is shown as follows:

Step approximation

2.000000000000000 -0.623854591742896

4.000000000000000 -0.940995768035230

8.000000000000000 -1.112592600798488

16.000000000000000 -1.207102951940195

32.000000000000000 -1.258874395487081

64.000000000000000 -1.286856326016077

1.0e+002 *

1.280000000000000 -0.013017605045802

1.0e+002 *

2.560000000000000 -0.013095928079541

1.0e+002 *

5.120000000000000 -0.013136609482607

1.0e+003 *
1.024000000000000 -0.001315753063183

1.0e+003 *

2.048000000000000 -0.001316819945001

1.0e+003 *

4.096000000000000 -0.001317360119595

1.0e+003 *

8.192000000000000 -0.001317631941802

1.0e+004 *

1.638400000000000 -0.000131776800227

1.0e+004 *

3.276800000000000 -0.000131783579334

1.0e+004 *

6.553600000000000 -0.000131786943298

1.0e+005 *

1.310720000000000 -0.000013178860660

1.0e+005 *

2.621440000000000 -0.000013178942640

r=1.5, the result is shown as follows:

Step approximation

2.000000000000000 -0.785252399689505

4.000000000000000 -1.102752633918305

8.000000000000000 -1.230956958968851

16.000000000000000 -1.282945971385789
32.000000000000000 -1.303961853848449

64.000000000000000 -1.312392276358369

1.0e+002 *

1.280000000000000 -0.013157431784960

1.0e+002 *

2.560000000000000 -0.013170629138336

1.0e+002 *

5.120000000000000 -0.013175782571961

1.0e+003 *

1.024000000000000 -0.001317777944723

1.0e+003 *

2.048000000000000 -0.001317854789939

1.0e+003 *

4.096000000000000 -0.001317884182432

1.0e+003 *

8.192000000000000 -0.001317895364322

r=2, The result is shown as follows:

Step approximation

2.000000000000000 -0.890018597344417

4.000000000000000 -1.192608285867874

8.000000000000000 -1.282681369490746

16.000000000000000 -1.308174900682751

32.000000000000000 -1.315243410237071
64.000000000000000 -1.317180972500353

1.0e+002 *

1.280000000000000 -0.013177077490157

1.0e+002 *

2.560000000000000 -0.013178500249506

1.0e+002 *

5.120000000000000 -0.013178882384224

1.0e+003 *

1.024000000000000 -0.001317898452849

r=3, the result is shown as follows:

Step approximation

2.000000000000000 -0.883618487324026

4.000000000000000 -1.195009443541396

8.000000000000000 -1.286108348460041

16.000000000000000 -1.309909614973187

32.000000000000000 -1.315905252904677

64.000000000000000 -1.317403523621169

1.0e+002 *

1.280000000000000 -0.013177775970825

1.0e+002 *

2.560000000000000 -0.013178710274392

1.0e+002 *

5.120000000000000 -0.013178943723826

1.0e+003 *
1.024000000000000 -0.001317900206935

r=10, the result is shown as follows:

Step approximation

2.000000000000000 -0.067756290305606

4.000000000000000 -0.605305883381251

8.000000000000000 -1.043582590843233

16.000000000000000 -1.235910892754504

32.000000000000000 -1.296223780878548

64.000000000000000 -1.312400159150013

1.0e+002 *

1.280000000000000 -0.013165213457023

1.0e+002 *

2.560000000000000 -0.013175566157121

1.0e+002 *

5.120000000000000 -0.013178157465841

1.0e+003 *

1.024000000000000 -0.001317880548928

1.0e+003 *

2.048000000000000 -0.001317896750741

1.0e+003 *

4.096000000000000 -0.001317900801271

Simpson Rule:

close; clear;
N=2;r=1.1;x0=0;x1=1; S1=0;err=1;
g=@(x) exp(x).*log(x);
f=@(x) r^2*exp(x.^r).*x.^(r-1).*log(x);
disp('Step approximation')
while err>=(1e-5)/2
h=(x1-x0)/2/N;
S=0;
for i=1:2*N
if (floor(i/2)-i/2==0) a=2;
else a=4;
end
S=S+a*f(x0+i*h);
end
S=S-f(x1);
S=h/3*S;
err=abs(S1-S)/15;
S1=S;
disp([N, S])
N=2*N;
end

r=1.1

Step approximation

2.000000000000000 -1.046709493466008

4.000000000000000 -1.169791545052907

8.000000000000000 -1.238606402320764

16.000000000000000 -1.276131543336043

32.000000000000000 -1.296183636192408

64.000000000000000 -1.306728564101631

1.0e+002 *

1.280000000000000 -0.013122035757454

1.0e+002 *

2.560000000000000 -0.013150169950296

1.0e+002 *
5.120000000000000 -0.013164504348236

1.0e+003 *

1.024000000000000 -0.001317175572273

1.0e+003 *

2.048000000000000 -0.001317540177793

1.0e+003 *

4.096000000000000 -0.001317722549204

1.0e+003 *

8.192000000000000 -0.001317813355760

1.0e+004 *

1.638400000000000 -0.000131785839036

r=1.5

Step approximation

2.000000000000000 -1.208586045327905

4.000000000000000 -1.273691733985700

8.000000000000000 -1.300275642191435

16.000000000000000 -1.310967148002669

32.000000000000000 -1.315202417195009

64.000000000000000 -1.316860145875236

1.0e+002 *

1.280000000000000 -0.013175028256128

1.0e+002 *

2.560000000000000 -0.013177500383170

1.0e+002 *
5.120000000000000 -0.013178445072319

1.0e+003 *

1.024000000000000 -0.001317880405011

r=2

Step approximation

2.000000000000000 -1.293471515375693

4.000000000000000 -1.312705730698371

8.000000000000000 -1.316672744413419

16.000000000000000 -1.317599580088510

32.000000000000000 -1.317826826588113

64.000000000000000 -1.317883341187503

r=3

Step approximation

2.000000000000000 -1.298806428947186

4.000000000000000 -1.316474650099589

8.000000000000000 -1.317843370477569

16.000000000000000 -1.317903798881840

r=10

Step approximation

2.000000000000000 -0.784489081073132

4.000000000000000 -1.189674826663894

8.000000000000000 -1.300020326724927

16.000000000000000 -1.316328076919896

32.000000000000000 -1.317792285240502
64.000000000000000 -1.317895074553085

1.0e+002 *

1.280000000000000 -0.013179017057153

6. Solution.

b)
clear; close;
format long
N=2;err=1;
x0=0;x1=2/pi;s1=100;
f=@(x) x.^2.*sin(1./x);
disp('Step approximation')
while err>=10^(-3)/2
h=(x1-x0)/N;
s=0*h/2;
for i=1:N-1
s=s+h*f(x0+h*i);
end
s=s+f(x1)*h/2;
err=abs(s1-s);
s1=s;
disp([N, s])
N=N*2;
end

Result:

Step approximation

2.000000000000000 0.064503068866399

4.000000000000000 0.063673513579651

8.000000000000000 0.059387564754992

16.000000000000000 0.058902335031313

c)
clear; close;
format long
N=2;err=1;
x0=0;x1=1; s1=111;
f=@(x) 3*cos(x.^3);
disp('Step approximation')
while err>=10^(-8)/2
h=(x1-x0)/N;
s=f(x0)*h/2;
for i=1:N-1
s=s+h*f(x0+h*i);
end
s=s+f(x1)*h/2;
err=abs(s1-s)/3;
s1=s;
disp([N, s])
N=N*2;
end

Step approximation

2.000000000000000 2.643523230245098

4.000000000000000 2.755912152904892

8.000000000000000 2.785264907428469

16.000000000000000 2.792648819817276

32.000000000000000 2.794497055948642

64.000000000000000 2.794959246696237

1.0e+002 *

1.280000000000000 0.027950748024685

1.0e+002 *

2.560000000000000 0.027951036919146

1.0e+002 *

5.120000000000000 0.027951109143075

1.0e+003 *

1.024000000000000 0.002795112719908

1.0e+003 *

2.048000000000000 0.002795113171308

1.0e+003 *

4.096000000000000 0.002795113284158

1.0e+003 *

8.192000000000000 0.002795113312370
1.0e+004 *

1.638400000000000 0.000279511331942

6. Solution.
First we plot the graph of f, p_1(x), p_3(x) and p_5(x).

close;clear;
x0=-1;x1=1; x=x0:0.001:x1;
f=@(x) sin(pi/2*x);
p5= @(x) pi* x/2 - (pi^3* x.^3)/48 + (pi^5 *x.^5)/3840;
p3=@(x) pi* x/2 - (pi^3* x.^3)/48;
p1=@(x) pi* x/2;
plot(x,p5(x),'--')
hold on
plot(x,p3(x),'-.')
hold on
plot(x,p1(x),':')
hold on
plot(x,f(x))
legend('n=5','n=3','n=1','analytic')

The result is shown on the first graph.

And also we plot the error of p_1(x), p_3(x) and p_5(x) with respect to f.

close;clear;
x0=-1;x1=1; x=x0:0.001:x1;
f=@(x) sin(pi/2*x);
p5= @(x) pi* x/2 - (pi^3* x.^3)/48 + (pi^5 *x.^5)/3840;
p3=@(x) pi* x/2 - (pi^3* x.^3)/48;
p1=@(x) pi* x/2;
plot(x,abs(f(x)-p5(x)),'--')
hold on
plot(x,abs(f(x)-p3(x)),'-.')
hold on
plot(x,abs(f(x)-p1(x)))
legend('n=5','n=3','n=1');

The result is shown on the second graph.

Das könnte Ihnen auch gefallen