当前位置:文档之家› 数值分析第一次作业

数值分析第一次作业

数值分析第一次作业班级学号姓名习题24、用Newton法求方程f(x)=x^3-2*x^2-4*x-7=0在[3,4]中的根。

代码:function[x_star,k]=Newton1[fname,dfname,x0,ep,Nmax]if nargin<5 Nmax=500; endif nargin<4 ep=1e-5;endx=x0;x0=x+2*ep;k=0;while abs(x0-x)>ep&k<Nmax k=k+1x0=x;x=x0-feval(fname,x0)/feval(dfname,x0);endx_star=x;if k==Nmax warning(‘已迭代上限次数’);endfname=inline('x^3-2*x^2-4*x-7');dfname=inline('3*x^2-4*x-4');[x_star,k]=Newton1(fname,dfname,3.5)x_star =3.6320k =4方法二:2-4用割线法求方程的根function [x_star,k]=Gline(fun,x0,x1,ep,Nmax)if nargin<5 Nmax=500;endif nargin<4 ep=1e-5;endk=0;while abs(x1-x0)>ep&k<Nmaxk=k+1;x2=x1-feval(fun,x1)*(x1-x0)/(feval(fun,x1)-feval(fun,x0))x0=x1;x1=x2;endx_star=x1;if k==Nmax warning('已迭代上限次数');endfun=inline('x^3-2*x^2-4*x-7');[x_star,k]=Gline(fun,3,4)x2 =3.5263x2 =3.6168x2 =3.6327x2 =3.6320x2 =3.6320x_star =3.6320k =5习题33、用列主元消去法解方程组[-1 2 -2; 3 -1 4; 2 -3 -2][x1 x2 x3]=[-1 7 0] 代码:function x=Gauss_x1(A,b)A=[A’;b]’,n=length(b);for k=1:n-1s=A(k,k);p=k;for i=l+1:nif abs(s)<abs(A(i,k))s=A(I,k);p=I;endendAfor i=k+1:nm=A(i,k)/A(k,k);fprintf(‘m%d%d=%f\n’,i,k,m);for j=k:n+1A(i,j)=A(i,j)-m*A(k,j);endendfprintf(‘A%d=\n’,k+1);AendA(n,n+1)=A(n,n+1)/A(n.n);for i=n-1:-1:1s=0for j=i+1:ns=s+A(i,j)*A(j,n+1);endA(i,n+1)=(A(i,n+1)-s)/A(i,i);endA(:,n+1)A=[-1,2,-2;3,-1,4;2,-3,-2];b=[-1;7;0];x=Gauss_x1(A,b)A =3.0000 -1.00004.0000 7.00000 1.6667 -0.6667 1.33330 -2.3333 -4.6667 -4.6667A=3.0000 -1.00004.0000 7.00000 -2.3333 -4.6667 -4.66670 0 -4.0000 -2.0000x =2.00001.00000.50004、用追赶法解三对角方程[2 -1 0 0 0;-1 2 -1 0 0;0 -1 2 -1 0;0 0 -1 2 -1;0 0 0 -1 2][x1 x2 x3 x4 x5=[1 0 0 0 0 ] 代码:function x=zhuigan(A,B,C,D)n=length(B);Xzeros(1,n);U=zeros(1,n);Q=zeros(1,n);U(1)=C(1)/B(1);Q(1)=D(1)/B(1);for i=2:n-1U(i)=C(i)/(B(i)-U(i-1)*A(i-1));endfor i=2:nQ(i)=(D(i)-Q(i-1)*A(i-1))/(B(i)-U(i-1)*A(i-1));endX(n)=Q(n);for i=n-1:-1:1X(i)=Q(i)-U(i)*X(i+1);endXA=[-1,-1,-1,-1;];B=[2,2,2,2,2];C=[-1,-1,-1,-1];D=[1;0;0;0;0];X=zhuigan(A,B,C,D)X= 0.8333 0.6667 0.5000 0.3333 0.16676、用三角分解法解方程组[-2 4 8;-4 18 -16;-6 2 -20][x1 x2 x3]=[5 8 7]代码function[y,x]=LU_s(A,b)b=b';A=[A';b]',n=length(b');x=zeros(n,1);y=zeros(n,1);U=zeros(n);L=eye(n);for k=1:nU(1,k)=A(1,k);L(k,1)=A(k,1)/U(1,1);endfor i=2:nfor k=i:nlu=0;lu1=0;for j=1:i-1lu=lu+L(i,j)*U(j,k);lu1=lu1+L(k,j)*U(j,i);endU(i,k)=A(i,k)-lu;L(k,i)=(A(k,i)-lu1)/U(i,i);endendLUfor i=1:nly=0;for j=1:ily=ly+L(i,j)*y(j);endy(i)=b(i)-ly;endfor i=n:-1:1ly1=0;for j=i+1:nly1=ly1+U(i,j)*x(j);endx(i)=(y(i)-ly1)/U(i,i);endA=[-2,4,8;-4,18,-16;-6,2,-20];b=[5;8;7];[y,x]=LU_s(A,b)A =-2 4 8 5-4 18 -16 8-6 2 -20 7L =1 0 02 1 03 -1 1U =-2 4 80 10 -320 0 -76y =5-2-10x =-1.53160.22110.13163-8用LU分解法解线性方程组[5,7,9,10;6,8,10,9;7,10,8,7;5,7,6,5][x1 x2 x3 x4]=[1 1 1 1] 代码function[y,x]=LU_s(A,b)b=b';A=[A';b]',n=length(b');x=zeros(n,1);y=zeros(n,1);U=zeros(n);L=eye(n);for k=1:nU(1,k)=A(1,k);L(k,1)=A(k,1)/U(1,1);endfor i=2:nfor k=i:nlu=0;lu1=0;for j=1:i-1lu=lu+L(i,j)*U(j,k);lu1=lu1+L(k,j)*U(j,i);endU(i,k)=A(i,k)-lu;L(k,i)=(A(k,i)-lu1)/U(i,i);EndendLUfor i=1:nly=0;for j=1:ily=ly+L(i,j)*y(j);endy(i)=b(i)-ly;endfor i=n:-1:1ly1=0;for j=i+1:nly1=ly1+U(i,j)*x(j);endx(i)=(y(i)-ly1)/U(i,i);endA=[5,7,9,10;6,8,10,9;7,10,8,7;5,7,6,5];b=[1;1;1;1];[y,x]=LU_s(A,b)A =5 7 9 10 16 8 10 9 17 10 8 7 15 76 5 1L =1.0000 0 0 01.2000 1.0000 0 01.4000 -0.5000 1.0000 01.0000 0 0.6000 1.0000U =5.0000 7.0000 9.0000 10.0000 0 -0.4000 -0.8000 -3.0000 0 0 -5.0000 -8.5000 0 0 0 0.1000y =1.0000 -0.2000 -0.5000 0.3000x =20.0000 -12.0000 -5.0000 3.0000。

相关主题