数值分析第七章上机题

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

数值分析第七章计算机实习题

写一程序实现下面问题的牛顿算法——求解方程组:

⎪⎩

⎪⎨⎧=--=-+.0)1sin(,18)7)(3(12321x e x x x

源程序如下:

function [x,it,hist] = newton2(x0,f,g,maxit,tol)

% Newton method for eqation systerm

% INPUTS:

% x0 initial point

% f function

% g gradient

% maxit maximum iteration

% tol tolerance for convergence

% OUTPUTS:

% x solution

% it iteration

% hist history of iteration

format long ;

if nargin<5, tol = 1e-7;

if nargin<4, maxit = 100;

if nargin<3, error('too few input!!');

end

end

end

flag = 1;

x0 = [0;0];

x = x0;

hist = x;

it = 0;

for k = 1:maxit,

x = x0 - feval(g,x0(1),x0(2))\feval(f,x0(1),x0(2));

if norm(x0-x)>=tol,

x0 = x;

else

fprintf('\nNewton Iteration successes!!\n')

return

end

it = it + 1;

hist = [hist x];

end

flag = 0;

fprintf('\nNewton Iteration fails!!\n');

在命令窗口输入:

>>f = inline('[(x1+3)*(x2^3-7)+18;sin(x2*exp(x1)-1)]','x1','x2');

>>g = inline

('[x2^3-7,3*x2^2*(x1+3);x2*exp(x1)*cos(x2*exp(x1)-1),exp(x1)*cos(x2*exp(x1)-1)]','x1','x2');

>> [x,it,hist] = newton2([0;0],f,g)

得到如下运行结果:

>> [x,it,hist] = newton2([0;0],f,g)

Newton Iteration successes!!

x =

-0.000000000000000

1.000000000000000

it =

5

hist =

0 -0.428571428571429 -0.141348392468100 -0.002875590925150 0.000000056935424 -0.000000000000101

0 1.557407724654902 1.087738055836075 1.001269946612821 1.000000431005363 1.000000000000127

由以上运行结果可知:

该方程组采用牛顿迭代法迭代5步可到足够精度,解为⎪⎪⎭

⎫ ⎝⎛=10x .

相关文档
最新文档