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 个评论

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.
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.
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.
x(1) x(2) and x(3) are the design variables of an optimization problem
y(1) and y(2) are two objectif functions
t1 and t2 are two angles varies between 0 and pi
my script can be written as:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [y, cons] = TP_CONSTR_objfun(x)
y = [0,0];
cons = [0,0];
t1=[0:0.1:pi];t2=[0:0.1:pi];
for t1=1:length(y(1))
for t2=1:length(y(2))
%%%%%%%%%
e=(x(1).*(sin(t1)-sin(t2))./(2.*x(3)+x(1).*cos(t2)-x(1).*cos(t1)));
f=(x(1).*x(3).*(cos(t2)+cos(t1))./(2.*x(3)+x(1).*cos(t2)-x(1).*cos(t1)));
d=(1+e.^2);
g=(2.*(e.*f-e.*x(1).*cos(t1)+e.*x(3)-x(1).*sin(t1)));
h=(f.^2-2.*f.*(x(1).*cos(t1)-x(3))-2.*x(1).*x(3).*cos(t1)+x(3).^2+x(1).^2-x(2).^2);
yp=(-g+sqrt(g.^2-4.*d.*h)./2.*d);
xp=(e.*y+f);
%objectif function
y(1) = (sqrt(x(1).^2+x(2).^2+x(3).^2)).*2.*(x(3)-x(1).*sin(t1));
y(2) =(((x(1).^2.*(yp - x(1).*sin(t2)).^2.*(sin(t1).*(x(3) + xp) - yp.*cos(t1)).^2)./(2.*(x(1).*yp.*cos(t1) - 2.*x(3).*yp - x(1).*yp.*cos(t2) + x(1).*x(3).*sin(t1) + x(1).*x(3).*sin(t2) - x(1).*xp.*sin(t1) + x(1).*xp.*sin(t2) - x(1).^2.*cos(t1).*sin(t2) + x(1).^2.*cos(t2).*sin(t1)).^2) + (x(1).^2.*(yp.*cos(t2) + sin(t2).*(x(3) - xp)).^2.*(x(3) + xp - x(1).*cos(t1)).^2)./(2.*(x(1).*yp.*cos(t1) - 2.*x(3).*yp - x(1).*yp.*cos(t2) + x(1).*x(3).*sin(t1) + x(1).*x(3).*sin(t2) - x(1).*xp.*sin(t1) + x(1).*xp.*sin(t2) - x(1).^2.*cos(t1).*sin(t2) + x(1).^2.*cos(t2).*sin(t1)).^2)).^(1./2).*((x(1).*yp.*cos(t1) - 2.*x(3).*yp - x(1).*yp.*cos(t2) + x(1).*x(3).*sin(t1) + x(1).*x(3).*sin(t2) - x(1).*xp.*sin(t1) + x(1).*xp.*sin(t2) - x(1).^2.*cos(t1).*sin(t2) + x(1).^2.*cos(t2).*sin(t1)).^2./(2.*x(1).^2.*(yp.*cos(t2) + x(3).*sin(t2) - xp.*sin(t2)).^2.*(x(3) + xp - x(1).*cos(t1)).^2) + (x(1).*yp.*cos(t1) - 2.*x(3).*yp - x(1).*yp.*cos(t2) + x(1).*x(3).*sin(t1) + x(1).*x(3).*sin(t2) - x(1).*xp.*sin(t1) + x(1).*xp.*sin(t2) - x(1).^2.*cos(t1).*sin(t2) + x(1).^2.*cos(t2).*sin(t1)).^2./(2.*x(1).^2.*(yp - x(1).*sin(t2)).^2.*(x(3).*sin(t1) - yp.*cos(t1) + xp.*sin(t1)).^2)).^(1./2));
end
end
%Constraints
c = (sqrt(x(1).^2+x(2).^2+x(3).^2))-100;
if(c<0)
cons(1) = abs(c);
end
c = 2.*(x(3)-x(1).*sin(t1))-200;
if(c<0)
cons(2) = abs(c);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The error message is:
Evaluating the objective function... Generation: 1 / 100 , Individual: 1 / 100
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in TP_CONSTR_objfun (line 24)
why don't you just use size() ?? to verify the sizes??

请先登录,再进行评论。

回答(1 个)

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 个评论

Thank you
I think the error is in the insertion of t1 and t2
even when i remove the loops i get this error:
Error using *
Inner matrix dimensions must agree.
Error in TP_CONSTR_objfun (line 24)
xp=(e*y+f);
Error in TP_CONSTR_objfun (line 24)
xp=(e*y+f);
Error in evaluate>evalIndividual (line 67)
[y, cons] = objfun( indi.var, varargin{:} );
Error in evaluate (line 45)
[pop(i), allTime(i)] = evalIndividual(pop(i), opt.objfun, varargin{:});
Error in nsga2 (line 81)
[pop, state] = evaluate(opt, pop, state, varargin{:});
Error in TP_CONSTR (line 25)
result = nsga2(options); % begin the optimization!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Polynomials 的更多信息

提问:

2018-12-10

评论:

2018-12-15

Community Treasure Hunt

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

Start Hunting!

Translated by