Error:Dimensions of matrices being concatenated are not consistent

4 views (last 30 days)
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 Comments
Aishwarya Bangalore Kumar
Aishwarya Bangalore Kumar on 23 Feb 2018
Error using horzcat Dimensions of matrices being concatenated are not consistent.
Error in hw3q2b (line 11) [to,yo]=sim('hw',5,[],[t',u]);

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 23 Feb 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 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!