数值分析第七章上机题
[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');
hist = 0 -0.428571428571429 -0.000000000000101 0 1.557407724654902 1.000000000000127 -0.141348392468100 1.087738055836075 -0.002875590925150 1.001269946612821
数理强化班
数值分析第七章计算机实习题
写一程序实现下面问题的牛顿算法——求解方程组:
3 ( x1 3)( x2 7) 18, x1 sin( x e 1) 0. 2
源程序如下:
function [x,it,hist] = newton2(x0,f,g,maxit,tol) % Newton method for eqation systerm % INPUTS: % x0 % f % g % maxit % tol % OUTPUTS: % x % it % hist format long; if nargin<5, if nargin<4, if nargin<3, end end end flag = 1; x0 = [0;0]; x = x0; hist = x; it = 0; 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; for k = 1:maxit, tol = 1e-7; maxit = 100; error('too few input!!'); solution iteration history of iteration initial point function gradient maximum iteration tolerance for convergence
0.000000056935424 1.000000431005363
由以上运行结果可知: 该方程组采用牛顿迭代法迭代 5 步可到足够精度,解为 x 1 .
0
>> [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