Unable to perform assignment because the left and right sides have a different number of elements.

9 次查看(过去 30 天)
clear
syms X1 X2 X3 X4
xx=[X1,X2,X3,X4]
X1 = xx(1);
d = length(xx);
term1 = (X1-1)^2;
sum = 0;
for ii = 2:d
xi = xx(ii);
xold = xx(ii-1);
new = ii * (2*xi^2 - xold)^2;
sum = sum + new;
end
xc1(1)=1;
xc2(1)=1;
xc3(1)=1;
xc4(1)=1;
err=10^(-2)
%yb4 = @(X1,X2,X3,X4) (term1 + sum)
yb4=(term1+sum)
dyb4_x1=diff(yb4,X1)
dyb4_x2=diff(yb4,X2)
dyb4_x3=diff(yb4,X3)
dyb4_x4=diff(yb4,X4)
J4=[subs(dyb4_x1,[X1,X2,X3,X4],[xc1(1),xc2(1),xc3(1),xc4(1)]) subs(dyb4_x2,[X1,X2,X3,X4],[xc1(1),xc2(1),xc3(1),xc4(1)]) subs(dyb4_x3,[X1,X2,X3,X4],[xc1(1),xc2(1),xc3(1),xc4(1)]) subs(dyb4_x4,[X1,X2,X3,X4],[xc1(1),xc2(1),xc3(1),xc4(1)])]
S4=-(J4)
i=1;
while norm(J4)>err
I4=[xc1(i),xc2(i),xc3(i),xc4(i)]';
syms h;
gr=subs(yb4,[X1,X2,X3,X4],[xc1(i)+h*S4(1),xc2(i)+h*S4(2),xc3(i)+h*S4(3),xc4(i)+h*S4(4)])
dgr_h=diff(gr,h)
h=vpasolve(dgr_h==0,h,[-Inf,Inf])
i=i+1
xc1(i+1)=I4(1)+h*S4(1)
xc2(i+1)=I4(2)+h*S4(2)
xc3(i+1)=I4(3)+h*S4(3)
xc4(i+1)=I4(3)+h*S4(4)
end
Error in dixon (line 38) xc1(i+1)=I4(1)+h*S4(1)

回答(2 个)

DGM
DGM 2021-5-14
The result of the vpasolve() call is a vector of multiple solutions (3). The result of operations on h with scalars is still a 3-element vector. The LHS of the assignment is a scalar. You're trying to put a vector into a scalar.
  2 个评论
DGM
DGM 2021-5-15
编辑:DGM 2021-5-15
The equation dgr_h==0 has multiple solutions. If you expect only one, you're going to have to decide which one is meaningful to you. I can't tell you that if you don't know it yourself. I don't know anything about your task at a conceptual level.
If you want to work with all solutions, you're going have to write your loop such that it can handle vectors of multiple solutions -- possibly vectors of unequal length if the number of solutions is not always 3. Since you're using xc1, etc as inputs to the solver, such an approach could get complicated if not impractical. Maybe someone else can offer a better recommendation.

请先登录,再进行评论。


Image Analyst
Image Analyst 2021-5-14
Try this (before the error line) and tell us what it says.
whos h
whos S4
whos I4
Chances are h is not a single scalar number, like it needs to be if you're going to stick it in the (i+1) element of xc1.

类别

Help CenterFile Exchange 中查找有关 Assumptions 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by