在matlab中,运用共轭梯度求二次函数极小点,程序老是会出现以下状况,请大仙帮我看看,哪边错了?syms x1 x2 kf=(3/2)*(x1^2)+(1/2)*(x2^2)-x1*x2-2*x1;A=[3 -1;-1 1];x_1=[-2;4];fx1=diff(f,x1);fx2=diff(f,x2);fx1_1=subs(fx1,{x1,
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/06 03:37:21
在matlab中,运用共轭梯度求二次函数极小点,程序老是会出现以下状况,请大仙帮我看看,哪边错了?syms x1 x2 kf=(3/2)*(x1^2)+(1/2)*(x2^2)-x1*x2-2*x1;A=[3 -1;-1 1];x_1=[-2;4];fx1=diff(f,x1);fx2=diff(f,x2);fx1_1=subs(fx1,{x1,
在matlab中,运用共轭梯度求二次函数极小点,程序老是会出现以下状况,请大仙帮我看看,哪边错了?
syms x1 x2 k
f=(3/2)*(x1^2)+(1/2)*(x2^2)-x1*x2-2*x1;
A=[3 -1;-1 1];
x_1=[-2;4];
fx1=diff(f,x1);
fx2=diff(f,x2);
fx1_1=subs(fx1,{x1,x2},{-2,4});
fx2_1=subs(fx2,{x1,x2},{-2,4});
r_1=[fx1_1;fx2_1];
p_1=-r_1;
t_1=-((r_1)'*p_1)/((A*p_1)'*p_1);
x_2=x_1+t_1.*p_1;
fx1_2=subs(fx1,{x1,x2},(x_2)');
fx2_2=subs(fx2,{x1,x2},(x_2)');
r_{2}=[fx1_2;fx2_2];
M=r_{2}
for k=2:10
while norm(r_{k})>=0.01
a_{k-1}=((r_{k})'*r_{k})/((r_{k-1})'*r_{k});
p_{k}=-(r_{k})+(a_{k-1}).*(p_{k-1});
t_{k}=-((r_{k})'*p_{k})/((A*p_{k})'*(p_{k}));
x_{k+1}=x_{k}+((t_{k}).*(p_{k}));
fx1_{k+1}=subs(fx1,{x1,x2},(x_{k+1})');
fx2_{k+1}=subs(fx2,{x1,x2},(x_{k+1})');
r_{k+1}=[fx1_{k+1};fx2_{k+1}];
k=k+1;
end
end
optx=(x_{k})
M =
0.3529
0.7059
Error using ==> mtimes
Inner matrix dimensions must agree.
在matlab中,运用共轭梯度求二次函数极小点,程序老是会出现以下状况,请大仙帮我看看,哪边错了?syms x1 x2 kf=(3/2)*(x1^2)+(1/2)*(x2^2)-x1*x2-2*x1;A=[3 -1;-1 1];x_1=[-2;4];fx1=diff(f,x1);fx2=diff(f,x2);fx1_1=subs(fx1,{x1,
你的这个 执行结果是生成二维元胞数组 r_{2}=[fx1_2;fx2_2];
我猜你用这个的本意是由可做下标的r_{k},其实循环中不用这样做做下标,只要找个中间变量循环更新一次变量值就可以了 ,帮你改好了,自己编程序要学会调试找错哦
syms x1 x2 k
f=(3/2)*(x1^2)+(1/2)*(x2^2)-x1*x2-2*x1;
A=[3 -1;-1 1];
fx1=diff(f,x1);
fx2=diff(f,x2);
fx11=subs(fx1,{x1,x2}, {-2,4});
fx21=subs(fx2,{x1,x2},{-2,4});
x_1=[-2;4];
r1=[fx11;fx21];
p1=-r1;
t1=-((r1)'*p1)/((A*p1)'*p1);
x_2=x_1+t1.*p1;
xx={x_2(1),x_2(2)};
fx12=subs(fx1,{x1,x2}, xx);
fx22=subs(fx2,{x1,x2},xx);
r2=[fx12;fx22];
%M=r_{2};
for k=2:10
while norm(r2)>=0.01
a1=((r2)'*r2)/((r1)'*r1);
p2=-(r2)+(a1).*(p1);
t2=-((r2)'*p2)/((A*p2)'*(p2));
x_3=x_2+((t2).*(p2));
xx={x_2(1),x_2(2)};
fx12=subs(fx1,{x1,x2},xx);
fx22=subs(fx2,{x1,x2},xx);
r2=[fx12;fx22];
x_2=x_3;
end
end
optx=(x_2)