函数文件:
1 function x=newton_Iterative_method(f,n,Initial) 2 x0=Initial; 3 tol=1e-11; 4 x1=x0-Jacobian(f,n,x0)\F(f,x0); 5 while (norm(x1-x0,2)>tol) 6 %数值解的2范数是否在误差范围内 7 x0=x1; 8 x1=x0-Jacobian(f,n,x0)\F(f,x0); 9 end 10 x=x1;%不动点 11 function g=Jacobian(f,n,a) 12 %求解任意矩阵的雅可比矩阵 13 %% 14 syms x y; 15 F(1:n,1)=jacobian(f,x); 16 F(1:n,2)=jacobian(f,y); 17 g=vpa(subs(F,{\'x\',\'y\'},{a(1),a(2)},6)); 18 %% 19 function h=F(f,a) 20 h=vpa(subs(f,{\'x\',\'y\'},{a(1),a(2)}));
脚本文件:
tic;
clear
clc
syms x y;
h=\'[x^2+y^2-4;x^2-y^2-1]\';
initial_value=[1.6;1.2];
n=2;%方程组的未知数的个数
g=newton_Iterative_method(h,n,initial_value)
toc;
算法推导: