Error using + Matrix dimensions must agree.
Mostrar comentarios más antiguos
how can i solve this error?
Below is my script:
function [y, cons] = TP_CONSTR_objfun(x)
y = [0,0];
cons = [0,0];
theta1=[0:0.1:pi];theta2=[0:0.1:pi];
for theta1=1:length(x(1))
for theta2=1:length(x(2))
y(1) = (-sqrt(x(1).^2+x(2).^2+x(3).^2)).*2.*(x(3)+x(1).*cos(x(4)));
%%%%%
e=(x(1).*(sin(theta1)-sin(theta2))./(2.*x(3)+x(1).*cos(theta2)-x(1).*cos(theta1)));
f=x(1).*x(3).*(cos(theta2)+cos(theta1))./(2.*x(3)+x(1).*cos(theta2)-x(1).*cos(theta1));
px=e.*y+f;
d=1+e.^2;
g=2.*(e.*f-e.*x(1).*cos(theta1)+e.*x(3)-x(1).*sin(theta1));
h=f.^2-2.*f.*(x(1).*cos(theta1)-x(3))-2.*x(1).*x(3).*cos(theta1)+x(3).^2+x(1).^2-x(2).^2;
py=-g+sqrt(g.^2-4.*d.*h)./2.*d;
%%%%%%%%%%
y(2)=(((py - x(1).*sin(theta2)).^2.*(sin(theta1).*(x(3) + px) - py.*cos(theta1)).^2)./(2.*(x(1).*py.*cos(theta1) - 2.*x(3).*py - x(1).*py.*cos(theta2) + x(1).*x(3).*sin(theta1) + x(1).*x(3).*sin(theta2) - x(1).*px.*sin(theta1) + x(1).*px.*sin(theta2) - x(1).^2.*cos(theta1).*sin(theta2) + x(1).^2.*cos(theta2).*sin(theta1)).^2) + ((py - x(1).*sin(theta1)).^2.*(py.*cos(theta2) + sin(theta2).*(x(3) - px)).^2)./(2.*(x(1).*py.*cos(theta1) - 2.*x(3).*py - x(1).*py.*cos(theta2) + x(1).*x(3).*sin(theta1) + x(1).*x(3).*sin(theta2) - x(1).*px.*sin(theta1) + x(1).*px.*sin(theta2) - x(1).^2.*cos(theta1).*sin(theta2) + x(1).^2.*cos(theta2).*sin(theta1)).^2) + ((sin(theta1).*(x(3) + px) - py.*cos(theta1)).^2.*(x(3) - px + x(1).*cos(theta2)).^2)./(2.*(x(1).*py.*cos(theta1) - 2.*x(3).*py - x(1).*py.*cos(theta2) + x(1).*x(3).*sin(theta1) + x(1).*x(3).*sin(theta2) - x(1).*px.*sin(theta1) + x(1).*px.*sin(theta2) - x(1).^2.*cos(theta1).*sin(theta2) + x(1).^2.*cos(theta2).*sin(theta1)).^2) + ((py.*cos(theta2) + sin(theta2).*(x(3) - px)).^2.*(x(3) + px - x(1).*cos(theta1)).^2)./(2.*(x(1).*py.*cos(theta1) - 2.*x(3).*py - x(1).*py.*cos(theta2) + x(1).*x(3).*sin(theta1) + x(1).*x(3).*sin(theta2) - x(1).*px.*sin(theta1) + x(1).*x.*sin(theta2) - x(1).^2.*cos(theta1).*sin(theta2) + x(1).^2.*cos(theta2).*sin(theta1)).^2)).^(1./2).*((py - x(1).*sin(theta1)).^2./(2.*(x(3).*sin(theta1) - py.*cos(theta1) + px.*sin(theta1)).^2) + (py - x(1).*sin(theta2)).^2./(2.*(py.*cos(theta2) + x(3).*sin(theta2) - px.*sin(theta2)).^2) + (x(3) + px - x(1).*cos(theta1)).^2./(2.*(x(3).*sin(theta1) - py.*cos(theta1) + px.*sin(theta1)).^2) + (x(3) - px + x(1).*cos(theta2)).^2./(2.*(py.*cos(theta2) + x(3).*sin(theta2) - px.*sin(theta2)).^2)).^(1./2);
end
end
% constraint function
c = (-sqrt(x(1).^2+x(2).^2+x(3).^2))-0.5;
if(c<0)
cons(1) = abs(c);
end
c = (2.*(x(3)+x(1).*cos(x(4))))-1.5;
if(c<0)
cons(2) = abs(c);
end
Thank you
5 comentarios
Bob Thompson
el 10 de Dic. de 2018
Editada: Bob Thompson
el 10 de Dic. de 2018
Can you post the specific error message, please. It helps to know which line is causing the problem.
Also, what is the structure of x? I assume that x is an array of doubles, as it looks like you're calling specific elements for your equations, but if that is the case then your for loops will both only loop once, because the length of an element is only 1.
Walter Roberson
el 10 de Dic. de 2018
It is the y(2) assignment.
The line is too long to understand easily. You should break it down into a series of sub-sections.
KSSV
el 11 de Dic. de 2018
Look at the expression for y(2)..it is huge........split it into small chynks and add them..you have error some where there in the expression.
Dikra dikra
el 15 de Dic. de 2018
Editada: per isakson
el 15 de Dic. de 2018
madhan ravi
el 15 de Dic. de 2018
why don't you just use size() ?? to verify the sizes??
Respuestas (1)
per isakson
el 15 de Dic. de 2018
Editada: per isakson
el 15 de Dic. de 2018
Observation:
for t1=1:length(y(1))
for t2=1:length(y(2))
Why loops? The length of one element is always ONE.
I made a simple test (R2018b) and got an error in line 17 (not 24), which is the very wide expression.
>> TP_CONSTR_objfun([1,2,3])
Unable to perform assignment because the left and right sides have a different number of elements.
Error in TP_CONSTR_objfun (line 17)
y(2) =(((x(1).^2.*(yp - x(1).*sin(t2)).^2.*(sin(t1).*(x(3) + xp) - <snip>
This expression returns a vector, which cannot be assigned to one element of y, thus the error.
Set a break-point and start debugging
>> TP_CONSTR_objfun([1,2,3])
14 xp=(e.*y+f);
K>> yp
yp =
1.6829 + 2.2361i
...
...
K>> (((x(1).^2.*(yp - x(1).*sin(t2)).^2.*(sin(t1).*(x(3) + xp) - yp.*cos(t1)).^2) <snip>
K>> zzz=ans
zzz =
1.0891 - 0.1597i 1.0891 - 0.1597i
1 comentario
Dikra dikra
el 15 de Dic. de 2018
Editada: Dikra dikra
el 15 de Dic. de 2018
Categorías
Más información sobre Polynomials en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!