matlab最速下降法显示串联的数组维度不一致,该怎么改啊?
figure(2)symsx1x2detf=(x1^2+x2-11)^2+(x1+x2^2-7)^2;%f=@(x1,x2)x1^2+2*x2^2-2*x1*x2-2*x...
figure(2)
syms x1 x2 det
f=(x1^2+x2-11)^2+(x1+x2^2-7)^2;
% f=@(x1,x2) x1^2+2*x2^2-2*x1*x2-2*x2;
f_pian=gradient(f,[x1,x2])
H=hessian(f,[x1,x2])
d_x=2; n=0;out=[]; x_1=[1;1];
while d_x>0.001
x_0=x_1;
y_0=double(subs(f,[x1,x2],[x_0(1),x_0(2)]));
ezplot(x1^2+2*x2^2-2*x1*x2-4*x1-y_0,[-20, 20]) ;
% ezplot(x1^2+1*x2^2-y_0,[-20, 20])
hold on
HH=double(subs(H,[x1,x2],[x_0(1),x_0(2)]))
HH_inv=inv(HH);
d=-double(subs(f_pian,[x1,x2],[x_0(1),x_0(2)]));
d=HH_inv*d;
x_1=x_0+det*d;
ff=subs(f,[x1,x2],[x_1(1),x_1(2)]);
d_ff=diff(ff,det)
if d_ff~=0
num_det=double(solve(d_ff));
else d_ff=0;
end
out=[out;n x_0(1) x_0(2) y_0 num_det];
x_1=x_0+num_det*d;
d_x=norm(x_1-x_0);
n=n+1;
end
plot(out(:,2),out(:,3)) 展开
syms x1 x2 det
f=(x1^2+x2-11)^2+(x1+x2^2-7)^2;
% f=@(x1,x2) x1^2+2*x2^2-2*x1*x2-2*x2;
f_pian=gradient(f,[x1,x2])
H=hessian(f,[x1,x2])
d_x=2; n=0;out=[]; x_1=[1;1];
while d_x>0.001
x_0=x_1;
y_0=double(subs(f,[x1,x2],[x_0(1),x_0(2)]));
ezplot(x1^2+2*x2^2-2*x1*x2-4*x1-y_0,[-20, 20]) ;
% ezplot(x1^2+1*x2^2-y_0,[-20, 20])
hold on
HH=double(subs(H,[x1,x2],[x_0(1),x_0(2)]))
HH_inv=inv(HH);
d=-double(subs(f_pian,[x1,x2],[x_0(1),x_0(2)]));
d=HH_inv*d;
x_1=x_0+det*d;
ff=subs(f,[x1,x2],[x_1(1),x_1(2)]);
d_ff=diff(ff,det)
if d_ff~=0
num_det=double(solve(d_ff));
else d_ff=0;
end
out=[out;n x_0(1) x_0(2) y_0 num_det];
x_1=x_0+num_det*d;
d_x=norm(x_1-x_0);
n=n+1;
end
plot(out(:,2),out(:,3)) 展开
1个回答
展开全部
为什么用matlab的最速下降法求解二元一次方程时,显示串联的数组维度不一致?
检查了题主给出的代码,有几个问题的原因:
1、syms x1 x2 det 声明变量中det应尽量不用。因'det' 是软件的是内置函数。所以可以声明变量
syms x1 x2 x
2、num_det的取值问题。由于num_det=double(solve(d_ff))得到是多个解,所以执行out=[out;n x_0(1) x_0(2) y_0 num_x]这个语句就出现《串联的数组维度不一致》的错误警告。因此
使用该语句前,应增加下列一条语句,即
num_x=num_det(1);
3、用x,修改对应的det
4、修改后运行代码,可以得到如下的解
x1= -2.80513925590361
x2= 3.13135016184299
5、运行后得到的图形
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询