4.(1)T=1/2(3+1)=2
S=1/6(3+8+1)=2
计算其准确的结果为2
与精确值比较,T的误差为0
S的误差为0
7(1)复合梯形公式T2n的matlab 实现:
function I= trapezoid(fun,a,b,n)
n=2*n;
h=(b-a)/(n-1);
x=a:h:b;
f=feval(fun,x);
I=h* (0.5*f(1)+sum(f(2:n-1))+
0.5*f(n));
function trapezoid_and_sinpsom
clc;
format long syms x Iexact= int(x*exp(x^2),x,0,1);
a=0;
b=1;
for n=2:1:4
t=trapezoid(@f,a,b,n)
s=simpson(@f,a,b,n)
err1=vpa(Iexact-t,5)
err2=vpa(Iexact-s,5)
end function y=f(x)y= x*exp(x^2);
return
从而得出的结果:
n=2
t=1.000576811286697
s=0.860997139578795
err1=-0.14144
err2=-0.0018562
n=3
t=0.923798756293777
s=0.859533825596209
err1=-0.064658
err2=-0.00039291
n=4
t=0.895892057505771
s=0.859268455239111
err1=-0.036751
err2=-0.00012754
13.function
[Dc,err]=dfDc(f,x0,h)
d0=1/x0;
Dc=(f(x0+h)-f(x0-h))/(2*h);
err=Dc-d0;
return function
[Sc,err]=dfSc(f,x0,h)
d0=1/x0;
Sc=4/3*dfDc(f,x0,h/2)...
-1/3*dfDc(f,x0,h);
err=Sc-d0;
return function
[Cc,err]=dfCc(f,x0,h)
d0=1/x0;
Cc=16/15*dfSc(f,x0,h/2)...
-1/15*dfSc(f,x0,h);
err=Cc-d0;return
f=@(x)log(x);
x0=2;h=0.1;
[Dc,err]=dfDc(f,x0,h)
[Sc,err]=dfSc(f,x0,h)
[Cc,err]=dfCc(f,x0,h)
Dc=0.500417292784913
err=4.172927849132035e-04
Sc=0.499999843400513
err=
-1.565994868224507e-07
Cc=0.500000000017481
err=1.748101663423540e-11
14.
3.示位法的MATLAB实现:Function
[c,k]=fapo(f,a,b,epsilon,max1) Use false position to find the toot of function
Input:f=the function
a,b=left and right brachets of root
Epsilon=the tolerance for the value of f at the zero
Max1=the maximum number of iterations
Output:c=estimate of the root
ya=f(a);
yb=f(b);
Flag=1;k=1
While flag==1
k=k+1
C=b-yb*(b-a)/(yb-ya);
ac=c-a
yc=f(c);
If yc==0,break
Else if yb*yc>0
b=c;
yb=yc
Else
a=c;
ya=yc;
End
If abs(yc)<epsilon,break,end
End
c;
Err=abs(b-a)/2
yc=f(c)
x=fzero(`x.^3+2.*x.^2-x-2`,2 )
得到x=1
所以有根区间为[0,2]
采用示位法计算:
a=0;b=2;
f=@(x)(x.^3+2.*x.^2-x-2);
Epsilon=0.5e-2;max1=20;
[c,k]=fapo(f,a,b,epsilon,max 1)
结果为;
c=0.9994;
k=12
4.Function x=iterat(f,x0,tol,max1) Use false position to find the toot of function
Input:f=the function
a,b=left and right brachets of root
Epsilon=the tolerance for the value of f at the zero
Max1=the maximum number of iterations
Output:c=estimate of the root X(1)=x0;
For k=2:max1
X(k)=feval(f,X(k-1));
k;err=abs(X(k)-X(k-1));
x=X(k);
If(err<tol),
break
End
If k==max1
disp(`maximum number of iterations exceeded`);
End
End
X=X;
构造迭代公式:
F=@(x)(2*log(x)+5)^(1/2);
X=iterat(f,3,1e-4,20)
结果为
x=2.633810444323049 10.function[x,err,k,y]=newton(f, df,x0,epsilon,max1)flag=0;
fork=1:max1
x=x0-f(x0)/df(x0);
err=abs(x-x0);
x0=x;y=f(x0);
if(err<epsilon)║(abs(y)<epsilon)
flag=1;
break;
End
end
If flag~=1
disp(['NewtonMethodfailedtogetth ezerowithin...'num2str(max1)'ite rations'])
End
return
function[x,err,k,y]=secant(f,x0, x1,epsilon,max1)flag=0;
fork=1:max1
x=x0-f(x0)*(x1-x0)/(f(x1)-f( x0));
err=abs(x-x1);
x0=x1;
x1=x;
y=f(x0);
if(err<epsilon)║(abs(y)<epsilon)
flag=1;
break;
End
end
If flag~=1
disp(['secantMethodfailedtogetth ezerowithin...'num2str(max1)'ite rations'])
End
return
x0=2;x1=-2.1;
epsilon=1e-6;max1=100;f=@(x)(x^3 -3*x4);df=@(x)(3*x^2-3);
用牛顿法
[x,err,k,y]=newton(f,df,x0,epsil on,max1)
用割线法
[x,err,k,y]=secant(f,x0,x1,epsil on,max1)
输出结果为:x=
-2.1958
err=
3.1965e-04
k=
42
y=
-6.7313e-07
输出结果为:x=
-2.1958
err=
1.4741e-10
k=
7
y=
1.6901e-09。