当前位置:
文档之家› 数值分析考试复习题(附matlab程序实现)
数值分析考试复习题(附matlab程序实现)
0.5
0
0
-0.5
-0.5
-1
-1
-1.5
-1.5
插值节点
N1(x)= 1.8232 x - 1.6048
N1(0.54)= -0.62022
-2
0
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
1
-2
插值节点
N2(x)= -2.0412 x2 + 4.0685 x - 2.2171
%(2)GaussLegendre
y=GaussLegendre('x./(4+x.^2)',0,1,4)%函数见 GaussLegendre.m
结果:
y=
0.1116
%(3)Romberg
[y,T,err,h]=Romberg('x./(4+x.^2)',0,1,4)%函数见 Romberg.m
结果:
A(1,1)=A(1,1)+1^2; A(1,2)=A(1,2)+xdata(i)^2; A(2,2)=A(2,2)+xdata(i)^4; B(1,1)=B(1,1)+ydata(i); B(2,1)=B(2,1)+xdata(i)^2*ydata(i); end A(2,1)=A(1,2); x=A\B;%等价于 inv(A)*B delta=0; for i=1:7 delta=delta+((x(1)+x(2)*xdata(i)^2)-ydata(i))^2; end delta=sqrt(delta); plot(xdata,x(1)+x(2).*xdata.^2,xdata,ydata,'*r'); legend(['y=',poly2str([x(2) 0 x(1)],'x'),...
1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1
0 -2.5
-2 -1.5
f(x)=1-exp(-x2)根 x=5.9416e-006迭 代 次 数 k=15
-1 -0.5
0
0.5
1
1.5
2
2.5
5
模式识别_周晓勇_Z201102021
8.
已知
5xx112xx22
解:
x=[0.4 0.5 0.6 0.7 0.8]; y=[-0.916291 -0.693147 -0.510826 -0.357765 -0.223144]; x0=0:0.01:1; p1=newpoly(x,y,1,0.54);%函数见 newpoly.m y1=polyval(p1,0.54); p2=newpoly(x,y,2,0.54); y2=polyval(p2,0.54); y01=polyval(p1,x0); y02=polyval(p2,x0); subplot(1,2,1),plot(x,y,'o',x0,y01,0.54,y1,'r*'); h1=legend('插值节点',['N1(x)=',poly2str(p1,'x')],['N1(0.54)= ',num2str(y1)]); set(h1,'Location','SouthEast','FontSize',20); subplot(1,2,2),plot(x,y,'o',x0,y02,0.54,y2,'r*'); h2=legend('插值节点',['N2(x)=',poly2str(p2,'x')],['N2(0.54)= ',num2str(y2)]); set(h2,'Location','SouthEast','FontSize',20); 结果:
2.6
[x,k,flag]=SOR(A,b,1e-5,w(i),100);%函数见 SOR.m result(:,i)=[w(i);flag;k;x]; end [C I]=min(result(3,:)); plot(w,result(2,:).*result(3,:),w(I),C,'r*');xlabel('w');ylabel('k'); legend('k-w 曲线',['k=',num2str(C), ',\omega=',num2str(w(I))]); title(['方程组解为:',num2str(result(4:end,I)','%10.4f')]); 结果:
-2
-1.5
-1
-0.5
f(x)=x3+2*x-5 根 x=1.3283迭 代 次 数 k=4
0
0.5
1
1.5
2
2.5
3. 当 X= -1, 1, 2 时 f(x)分别为 -3,0 ,4 利用基函数法,求 f(x)的二次插值多项式 解:
x=[-1 1 2]; y=[-3 0 4]; p=polyfit(x,y,2); f=poly2str(p,'x'); x1=-5:0.01:5; y1=polyval(p,x1); plot(x1,y1,x,y,'o'); legend(['f(x)=',f],'节点'); 结果:
y=
0.1116
T=
0.1000
0
0
0
0.1088 0.1118
0
0
0.1109 0.1116 0.1116
0
0.1114 0.1116 0.1116 0.1116
err = 2.1240e-006
h=
0.1250
7. 利用牛顿弦切法 求非线性方程 y 1 ex 在 0.2 附近的根
解: [x,err,k,y]=NewtonDownhill('1-exp(-x^2)',0.2,10^(-5),30)%函数见 NewtonDownhill.m 结果:
ans =
x=
k= flag =
-5.8991 6.9633 3.5688 -5.8991 6.9633 3.5688 66 1
平时作业
1. 已知函数 f (x) 值及差商表,求
(1) N4 (x) 来近似 f (0.596)
(2) N2 (x) 和 N3 (x) 来近似 f (0.596) ,并比较(1)和(2)的误差
模式识别_周晓勇_Z201102021
期末作业
2x y 4z 6 1. 给定一个方程组 x 4 y z 3 试建立一个收敛的迭代格式,并说明收敛的理由
3x y z 2
解: A=[2 1 4;1 4 1;3 1 1]; b=[6 3 2]'; %对 A|b 进行行变换,使 A 为严格对角占优阵 %如果 A 为严格对角占优阵,则解 Ax=b 的 Jacobi 迭代法和 Gauss-Seidel 迭代法均收敛 A=[3 1 1;1 4 1;2 1 4]; b=[2 3 6]'; A\b%正确答案 w=[1:20]*0.1; for i=1:20
解: %% 期末作业 No.5:方法 1 用函数 lsqcurvefit 实现 xdata = [-1.0 -0.5 0.0 0.5 1.0 1.5 2.0];
3
模式识别_周晓勇_Z201102021
ydata = [-4.447 -0.452 0.551 0.048 -0.447 0.549 4.552]; F = inline('x(1)+x(2).*xdata.^2','x','xdata'); x0 = [0; 0]; [x,resnorm] = lsqcurvefit(F,x0,xdata,ydata); plot(xdata,x(1)+x(2).*xdata.^2,xdata,ydata,'*r'); legend(['y=',poly2str([x(2) 0 x(1)],'x'),...
解:
%(1)NewtonCotes
[y,Ck,Ak]=NewtonCotes(@(x)(x./(4+x.^2)),0,1,4)%函数见 NewtonCotes.m
结果:
y=
0.1116
Ck = 0.0778 0.3556 0.1333 0.3556 0.0778
Ak = 0.0778 0.3556 0.1333 0.3556 0.0778
50
模式识别_周晓勇_Z201102021
0
-50
-100
-150
-200
N3(x)= -1.25 x3 + 14 x2 - 38.75 x + 26
插值节点
-250
0
1
2
3
4
5
6
7
8
9
10
3. 已知离散数据点 xi (1.8,2.0,2.2,2.4,2.6)
f (xi ) (3.12014,4.42569,6.04241,8.03014,10.46675)
x3 2x3
12 20
实现高斯-赛德尔迭代,要求精度
10-4
2x1 x2 10x3 3
解:
A=[5 2 1;-1 1 2;2 -3 10]; b=[-12;20;3]; A\b%精确解 [x,k,flag]=SOR(A,b,1e-4,1,100)%函数见附件 SOR.m,w=1 时即为高斯塞德尔迭代 结果: