关于共轭梯度法的程序运行问题
分别用共轭梯度法和拟牛顿法极小化奇异函数f(x)=x1+10*x2)^2+5*(x3-x4)^2+10*(x1-x4)^4,x0=(3,-1,0,1)',我用FR-CG法编的,但是迭代到第二步,就解不搜索因子了。麻烦高手帮忙看哈嘛
clc
clear
syms x1 x2 x3 x4
x=;
f=(x1+10*x2)^2+5*(x3-x4)^2+10*(x1-x4)^4;
g=;
err=0.01;
a1=3;a2=-1;a3=0;a4=1;
A=;
k=0;
for i=1:10
f0=subs(subs(subs(subs(f,x1,a1),x2,a2),x3,a3),x4,a4);
g0=simple()
d=-g0
s=eval(sqrt(g0(:,1)^2+g0(:,2)^2+g0(:,3)^2+g0(:,4)^2))
if err>s
A
break
else
syms t
A1=A+t*d;
f1=subs(f,x,A1);
df1=simple(diff(f1,t))
t1=eval(simple(solve(df1,t)))
t1=t1(1,:)
A1=
g1=
b=(g1*g1')/(g0*g0')
d1=-g1+b*d;
d=d1
A=A1
k=k+1
end
end
[ 本帖最后由 eight 于 2007-12-10 15:52 编辑 ]
页:
[1]