Error:Dimensions of matrices being concatenated are not consistent

4 visualizaciones (últimos 30 días)
This is my code to optimize a function. I get the error when i run the code. The error is in the line
[to,yo]=sim('hw',5,[],[t',u]);
and
[u_final,cost]=fmincon('find_cost',u,[],[],[],[],l_b,u_b,'find_constraint',options);
This is the code
%Control input to define 0.1 for one half and -0.1 for the other half of
%the time interval
t=0:.1:5;
u(1:25)=.1;
u(26:51)=-.1;
plot(t,u);
[to,yo]=sim('hw',5,[],[t',u']);
%Finding u_final and cost
l_b=ones(51,1)*(-50);
u_b=ones(51,1)*50;
options=optimset('Display','iter','PlotFcns','optimplotx');
[u_final,cost]=fmincon('find_cost',u,[],[],[],[],l_b,u_b,'find_constraint',options);
[t_f,x_f,y_f]=sim('hw',5,[],[t' u_final']);
figure(3);
subplot(t_f,y_f(:,1));
grid;
xlabel('Time');
ylabel('Position');
figure(4);
subplot(t_f,y_f(:,2));
grid;
xlabel('Time');
ylabel('Velocity');
figure(5);
subplot(t_f,y_f(:,3));
grid;
xlabel('Time');
ylabel('Acceleration');
figure(6);
subplot(t_f,y_f(:,4));
grid;
xlabel('Time');
ylabel('Jerk');
figure(7);
subplot(t_f,y_f(:,5));
grid;
xlabel('Time');
ylabel('Jounce');
Kindly help me. I need to do this very soon
  3 comentarios
Aishwarya Bangalore Kumar
Aishwarya Bangalore Kumar el 23 de Feb. de 2018
Error using horzcat Dimensions of matrices being concatenated are not consistent.
Error in hw3q2b (line 11) [to,yo]=sim('hw',5,[],[t',u]);

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 23 de Feb. de 2018
t=0:.1:5;
u(1:25)=.1;
u(26:51)=-.1;
Those both create row vectors. You do not change them in the code you show.
Then at some point you have
[to,yo]=sim('hw',5,[],[t',u]);
With t being a row vector, t' is a column vector. u is still a row vector. So you are trying to use [,] between a column vector and a row vector, which fails because the two do not have the same number of rows.
The line you indicate you are having a problem with does not appear in the code you posted. In the code you posted, the similar line uses u' which makes a column vector, which should be fine with t' provided that t and u are the same length.
  9 comentarios
Walter Roberson
Walter Roberson el 23 de Feb. de 2018
You need to use
[to,yo]=sim('hw',5,[],[t',u']);
Aishwarya Bangalore Kumar
Aishwarya Bangalore Kumar el 23 de Feb. de 2018
I got it now. Thank you for your help sir.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by