紧急求助:哪位高手帮我该下以下代码,阻尼牛顿法
用阻尼牛顿法求解 minf(x)=x1^2-2*x1*x2+1.5*x2^2+x1-2*x2
ξ=0.01 选择合适的初始点停止误差
clear;
clc
syms m n r
f=sym(m^2-2*m*n+1.5*n^2+m-2*n) %函数表达式
p=diff(f,m)
q=diff(f,n)
t=[p;q]
e=0.01 %输入停止误差
m0=input('请输入初始点(二维)第一个变量=');
n0=input('请输入初始点(二维)第二个变量=');
x0=[m0;n0]
%h=[];
%l=[];
%a=[];
flag=0;
i=0;
x=zeros(2,100);
while flag==0
i=i+1;
p=diff(f,m);
q=diff(f,n);
p=subs(p,[m,n],[m0,n0]);
q=subs(q,[m,n],[m0,n0]);
t=[p;q];
%t=subs(t,[m,n],[m0,n0])
h=[diff(f,m,2),diff(diff(f,m,1),n,1);diff(diff(f,n,1),m,1),diff(f,n,2)];%Hessian矩阵
l=inv(h);%Hessian矩阵的逆矩阵
a=-l*t ;%牛顿方向
m0=m0+r*a(1);
n0=n0+r*a(2);
f1=subs(f,[m,n],[m0,n0]);
f1=diff(f1,r);
r=solve(f1);%r为最优步长
m0=subs(m0);
n0=subs(n0);
p=subs(p,[m,n],[m0,n0]);
q=subs(q,[m,n],[m0,n0]);
s=sqrt((p)^2+(q)^2)
if s
flag=1;
end
end
哪位帮我改一下啊,谢谢!!!