In an assignment A(I) = B, the number of elements in B and I must be the same.

1 次查看(过去 30 天)
at first i wrote below code to solve mechanism equations and find best parameters for my linkages
my first code:
l1=x(:,1);
l2=2;
l3=x(:,3);
l4=x(:,4);
l5=3;
l6=4;
theta1=0*pi/180;
alpha=acos((l3.^2+l5.^2-l6.^2)/(2*l3*l5)); % tha angle of coupler triangle
%%Gerashof principle %%
if l2+l3<=l1+l4;
A=1;
else
A=0;
end
R1=l1*exp(1i*theta1); %%length of 1 body
n=400; %%points
u=0; %%initial point for creating loop
for t2=0:2*pi/n:2*pi %%theta2 and step which it goes
u=u+1;
R2=l2*exp(1i*t2); %%length of 2 body
D=R1-R2; %%length of interact of 1 and 2 bodies
d=norm(D); %%mesure of interact body
tD=angle(D); %%angle of interact body wich called "D"
delta=abs(acos((l3.^2.+d.^2-l4.^2)/(2*l3*d))); %%angle between two bodies
sigma=abs(acos((l4.^2.+d.^2-l3.^2)/(2*l4*d))); %%angle between two bodies
theta3(u,1)=tD + delta;
theta2(u,1)=t2 %%tehta 2
P=A*(l2*exp(1i*t2)+l5*exp(1i*(alpha+theta3(u,1)))); %%length of distance between input and specific point
px(u,1)=real(P); %%real measure
py(u,1)=imag(P); %%imagine measure
But i encountered with this Error:
??? Error using ==> plus
Matrix dimensions must agree.
Error in ==> costFunction at 42
theta3(u,1)=tD + delta;
then i changed that to this code:
l1=x(:,1);
l2=2;
l3=x(:,3);
l4=x(:,4);
theta1=0*pi/180;
%%Gerashof principle %%
if l2+l3<=l1+l4;
A=1;
else
A=0;
end
R1=l1*exp(1i*theta1); %%length of 1 body
n=3; %%points
u=0; %%initial point for creating loop
for t2=0:2*pi/n:2*pi %%theta2 and step which it goes
u=u+1;
R2=l2*exp(1i*t2); %%length of 2 body
D=R1-R2; %%length of interact of 1 and 2 bodies
d=norm(D); %%mesure of interact body
t_D=angle(D) %%angle of interact body wich called "D"
Delta=abs(acos((l3.^2+d.^2-l4.^2)/(2*l3*d))) %%angle between two bodies
sigma=abs(acos((l4.^2+d.^2-l3.^2)/(2*l4*d))) %%angle between two bodies
tD=t_D
delta=Delta
for nn=1:n
theta3(nn)=tD+delta(nn)
end
theta2(u,1)=t2 %%theta 2
% theta3(u,1)=delta+tD %%theta3
P=A*(l2*exp(1i*t2)+l3*exp(1i*(theta3(u,1)))); %%length of distance between input and specific point
px(u,1)=real(P); %%real measure
py(u,1)=imag(P); %%imagine measure
if true
% code
end
now i encounter with this Error:
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> costtFunction at 41
theta3(nn)=tD+delta(nn)
i tried and read alot to solve this code but i couldn't.
would you please guide me ?
regards
  4 个评论
Image Analyst
Image Analyst 2017-1-2
x is not a function. It can't be. I don't know why you just don't say what x is.
Anyway, I made up something for x, a 30 by 4 columns array. If x has N rows then tD is an N by 1 column vector and delta is an N by N square matrix. Thus you cannot add them because they are different shapes. You need to go over the logic some more.
And you shouldn't do this:
for t2=0:2*pi/n:2*pi
You should use integers and then get the angle inside the loop. Like
theta2 = t2=0:2*pi/n:2*pi
for t2= 1 : length(theta2)
thisTheta2 = theta2(t2);
Niels
Niels 2017-1-2
thats exactly what i mean, use the command
keyboard
to check the size of td and delta and tell us
1.the size
2. if it has the size u wanted it to have

请先登录,再进行评论。

采纳的回答

Morteza.Moslehi
Morteza.Moslehi 2017-1-6
编辑:Stephen23 2017-1-6
Answer: i should write inputs same below:
cost=zeros(size(x,1),1);
for ii= 1:size(x)
l1=x(ii,1);
l2=2;
l3=x(ii,3);
l4=x(ii,4);
l5=3;
l6=4;

