X(j)=x1;
j=j+1;
break
end
end
end
X
%运行结果:
%X=1.10001.10002.10001.1000
四、数值积分
分别用复化梯形公式和Romberg公式计算积分
的近似值,要求误差不超过
,并给出节点个数。
1)复化梯形公式
程序代码:
function[I,step]=Trapezoid(f,a,b,eps)
f='1/x';a=2;b=8;eps=1.0e-5;
n=1;
h=(b-a)/2;
I1=2;
I2=(subs(sym(f),findsym(sym(f)),a)+subs(sym(f),findsym(sym(f)),b))/h;
whileabs(I2-I1)>eps
n=n+1;
h=(b-a)/n;
I1=I2;
I2=0;
fori=0:
n-1
x=a+h*i;
x1=x+h;
I2=I2+(h/2)*(subs(sym(f),findsym(sym(f)),x)+...
subs(sym(f),findsym(sym(f)),x1));
end
end
I=I2
step=n
结果输出
I=
1.38654458753674
step=
53
2)Romberg公式
程序代码:
functionapprox=romberg(f,a,b,TOL)
clc;clear;
f=inline('1/x');
a=2;b=8;
TOL=1e-05;
A=zeros(20,20);
A(1,1)=(b-a)*(feval(f,a)+feval(f,b))/2;
h=(b-a)/2;
A(2,1)=A(1,1)/2+h*feval(f,a+h);
A(2,2)=(4*A(2,1)-A(1,1))/3;
errest=abs(A(2,2)-A(1,1)/2);
i=2;
while(errest>TOL)
i=i+1;
h=h/2;
sum=0.0;
forj=1:
2:
2^(i-1)-1
sum=sum+feval(f,a+j*h);
end;
A(i,1)=A(i-1,1)/2+h*sum;
forj=2:
i
power=4^(j-1);
A(i,j)=(power*A(i,j-1)-A(i-1,j-1))/(power-1);
end;
errest=abs(A(i,i)-A(i-1,i-1))/2^(i-1);
end;
if(nargout==0)
s=sprintf('\t\tapproximatevalueofintegral:
\t%.12f\n',A(i,i));
s=sprintf('%s\t\terrorestimate:
\t\t\t\t\t%.4e\n',s,errest);
s=sprintf('%s\t\tnumberoffunctionevaluations:
\t%d\n',s,2^(i-1)+1);
disp(s)
else
approx=A(i,i);
end
运行结果:
approximatevalueofintegral:
1.386297441871
errorestimate:
8.7527e-006
numberoffunctionevaluations:
17
五、插值与逼近
1.给定
上的函数
,请做如下的插值逼近:
构造等距节点分别取
,
,
的Lagrange插值多项式;
构造分段线性取
的Lagrange插值多项式;
取Chebyshev多项式
的零点:
,
作插值节点构造
的插值多项式
和上述的插值多项式均要求画出曲线图形(用不同的线型或颜色表示不同的曲线)。
程序代码
functionLagrange
clc;clear;closeall;
fori=1:
3;
ifi==1
N=5;
elseifi==2
N=8;
else
N=10;
end
f=inline('1/(1+25*x^2)');
x1=zeros(1,N+1);
a=-1;
b=1;
fori=1:
N+1
x1(i)=a+(i-1)*(b-a)/N;
y1(i)=feval(f,x1(i));
end
symsx
ff=0;
fori=1:
N+1
f=1;
forj=1:
i-1
f=f*(x-x1(j))/(x1(i)-x1(j));
end
forj=i+1:
N+1
f=f*(x-x1(j))/(x1(i)-x1(j));
end
ff=f*y1(i)+ff;
f=1;
end
ff=collect(ff,x);
ff=vpa(ff,4);
y=ff;
p=ezplot(y,[a,b]);grid
YLIM([-0.10.6]);
ifN==5
set(p,'Color','black');
set(p,'LineStyle','--');
lagrange_5=y
elseifN==8
set(p,'Color','r');
set(p,'LineStyle','--');
lagrange_8=y
else
set(p,'Color','b');
set(p,'LineStyle','--')
lagrange_10=y
end
holdon;
xlabel('x');ylabel('y');
title('y=p(x)');holdon;
end
Lag_Cheb();
x=-1:
0.01:
1;
y=1./(1+25*x.^2);
acu=plot(x,y);gridon;holdon
set(acu,'Color','m');
set(acu,'LineStyle','-');
legend('N=5','N=8','N=10','Cheb,N=10','¾«È·Öµ');
functionLag_Cheb()
f=inline('1/(1+25*x^2)');
N=10;
x1=zeros(1,N+1);
a=-1;
b=1;
fori=1:
N+1
x1(i)=cos((2*i-1)*pi/(2*N));
y1(i)=feval(f,x1(i));
end
symsx
ff=0;
fori=1:
N+1
f=1;
forj=1:
i-1
f=f*(x-x1(j))/(x1(i)-x1(j));
end
forj=i+1:
N+1
f=f*(x-x1(j))/(x1(i)-x1(j));
end
ff=f*y1(i)+ff;
f=1;
end
ff=collect(ff,x);
ff=vpa(ff,4);
yy=ff;
ff=collect(ff,x);
yy=ff;
lagrange_chebshev_10=yy
cheb=ezplot(yy,[a,b]);gridon
YLIM([-0.10.6]);
set(cheb,'Color','g');
set(cheb,'LineStyle',':
');
结果输出
lagrange_5=.5673+1.202*x^4-1.731*x^2
lagrange_8=1.+53.69*x^8-102.8*x^6+61.37*x^4-13.20*x^2
lagrange_10=1.-220.9*x^10+494.9*x^8-381.4*x^6+123.4*x^4-16.86*x^2
lagrange_chebshev_10=.7413-5.359*x^10-.4e-2*x^9+18.96*x^8-.1321*x^7-
25.78*x^6+.10*x^5+16.81*x^4+.5e-2*x^3-5.336*x^2+.5288e-3*x
2.已知函数值
0
1
2
3
4
5
6
7
8
9
10
2.51
3.30
4.04
4.70
5.22
5.54
5.78
5.40
5.57
5.70
5.80
和边界条件:
. 求三次样条插值函数
并画出其图形.
程序代码:
functionSpline3_1(x,y,df0,dfn)
formatshort
x=[012345678910];
y=[2.513.304.044.705.225.545.785.405.575.705.80];
plot(x,y,'g*','MarkerSize',15);holdon;
df0=1;
dfn=0;
n=length(x);
h=zeros(n-1,1);
lan=zeros(n-2,1);
mu=zeros(n-2,1);
g=zeros(n-2,1);
m=zeros(n,1);
m
(1)=df0;
m(n)=dfn;
fori=1:
n-1
h(i)=x(i+1)-x(i);
end
fori=1:
n-2
mu(i)=h(i)/(h(i+1)+h(i));
lan(i)=h(i+1)/(h(i+1)+h(i));
g(i)=3*(mu(i)*(y(i+2)-y(i+1))/h(i+1)+lan(i)*(y(i+1)-y(i))/h(i));
end
A=zeros(n-2,n-2);
A(1,1)=2;
A(1,2)=mu
(1);
A(n-2,n-2)=lan(n-2);
fori=2:
n-2
A(i,i)=2;
A(i,i-1)=mu(i);
A(i-1,i)=lan(i);
end
g
(1)=g
(1)-lan
(1)*df0;
g(n-2)=g(n-2)-mu(n-2)*dfn;
b=A\g;
fori=2:
n-1
m(i)=b(i-1);
end
symsz
fori=1:
n-1
xx=x(i):
0.01:
x(i+1);
sx1=y(i)*(xx-x(i+1)).^2.*(h(i)+2*(xx-x(i)))/h(i)^3;
sx2=y(i+1)*(xx-x(i)).^2.*(h(i)-2*(xx-x(i+1)))/h(i)^3;
sx3=m(i)*(xx-x(i+1)).^2.*(xx-x(i))/h(i)^2;
sx4=m(i+1)*(xx-x(i)).^2.*(xx-x(i+1))/h(i)^2;
sx=sx1+sx2+sx3+sx4;
z1=y(i)*(z-x(i+1)).^2.*(h(i)+2*(z-x(i)))/h(i)^3;
z2=y(i+1)*(z-x(i)).^2.*(h(i)-2*(z-x(i+1)))/h(i)^3;
z3=m(i)*(z-x(i+1)).^2.*(z-x(i))/h(i)^2;
z4=m(i+1)*(z-x(i)).^2.*(z-x(i+1))/h(i)^2;
zz=z1+z2+z3+z4;
zz=collect(zz);
vpa(zz,4)
plot(xx,sx,'r');
holdon;
gridon;
end
程序输出
ans=2.510+.1379*z^3-.3479*z^2+z
ans=2.692-.4369e-1*z^3+.1969*z^2+.4552*z
ans=2.287+.6872e-2*z^3-.1065*z^2+1.062*z
ans=3.655-.4380e-1*z^3+.3495*z^2-.3060*z
ans=-6.080+.1083*z^3-1.476*z^2+6.995*z
ans=41.14-.2694*z^3+4.190*z^2-21.34*z
ans=-109.8+.4295*z^3-8.390*z^2+54.14*z
ans=133.0-.2784*z^3+6.475*z^2-49.91*z
ans=-57.75+.9411e-1*