Error:Dimensions of matrices being concatenated are not consistent

2 ビュー (過去 30 日間)
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 件のコメント
Aishwarya Bangalore Kumar
Aishwarya Bangalore Kumar 2018 年 2 月 23 日
Error using horzcat Dimensions of matrices being concatenated are not consistent.
Error in hw3q2b (line 11) [to,yo]=sim('hw',5,[],[t',u]);
Aishwarya Bangalore Kumar
Aishwarya Bangalore Kumar 2018 年 2 月 23 日
This is the error message

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2018 年 2 月 23 日
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 件のコメント
Walter Roberson
Walter Roberson 2018 年 2 月 23 日
You need to use
[to,yo]=sim('hw',5,[],[t',u']);
Aishwarya Bangalore Kumar
Aishwarya Bangalore Kumar 2018 年 2 月 23 日
I got it now. Thank you for your help sir.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by