求解非线性方程组之一般牛顿法
程序员文章站
2022-04-14 08:18:20
...
一、理论基础
二、 算法设计
三、算法伪代码
四、老师课件
五、matlab程序实现
例子:
function ordinary_newton
% date : 2020.10.15
% 一般牛顿法求解非线性方程组
% author : mw_1422102031
clear;
% clc;
x0 = [0.1, 0.1, -0.1]';
eps1 = 1e-8 ;
eps2 = 1e-8 ;
KMAX = 20;
x = x0;
k = 0;
fprintf('******************newton iterate history**********************\n\n') %\n : 表示换行
for k = 0:KMAX
b = F(x);
norm_b = norm(b,Inf);
if norm_b<eps1
fprintf('%d: norm_Fx = %10.8f, norm_dx = %10.8f\n',k,norm_b,norm_dx);
break
end
A = Jac(x);
dx = -A\b;
x = x+dx;
norm_dx = norm(dx,Inf);
if norm_dx < eps2
fprintf('%d: norm_Fx = %10.8f, norm_dx = %10.8f\n',k,norm_b,norm_dx);
break
end
fprintf('%d: norm_Fx = %10.8f, norm_dx = %10.8f\n',k,norm_b,norm_dx);
end
x
end
% ~~~~~~~~~~~~~~~~~~~~~subroutines(子程序)~~~~~~~~~~~~~~~~~~~~~~~
function y = F(x)
y = zeros(size(x));
x1 = x(1);
x2 = x(2);
x3 = x(3);
y(1) = 3*x1 - cos( x2*x3 ) -1/2;
y(2) = x1^2 - 81*(x2+0.1)^2 + sin(x3) +1.06;
y(3) = exp( - x1*x2 ) + 20*x3 + ( 10*pi-3 )/3;
end
function M = Jac(x)
M = zeros(3,3);
x1 = x(1);
x2 = x(2);
x3 = x(3);
M = [3 x3*sin(x2*x3) x2*sin(x2*x3);...
2*x1 -162*(x2+0.1) cos(x3);...
-x2*exp(-x1*x2) -x1*exp(-x1*x2) 20];
end