1、数值分析实验报告2数值分析上机实验报告(2) (注:本实验报告中所有程序均为MATLAB语言程序)班级:姓名:学号:六章2、用比例求根法求在区间(1, /2)内的一个根,直到近似根满足精度| ()|0.00001 if f(a+b)/2)0 a=(a+b)/2; x=a; end if f(a+b)/2)=0 x=(a+b)/2; break endendx运行结果:x = 1.1141531685964554、比较一下两种求+10 -2=0的根到三位有效数字所需的计算量。(1)在区间(0,1)内用二分法;(2)用迭代法=(2-)/10,取初值=0。(1)程序:format longa=0;b
2、=1;R=1;k=0;f=inline(exp(x)+10*x-2);while R1e-5 c=(a+b)/2; if f(a)*f(c)0; a=c; else b=c; end R=b-a;k=k+1;endkx=c运行结果:k = 17x = 0.09052276611328首先初步估计精确值在0.09左右,故取误差限选择为10-5,得到的计算次数为17次。(2)程序:format longf=inline(2-exp(x)/10);disp(x=);x=feval(f,0);disp(x);Eps=1E-5;i=1;while 1 x0=x; i=i+1; x=feval(f,x);
3、 disp(x); if x1E10; break; end; if abs(x-x0)0 x(n+1)=x(n)-2*f(x(n)/(w(n)+sqrt(w(n)2-4*f(x(n)*p(n); end if w(n)-s(n)0 x(n+1)=x(n)-2*f(x(n)/(w(n)-sqrt(w(n)2-4*f(x(n)*p(n); endendx=x(n+1)运行结果:m=10x = 1.87938524157059613应用Newton法于方程,导出求的迭代公式,并求出的值程序:clearm=input(m=);x(1)=11;f=inline(1-115/x2);f1=inline(
4、2*115*x(-3);for n=1:m x(n+1)=x(n)-f(x(n)/f1(x(n);endsqrta=x(n+1)运行结果:m=10sqrta = 10.723805294763608七、八、九章求解下面线性方程的解:A是一个主对角线为4,主对角线上下各三条对角线为1的100阶的方阵,b是一个1到100的一列矩阵。利用不同种方法就解线性方程组。Lu分解程序:clearformat shortn=input(n=);a=ones(n);b=diag(diag(a,-1),-1);c=diag(diag(a,-2),-2);d=diag(diag(a,1),1);e=diag(dia
5、g(a,2),2);g=diag(diag(a,3),3);h=diag(diag(a,-3),-3);f=4*eye(n);A=b+c+d+e+f+g+h;b=1:100;L,U=lu(A);x=U(Lb);x运行结果:n=100ans = Columns 1 through 11 0.0170 0.1855 0.3226 0.4239 0.4945 0.5886 0.6964 0.8050 0.9033 0.9993 1.0978 Columns 12 through 22 1.1996 1.3008 1.4006 1.4998 1.5997 1.6999 1.8001 1.9001 2.
6、0000 2.0999 2.2000 Columns 23 through 33 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3.0000 3.1000 3.2000 3.3000 Columns 34 through 44 3.4000 3.5000 3.6000 3.7000 3.8000 3.9000 4.0000 4.1000 4.2000 4.3000 4.4000 Columns 45 through 55 4.5000 4.6000 4.7000 4.8000 4.9000 5.0000 5.1000 5.2000 5.300
7、0 5.4000 5.5000 Columns 56 through 66 5.6000 5.7000 5.8000 5.9000 6.0000 6.1000 6.2000 6.3000 6.4000 6.5000 6.6000 Columns 67 through 77 6.6999 6.8000 6.9001 7.0002 7.0999 7.1996 7.2998 7.4006 7.5009 7.5995 7.6976 Columns 78 through 88 7.7990 7.9039 8.0054 8.0964 8.1863 8.2949 8.4248 8.5292 8.5780 8
8、.6158 8.7831 Columns 89 through 99 9.0400 9.1815 8.9230 8.7585 9.1537 10.3911 10.2767 8.8545 6.4691 10.2662 14.0251 Column 100 17.3099Cholesky分解:clearformat shortn=input(n=);a=ones(n);b=diag(diag(a,-1),-1);c=diag(diag(a,-2),-2);d=diag(diag(a,1),1);e=diag(diag(a,2),2);g=diag(diag(a,3),3);h=diag(diag(
9、a,-3),-3);f=4*eye(n);A=b+c+d+e+f+g+h;b=1:100;R=chol(A);x=R(Rb);x运行结果:n=100ans = Columns 1 through 11 0.0170 0.1855 0.3226 0.4239 0.4945 0.5886 0.6964 0.8050 0.9033 0.9993 1.0978 Columns 12 through 22 1.1996 1.3008 1.4006 1.4998 1.5997 1.6999 1.8001 1.9001 2.0000 2.0999 2.2000 Columns 23 through 33 2
10、.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3.0000 3.1000 3.2000 3.3000 Columns 34 through 44 3.4000 3.5000 3.6000 3.7000 3.8000 3.9000 4.0000 4.1000 4.2000 4.3000 4.4000 Columns 45 through 55 4.5000 4.6000 4.7000 4.8000 4.9000 5.0000 5.1000 5.2000 5.3000 5.4000 5.5000 Columns 56 through 66 5.60
11、00 5.7000 5.8000 5.9000 6.0000 6.1000 6.2000 6.3000 6.4000 6.5000 6.6000 Columns 67 through 77 6.6999 6.8000 6.9001 7.0002 7.0999 7.1996 7.2998 7.4006 7.5009 7.5995 7.6976 Columns 78 through 88 7.7990 7.9039 8.0054 8.0964 8.1863 8.2949 8.4248 8.5292 8.5780 8.6158 8.7831 Columns 89 through 99 9.0400
12、9.1815 8.9230 8.7585 9.1537 10.3911 10.2767 8.8545 6.4691 10.2662 14.0251 Column 100 17.3099Jacobi方法迭代函数M文件:function y,n=jacobi(A,b,x0,eps)if nargin=3 eps=1.0e-6;elseif nargin=eps x0=y; y=B*x0+f; n=n+1;end程序:clearformat shortn=input(n=);a=ones(n);b=diag(diag(a,-1),-1);c=diag(diag(a,-2),-2);d=diag(di
13、ag(a,1),1);e=diag(diag(a,2),2);g=diag(diag(a,3),3);h=diag(diag(a,-3),-3);f=4*eye(n);A=b+c+d+e+f+g+h;b=1:n;m=diag(a);x,n=jacobi(A,b,m,1.0e-3);xn运行结果:因该方程的系数矩阵A不满足相关条件,故Jacobi方法迭代对该方程不适用Gauss-Serdel迭代方法函数M文件:function y,n=gauseidel(A,b,x0,eps)if nargin=3 eps=1.0e-6;elseif nargin=eps x0=y; y=G*x0+f; n=n
14、+1;end命令程序:clearformat longn=input(n=);a=ones(n);b=diag(diag(a,-1),-1);c=diag(diag(a,-2),-2);d=diag(diag(a,1),1);e=diag(diag(a,2),2);g=diag(diag(a,3),3);h=diag(diag(a,-3),-3);f=4*eye(n);A=b+c+d+e+f+g+h;b=1:n;m=diag(a);x,n=gauseidel(A,b,m,1.0e-10);xn运行结果:n=100ans = Columns 1 through 10 0.0170 0.1855
15、0.3226 0.4239 0.4945 0.5886 0.6964 0.8050 0.9033 0.9993 Columns 11 through 20 1.0978 1.1996 1.3008 1.4006 1.4998 1.5997 1.6999 1.8001 1.9001 2.0000 Columns 21 through 30 2.0999 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000 3.0000 Columns 31 through 40 3.1000 3.2000 3.3000 3.4000 3.5000 3.6
16、000 3.7000 3.8000 3.9000 4.0000 Columns 41 through 50 4.1000 4.2000 4.3000 4.4000 4.5000 4.6000 4.7000 4.8000 4.9000 5.0000 Columns 51 through 60 5.1000 5.2000 5.3000 5.4000 5.5000 5.6000 5.7000 5.8000 5.9000 6.0000 Columns 61 through 70 6.1000 6.2000 6.3000 6.4000 6.5000 6.6000 6.6999 6.8000 6.9001
17、 7.0002 Columns 71 through 80 7.0999 7.1996 7.2998 7.4006 7.5009 7.5995 7.6976 7.7990 7.9039 8.0054 Columns 81 through 90 8.0964 8.1863 8.2949 8.4248 8.5292 8.5780 8.6158 8.7831 9.0400 9.1815 Columns 91 through 100 8.9230 8.7585 9.1537 10.3911 10.2767 8.8545 6.4691 10.2662 14.0251 17.3099n =46幂法:建立m
18、atlab的函数文件eig_power.m如下:function V,D=eig_power(A)Maxtime=100;Eps=1E-5;n=length(A);V=ones(n,1);k=0;m0=0;while k=Maxtime v=A*V; vmax,i=max(abs(v); m=v(i); V=v/m; if abs(m-m0)Eps break; end m0=m; k=k+1;endD=m;在命令窗口中调用函数文件eig_power.m:a=linspace(4,4,100);c=linspace(1,1,99);d=linspace(1,1,98);e=linspace(1
19、,1,97);f=diag(a);g=diag(c,1);h=diag(c,-1);i=diag(d,2);j=diag(d,-2);k=diag(e,3);l=diag(e,-3);A=f+g+h+i+j+k+l;V,D=eig_power(A);VD输出结果:ans = Columns 1 through 9 0.5500 0.6800 0.8100 0.9400 0.9700 0.9900 1.0000 1.0000 1.0000 Columns 10 through 18 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1
20、.0000 Columns 19 through 27 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 Columns 28 through 36 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 Columns 37 through 45 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 Columns 46 through 54 1.0000 1.0000 1.0
21、000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 Columns 55 through 63 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 Columns 64 through 72 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 Columns 73 through 81 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 Columns 82 through 90 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 Columns 91 through 99 1.0000 1.0000 1.0000 1.0000 0.9900 0.9700 0.9400 0.8100 0.6800 Column 100 0.5500D =10其中D是最大特征值,V则是特征向量。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1