我嘗試了非線性多項式函數,並且此代碼運行良好。但對於這一個,我嘗試了幾種方法來解決使用反斜槓或bicg或lsqr的線性方程df0 * X = f0,也嘗試了幾個初始值,但結果從未收斂。NOT CONVERGE:使用Newton Raphson-Method找到非線性方程的根
% Define the given function
syms x1 x2 x3
x=[x1,x2,x3];
f(x)=[3*x1-cos(x2*x3)-1/2;x1^2+81*(x2+0.1)^2-sin(x3)+1.06;...
exp(-x1*x2)+20*x3+1/3*(10*pi-3)];
% Define the stopping criteria based on Nither or relative errors
tol=10^-5;
Niter=100;
df=jacobian(f,x);
x0=[0.1;0.1;-0.1];
% Setting starting values
error=1;
i=0;
% Start the Newton-Raphson Iteration
while(abs(error)>tol)
f0=eval(f(x0(1),x0(2),x0(3)));
df0=eval(df(x0(1),x0(2),x0(3)));
xnew=x0-df0\f0; % also tried lsqr(df0,f0),bicg(df0,f0)
error=norm(xnew-x0);
x0=xnew;
i=i+1
if i>=Niter
fprintf('Iteration times spill over Niter\n');
return;
end
end
繪製函數並查看關於開始猜測的更好選擇。 – duffymo
該函數是一個由3個方程組成的向量,如何繪圖? – Jarvis
你爲什麼在那裏使用'eval'?!?這是完全沒有必要的,並且會破壞你的計算速度,並且將你吸引到「eval」和它相關的壞習慣的深淵坑中。 – Adriaan