实验4 解非线性方程组实验
成绩
专业班级 学号 姓名 报告日期 2012/4/17 .
实验类型:●验证性实验 ○综合性实验 ○设计性实验
实验目的:进一步熟练掌握解非线性方程组牛顿迭代算法,提高编程能力和解算非线性方程组问题的实践技能。
实验内容:
设有非线性方程组
x22xy0.50 22x4y40设初始值x0,y0(2.00,0.25) 误差小于0.001 实验原理
PnPn1J(Pn1)1F(Pn1)
实验说明 实验步骤
1 要求上机实验前先编写出程序代码 2 编辑录入程序
3 调试程序并记录调试过程中出现的问题及修改程序的过程 4 经反复调试后,运行程序并验证程序运行是否正确。 5 记录运行时的输入和输出。 实验总结
实验报告:根据实验情况和结果撰写并递交实验报告。 上机实验前程序代码预编写: 非线性方程的牛顿迭代法程序代码
function [P,iter,err]=newdim(F,JF,P,delta,epsilon,max1) %Input - F is the system saved as the M-file F.m
% - JF is the Jacobian of F saved as the M-file JF.m % - P is the initial 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 of iterations required % - err is the error estimate for P Y=feval(F,P); for k=1:max1
J=feval(JF,P); Q=P-(J\\Y); Z=feval(F,Q); err=norm(Q-P);
relerr=err/(norm(Q)+eps); P=Q; Y=Z; iter=k;
if (err向量函数录入 function Z=F(X) x=X(1);y=X(2); Z=zeros(2,1);Z(1)=x^2-2*x-y+0.5; Z(2)=x^2+4*y^2-4; 雅可比矩阵录入
function W=JF(X) x=X(1);y=X(2);
W=[2*x-2 -1;2*x 8*y];
初始值录入 P=[2 0.25]'; 调用函数求解
[P,iter,err]=newdim('F','JF',P,0.001,0.00001,10) P =
1.9007 0.3112
iter =
3 err =
1.5071e-005
实验总结:用牛顿法求非线性方程比较方便,收敛速度比较快,但求雅可比矩阵比较麻烦。