Error using + Matrix dimensions must agree.
이전 댓글 표시
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
Bob Thompson
2018년 12월 10일
편집: Bob Thompson
2018년 12월 10일
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
2018년 12월 10일
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
2018년 12월 11일
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
2018년 12월 15일
편집: per isakson
2018년 12월 15일
madhan ravi
2018년 12월 15일
why don't you just use size() ?? to verify the sizes??
답변 (1개)
per isakson
2018년 12월 15일
편집: per isakson
2018년 12월 15일
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
Dikra dikra
2018년 12월 15일
편집: Dikra dikra
2018년 12월 15일
카테고리
도움말 센터 및 File Exchange에서 Polynomials에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!