更多回答(2 个)

Niels
Niels 2017-1-2
编辑:Niels 2017-1-2
i tried to run it, but i could not at that moment i asked myself what your x might be
can u tell me?
if it is a vecor why would you type
l1=x(:,1);
if it is a matrix -> here is your problem
because then your R1 is also a vector, same for D and td
thats why u cant set theta3(1,1) a vector which is supposed to be a scalar
use
keyboard
to check if i am right
replace this part of your code and run it, afterwards type l1 in the cmd window
delta=abs(acos((l3.^2.+d.^2-l4.^2)/(2*l3*d))); %%angle between two bodies
sigma=abs(acos((l4.^2.+d.^2-l3.^2)/(2*l4*d))); %%angle between two bodies
keyboard
theta3(u,1)=tD + delta;
l1
  11 个评论
Walter Roberson
Walter Roberson 2017-1-3
Strictly speaking, if works the same way for vectors and scalars: it is just that the way it works tends to surprise people when it is applied to vectors or matrices.
Morteza.Moslehi
Morteza.Moslehi 2017-1-5
编辑:Morteza.Moslehi 2017-1-5
@Niels sorry for being late.I've just arrived home from Hospital. the current code is about path generation of mechanism which designed just with 4 linkages.
l2 is input and can rotate for 360 degree and in following l3 and l4 rotate too. theta 2 in the angle between l2 and l1 that l1 is gound as you can see in code.
function cost = costtFunction(x)
global LowerBand UpperBand
l1=x(:,1); l2=2; l3=x(:,3); l4=x(:,4); l5=5 l6=4 theta1=0*pi/180; alpha=acos((l3.^2+l5.^2-l6.^2)/(2.*l3.*l5))
%% Gerashof principle %%
if l2+l3<=l1+l4; A=1; else A=0; end
R1=l1*exp(1i*theta1); %%length of 1 body
n=3; %% points u=0; %%initial point for creating loop
for t2=0:2*pi/n:2*pi %% theta2 and step which it goes u=u+1;
R2=l2*exp(1i*t2); %%length of 2 body
D=R1-R2; %%length of interact of 1 and 2 bodies
d=norm(D); %%mesure of interact body
tD=angle(D); %%angle of interact body wich called "D"
delta=abs(acos((l3.^2.+d.^2-l4.^2)/(2*l3*d))); %%angle between two bodies
sigma=abs(acos((l4.^2.+d.^2-l3.^2)/(2*l4*d))); %%angle between two bodies
for nn=1:n
theta3(nn)=tD(nn)+delta(nn)
end
theta2(u,1)=t2 %%theta 2
% theta3(u,1)=delta+tD %%theta3
% % P=A*(l2*exp(1i*t2)+l3*exp(1i*(theta3(u,1)))); %%length of distance between input and specific point P=A*(l2*exp(1i*t2)+l5*exp(1i*(alpha+theta3(nn))))
px(u,nn)=real(P(nn)); %%real measure
px(u,nn)=image(P(nn));%%imagine measure
end % G=sum(px)/n; %Sum of the x values and divide on total number of x % D=sum(py)/n; %Sum of the y values and divide on total number of y
% hold on % plot(px,py), xlabel('P_x (m)'), ylabel('P_y (m)'), title('Path of the point P') LowerBand = [4 2 1 3]; UpperBand = [5 2 3 9]; for ii = 1:length(LowerBand) x(:,ii) = LowerBand(ii)+(UpperBand(ii)-LowerBand(ii))*x(:,ii); end cost = ((x(:,1)) - (2)).^2 + ((x(:,3)) + (x(:,4))).^2;
i want to use this function to find best parameters of l1,l3 and l4. would you please guide me.?

请先登录,再进行评论。


Morteza.Moslehi
Morteza.Moslehi 2017-1-6
Dears @Niels and @Walter Roberson thank you so much for your favor. finally i found my mistake.. is should write my inputs like below to find scalar amount NOt vector.
cost=zeros(size(x,1),1); for ii= 1:size(x) l1=x(ii,1); l2=2; l3=x(ii,3); l4=x(ii,4); l5=3; l6=4;

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by