1、数值计算方法实验指导书数 值 计 算 方 法实 验 指 导 书陕西科技大学信息与计算科学教研室实验0 Matlab基础实验专业班级 学号 姓名 成绩 .实验类型:验证性实验 综合性实验 设计性实验实验目的:熟悉MATLAB系统的启动、退出、演示系统、帮助系统、 MATLAB的运行环境,掌握Matlab矩阵及其运算基础知识实验内容: 矩阵的建立、矩阵的拆分、特殊矩阵、矩阵求逆,求行列式的值,矩阵算符及其表达式表示和计算、常用数学函数、画图函数应用实验报告:根据实验情况和结果撰写并递交实验报告。实验1 代数插值实验(一)实验类型:验证性实验 综合性实验 设计性实验实验目的:进一步熟练掌握Lagra
2、nge插值算法,提高编程能力和解决插值问题的实践技能。实验内容:依照Lagrange算法编写基于N+!个点的 的N次Lagrange插值多项式系数计算的程序实验原理已知N+!个点的 计算 Lagrange插值基函数 (k=0,N)计算 N次Lagrange插值多项式函数 P(x)=实验说明 需要建立两个数组X,Y分别存放(,(,需要考虑多项式乘积运算的算法或者函数。实验步骤1 要求上机实验前先编写出程序代码 2 编辑录入程序3 调试程序并记录调试过程中出现的问题及修改程序的过程4 经反复调试后,运行程序并验证程序运行是否正确。5 记录运行时的输入和输出。 实验总结 实验报告:根据实验情况和结果
3、撰写并递交实验报告。参考程序(MATLAB程序)function C,L=lagran(X,Y)%Input - X is a vector that contains a list of abscissas% - Y is a vector that contains a list of ordinates%Output - C is a matrix that contains the coefficents of% the Lagrange interpolatory polynomial% - L is a matrix that contains the Lagrange% coef
4、ficient polynomials% NUMERICAL METHODS: Matlab Programs% (c) 2004 by John H. Mathews and Kurtis D. Fink% Complementary Software to accompany the textbook:% NUMERICAL METHODS: Using Matlab, Fourth Edition% ISBN: 0-13-065248-2% Prentice-Hall Pub. Inc.% One Lake Street% Upper Saddle River, NJ 07458w=le
5、ngth(X);n=w-1;L=zeros(w,w);%Form the Lagrange coefficient polynomialsfor k=1:n+1 V=1; for j=1:n+1 if k=j V=conv(V,poly(X(j)/(X(k)-X(j); end end L(k,:)=V;end%Determine the coefficients of the Lagrange interpolator%polynomialC=Y*L;实验2 代数插值实验(二)实验类型:验证性实验 综合性实验 设计性实验实验目的:进一步熟练掌握spline(样条)插值算法,提高编程能力和解决
6、插值问题的实践技能。实验内容:编写程序用计算机求解三次压紧样条曲线,经过点(0,0.0),(1,0.5),(2,2.0)和(3,1.5),而且一阶导数边界条件S(0)=0.2和S(3)=-1实验原理 若已知N+!个点的及其一阶导数的边界条件S(a)=和S(b)=,则存在唯一的三次样条曲线。求解下列线性方程组 ; ; ; 实验说明 需要建立两个数组X,Y分别存放(,(,建立两个变量dxo、 dxn分别存放左右端点的一阶导数 dxo = S(x0) , dxn = S(xn)实验步骤1 要求上机实验前先编写出程序代码 2 编辑录入程序3 调试程序并记录调试过程中出现的问题及修改程序的过程4 经反复
7、调试后,运行程序并验证程序运行是否正确。5 记录运行时的输入和输出。 实验总结实验报告: 根据实验情况和结果撰写并递交实验报告。参考程序(MATLAB程序)function S=csfit(X,Y,dx0,dxn)%Input - X is the 1xn abscissa vector% - Y is the 1xn ordinate vector% - dxo = S(x0) first derivative boundary condition% - dxn = S(xn) first derivative boundary condition%Output - S: rows of S
8、 are the coefficients for the cubic interpolants% NUMERICAL METHODS: Matlab Programs% (c) 2004 by John H. Mathews and Kurtis D. Fink% Complementary Software to accompany the textbook:% NUMERICAL METHODS: Using Matlab, Fourth Edition% ISBN: 0-13-065248-2% Prentice-Hall Pub. Inc.% One Lake Street% Upp
9、er Saddle River, NJ 07458N=length(X)-1;H=diff(X);D=diff(Y)./H;A=H(2:N-1);B=2*(H(1:N-1)+H(2:N);C=H(2:N);C=H(2:N);U=6*diff(D);%Clamped spline endpoint constraintsB(1)=B(1)-H(1)/2;U(1)=U(1)-3*(D(1)-dx0);B(N-1)=B(N-1)-H(N)/2;U(N-1)=U(N-1)-3*(dxn-D(N);for k=2:N-1 temp=A(k-1)/B(k-1); B(k)=B(k)-temp*C(k-1)
10、; U(k)=U(k)-temp*U(k-1);endM(N)=U(N-1)/B(N-1);for k=N-2:-1:1 M(k+1)=(U(k)-C(k)*M(k+2)/B(k);end%Clamped spline endpoint constraintsM(1)=3*(D(1)-dx0)/H(1)-M(2)/2;M(N+1)=3*(dxn-D(N)/H(N)-M(N)/2;for k=0:N-1 S(k+1,1)=(M(k+2)-M(k+1)/(6*H(k+1); S(k+1,2)=M(k+1)/2; S(k+1,3)=D(k+1)-H(k+1)*(2*M(k+1)+M(k+2)/6;
11、S(k+1,4)=Y(k+1);end实验3 数据拟合实验实验类型:验证性实验 综合性实验 设计性实验实验目的: 进一步熟练掌握最小二乘多项式拟合算法,提高编程能力和解决拟合问题的实践技能。实验内容:对下列数据,求解最小二乘抛物线 -3-11315515实验原理: 求使得偏差平方和最小的多项式实验说明:要求输入拟合点,输出拟合多项式的系数实验步骤1 要求上机实验前先编写出程序代码 2 编辑录入程序3 调试程序并记录调试过程中出现的问题及修改程序的过程4 经反复调试后,运行程序并验证程序运行是否正确。5 记录运行时的输入和输出。 实验总结实验报告:根据实验情况和结果撰写并递交实验报告。参考程序f
12、unction C = lspoly(X,Y,M)%Input - X is the 1xn abscissa vector% - Y is the 1xn ordinate vector% - M is the degree of the least-squares polynomial% Output - C is the coefficient list for the polynomial% NUMERICAL METHODS: Matlab Programs% (c) 2004 by John H. Mathews and Kurtis D. Fink% Complementary
13、Software to accompany the textbook:% NUMERICAL METHODS: Using Matlab, Fourth Edition% ISBN: 0-13-065248-2% Prentice-Hall Pub. Inc.% One Lake Street% Upper Saddle River, NJ 07458n=length(X);B=zeros(1:M+1);F=zeros(n,M+1);%Fill the columns of F with the powers of Xfor k=1:M+1 F(:,k)=X.(k-1);end%Solve t
14、he linear system from (25)A=F*F;B=F*Y;C=AB;C=flipud(C);实验4 数值积分实验实验类型:验证性实验 综合性实验 设计性实验实验目的:进一步熟练掌握变步长数值积分算法,提高编程能力和解决定积分问题的实践技能。实验内容:用龙贝格积分算法计算实验原理实验步骤1 要求上机实验前先编写出程序代码 2 编辑录入程序3 调试程序并记录调试过程中出现的问题及修改程序的过程4 经反复调试后,运行程序并验证程序运行是否正确。5 记录运行时的输入和输出。 实验总结实验报告:根据实验情况和结果撰写并递交实验报告。参考程序function R,quad,err,h=r
15、omber(f,a,b,n,tol)%Input - f is the integrand % - a and b are upper and lower limits of integration% - n is the maximum number of rows in the table% - tol is the tolerance%Output - R is the Romberg table% - quad is the quadrature value% - err is the error estimate% - h is the smallest step size used
16、%If f is defined as an M-file function use the notation% call R,quad,err,h=romber(f,a,b,n,tol).%If f is defined as an anonymous function use the% call R,quad,err,h=romber(f,a,b,n,tol).% NUMERICAL METHODS: Matlab Programs% (c) 2004 by John H. Mathews and Kurtis D. Fink% Complementary Software to acco
17、mpany the textbook:% NUMERICAL METHODS: Using Matlab, Fourth Edition% ISBN: 0-13-065248-2% Prentice-Hall Pub. Inc.% One Lake Street% Upper Saddle River, NJ 07458M=1;h=b-a;err=1;J=0;R=zeros(4,4);R(1,1)=h*(f(a)+f(b)/2;while(errtol)&(Jn)|(J4) J=J+1; h=h/2; s=0; for p=1:M x=a+h*(2*p-1); s=s+f(x); end R(
18、J+1,1)=R(J,1)/2+h*s; M=2*M; for K=1:J R(J+1,K+1)=R(J+1,K)+(R(J+1,K)-R(J,K)/(4K-1); end err=abs(R(J,J)-R(J+1,K+1);endquad=R(J+1,J+1);实验5 解非线性方程实验实验类型:验证性实验 综合性实验 设计性实验实验目的:进一步熟练掌握解非线性方程牛顿迭代算法,提高编程能力和解算非线性方程问题的实践技能。实验内容:用牛顿迭代法求平方根,计算实验原理 令实验步骤1 要求上机实验前先编写出程序代码 2 编辑录入程序3 调试程序并记录调试过程中出现的问题及修改程序的过程4 经反复调
19、试后,运行程序并验证程序运行是否正确。5 记录运行时的输入和输出。 实验总结实验报告:根据实验情况和结果撰写并递交实验报告。参考程序function p0,err,k,y=newton(f,df,p0,delta,epsilon,max1)%Input - f is the object function % - df is the derivative of f % - p0 is the initial approximation to a zero of f% - delta is the tolerance for p0% - epsilon is the tolerance for
20、the function values y% - max1 is the maximum number of iterations%Output - p0 is the Newton-Raphson approximation to the zero% - err is the error estimate for p0% - k is the number of iterations% - y is the function value f(p0)%If f and df are defined as M-file functions use the notation% call p0,er
21、r,k,y=newton(f,df,p0,delta,epsilon,max1).%If f and df are defined as anonymous functions use the% call p0,err,k,y=newton(f,df,p0,delta,epsilon,max1).% NUMERICAL METHODS: Matlab Programs% (c) 2004 by John H. Mathews and Kurtis D. Fink% Complementary Software to accompany the textbook:% NUMERICAL METH
22、ODS: Using Matlab, Fourth Edition% ISBN: 0-13-065248-2% Prentice-Hall Pub. Inc.% One Lake Street% Upper Saddle River, NJ 07458for k=1:max1 p1=p0-f(p0)/df(p0); err=abs(p1-p0); relerr=2*err/(abs(p1)+delta); p0=p1; y=f(p0); if (errdelta)|(relerrdelta)|(abs(y)epsilon),break,endend实验6 解线性方程组实验实验类型:验证性实验
23、综合性实验 设计性实验实验目的:进一步熟练掌握解线性方程组高斯列主元算法,提高编程能力和解算线性方程组问题的实践技能。实验内容:用高斯列主元算法解线性方程组实验原理:高斯列主元算法实验步骤1 要求上机实验前先编写出程序代码 2 编辑录入程序3 调试程序并记录调试过程中出现的问题及修改程序的过程4 经反复调试后,运行程序并验证程序运行是否正确。5 记录运行时的输入和输出。 实验总结实验报告:根据实验情况和结果撰写并递交实验报告。参考程序function X = uptrbk(A,B)%Input - A is an N x N nonsingular matrix% - B is an N x
24、1 matrix%Output - X is an N x 1 matrix containing the solution to AX=B.% NUMERICAL METHODS: Matlab Programs% (c) 2004 by John H. Mathews and Kurtis D. Fink% Complementary Software to accompany the textbook:% NUMERICAL METHODS: Using Matlab, Fourth Edition% ISBN: 0-13-065248-2% Prentice-Hall Pub. Inc
25、.% One Lake Street% Upper Saddle River, NJ 07458%Initialize X and the temporary storage matrix C N N=size(A);X=zeros(N,1);C=zeros(1,N+1);%Form the augmented matrix: Aug=A|BAug=A B;for p=1:N-1 %Partial pivoting for column p Y,j=max(abs(Aug(p:N,p); %Interchange row p and j C=Aug(p,:); Aug(p,:)=Aug(j+p
26、-1,:); Aug(j+p-1,:)=C; if Aug(p,p)=0 A was singular. No unique solution break end %Elimination process for column p for k=p+1:N m=Aug(k,p)/Aug(p,p); Aug(k,p:N+1)=Aug(k,p:N+1)-m*Aug(p,p:N+1); endend%Back Substitution on U|Y using Program 3.1X=backsub(Aug(1:N,1:N),Aug(1:N,N+1);实验7解非线性方程组实验实验类型:验证性实验 综
27、合性实验 设计性实验实验目的:进一步熟练掌握解非线性方程组牛顿迭代算法,提高编程能力和解算非线性方程组问题的实践技能。实验内容:设有非线性方程组设初始值 误差小于0.001实验原理实验说明实验步骤1 要求上机实验前先编写出程序代码 2 编辑录入程序3 调试程序并记录调试过程中出现的问题及修改程序的过程4 经反复调试后,运行程序并验证程序运行是否正确。5 记录运行时的输入和输出。 实验总结实验报告:根据实验情况和结果撰写并递交实验报告。参考程序function P,iter,err=newdim(F,JF,P,delta,epsilon,max1)%Input -F is the system
28、saved as the M-file F.m% -JF is the Jacobian of F saved as the M-file JF.M% -P is the inital approximation to the solution% -delta is the tolerance for P% -epsilon is the tolerance for F(P)% -max1 is the maximum number of iterations%Output -P is the approximation to the solution% -iter is the number
29、 of iterations required% -err is the error estimate for P%Use the notation call%P,iter,err=newdim(F, JF, P, delta, epsilon, max1).% NUMERICAL METHODS: Matlab Programs% (c) 2004 by John H. Mathews and Kurtis D. Fink% Complementary Software to accompany the textbook:% NUMERICAL METHODS: Using Matlab,
30、Fourth Edition% ISBN: 0-13-065248-2% Prentice-Hall Pub. Inc.% One Lake Street% Upper Saddle River, NJ 07458Y=F(P);for k=1:max1 J=JF(P); Q=P-(JY); Z=F(Q); err=norm(Q-P); relerr=err/(norm(Q)+eps); P=Q; Y=Z; iter=k; if (errdelta)|(relerrdelta)|(abs(Y)epsilon) break endend实验8 解常微分方程组-导弹跟踪问题仿真实验实验类型:验证性实验 综合性实验