i'm getting this error "Unable to perform assignment because the left and right sides have a different number of elements"
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Jan
am 14 Mai 2021
Please post the complete error message, which mentions the failing line of code. It is hard to guess, where the error occurs.
Note: redefining important Matlab functions causes bugs frequently. "sum" and "error" are shadowed by variables in your code.
Antworten (1)
Stephan
am 14 Mai 2021
Bearbeitet: Stephan
am 14 Mai 2021
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 Kommentare
Siehe auch
Kategorien
Mehr zu Assumptions finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!