Error using + Matrix dimensions must agree.
3 views (last 30 days)
Show older comments
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 Comments
Answers (1)
per isakson
on 15 Dec 2018
Edited: per isakson
on 15 Dec 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
See Also
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!