# i'm getting this error "Unable to perform assignment because the left and right sides have a different number of elements"

조회 수: 1(최근 30일)
alok ranjan 2021년 5월 14일
댓글: Stephan 2021년 5월 14일
clear
syms X1 X2 X3 X4
xx = [X1, X2, X3, X4]
d = length(xx);
sum = 0;
for ii = 1:(d/4)
term1 = (xx(4*ii-3) + 10*xx(4*ii-2))^2;
term2 = 5 * (xx(4*ii-1) - xx(4*ii))^2;
term3 = (xx(4*ii-2) - 2*xx(4*ii-1))^4;
term4 = 10 * (xx(4*ii-3) - xx(4*ii))^4;
sum = sum + term1 + term2 + term3 + term4;
end
yb1 = sum
xa1(1)=1;
xa2(1)=1;
xa3(1)=1;
xa4(1)=1;
xa5(1)=1;
xa6(1)=1;
xa7(1)=1;
xa8(1)=1;
error = 10^(-4);
i = 1;
dyb1_dx1 = diff(yb1, X1);
dyb1_dx2 = diff(yb1, X2);
dyb1_dx3 = diff(yb1, X3)
dyb1_dx4 = diff(yb1, X4)
J2 = [subs(dyb1_dx1,[X1,X2,X3,X4], [xa1(1),xa2(1),xa3(1),xa4(1)]) subs(dyb1_dx2, [X1,X2,X3,X4], [xa1(1),xa2(1),xa3(1),xa4(1)]) subs(dyb1_dx3,[X1,X2,X3,X4],[xa1(1),xa2(1),xa3(1),xa4(1)]) subs(dyb1_dx4,[X1,X2,X3,X4],[xa1(1),xa2(1),xa3(1),xa4(1)] ) ] % Gradient
S2 = -(J2)
while (norm(J2))>error
I2 = [xa1(i),xa2(i),xa3(i),xa4(i)]';
syms h;
g2 = subs(yb1, [X1,X2,X3,X4], [xa1(i)+h*S2(1),xa2(i)+h*S2(2),xa3(i)+h*S2(3),xa4(i)+h*S2(4)])
dg_dh = diff(g2,h);
h = vpasolve(dg_dh==0, h);
xa1(i+1)=I2(i)+h*S2(1)
xa2(i+1)=I2(i)+h*S2(2)
xa3(i+1)=I3(i)+h*S2(3)
xa4(i+1)=I4(i)+h*S2(4)
%xa1(i+1) = I2(1)+h*S2(1);
%xa2(i+1) = I2(2)+h*S2(2);
%xa3(i+1) = I2(3)+h*S2(3);
%xa4(i+1) = I2(4)+h*S2(4);
i = i+1;
J2 = [subs(dyb1_dx1,[X1,X2,X3,X4], [xa1(i),xa2(i),xa3(i),xa4(i)]) subs(dyb1_dx2, [X1,X2,X3,X4], [xa1(i),xa2(i),xa3(i),xa4(i)]) subs(dyb1_dx3,[X1,X2,X3,X4],[xa1(i),xa2(i),xa3(i),xa4(i)]) subs(dyb1_dx4,[X1,X2,X3,X4],[xa1(i),xa2(i),xa3(i),xa4(i)])];
S2 = -(J2);
end
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
alok ranjan 2021년 5월 14일
Unable to perform assignment because the left and right sides have a different number of elements.
Error in hoja (line 143)
xa1(i+1)=I2(i)+h*S2(1)
last part of the code im getting error..

댓글을 달려면 로그인하십시오.

### 답변(1개)

Stephan 2021년 5월 14일
편집: Stephan 2021년 5월 14일
Problem 1
h returns more than one solution which produces the error because the result of
xa1(i+1)=I2(i)+h*S2(1)
with h being a vector produces a vector while xa1 is a scalar.
- to get only real solutions use:
h = vpasolve(dg_dh==0, h, [-Inf Inf]);
or if you need one of the complex solutions choose them appropriate.
Problem 2
I3 and I4 are not defined - if i use the section that is commented out instead it works, because no undefined variables are in use. So i guess change:
xa1(i+1)=I2(i)+h*S2(1)
xa2(i+1)=I2(i)+h*S2(2)
xa3(i+1)=I3(i)+h*S2(3)
xa4(i+1)=I4(i)+h*S2(4)
% xa1(i+1) = I2(1)+h*S2(1);
% xa2(i+1) = I2(2)+h*S2(2);
% xa3(i+1) = I2(3)+h*S2(3);
% xa4(i+1) = I2(4)+h*S2(4);
to:
% xa1(i+1)=I2(i)+h*S2(1)
% xa2(i+1)=I2(i)+h*S2(2)
% xa3(i+1)=I3(i)+h*S2(3)
% xa4(i+1)=I4(i)+h*S2(4)
xa1(i+1) = I2(1)+h*S2(1);
xa2(i+1) = I2(2)+h*S2(2);
xa3(i+1) = I2(3)+h*S2(3);
xa4(i+1) = I2(4)+h*S2(4);
If i do so and set the error to 10e-1 (because of calculation time) the code works for me.
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Stephan 2021년 5월 14일
Did you notice that you cann accept and/or vote for useful answers?

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by