State space without using the control toolbox - Error using + Matrix dimensions must agree.

1 回表示 (過去 30 日間)
George Green
George Green 2015 年 5 月 28 日
編集済み: George Green 2015 年 5 月 29 日
I am trying to calculate a continous plant for a unit step input.
Eg dX/dt= AX * Bu y = CX
I get the following error: Error using + Matrix dimensions must agree.
Can anyone please assist me??
My code is below:
%%Time specifications
stopTime = 10;
Fs = 1;
dt = 1/Fs;
t = (0:dt:stopTime)';
N = size(t,1);
%%State space model
A = [0,1;-45,-876];
B = [0,0;1,1]; %Rev 3
C = [59 99];
%%Pre-allocation
u = zeros(2,N);
x = zeros(2,N); %Rev 1
y = zeros(2,N);
%%Initial conditions
u(:,1) = [ 1 ; 1 ];
x(:,1) = [ 0 ; 0 ]; % Rev 1
y(:,1) = [ 0 ; 0 ];
%%Simulation - main loop
for k = 2:N
x(:,k) = x(:,k-1) + dt.*(A*x(:,k-1) + B*u(:,k-1); % Rev 2
y(:,k) = C*x(:,k);
end
%%Plot results
figure;
ax(1) = subplot(2,1,1);
plot(t,x);
ax(2) = subplot(2,1,2);
plot(t,y);
linkaxes(ax,'x');

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2015 年 5 月 28 日
The matrix A is 2x2 then x should be 2x1 not 3x1
  1 件のコメント
George Green
George Green 2015 年 5 月 29 日
編集済み: George Green 2015 年 5 月 29 日
Hi Azzi,
I correct the errors above for x (Rev 1) however I still get:
Error using + Matrix dimensions must agree.
Error in (line 28) x(:,k) = x(:,k-1) + dt*(A*x(k-1) + B*u(k-1));
Your suggestions would be much appreciated.

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


Nobel Mondal
Nobel Mondal 2015 年 5 月 29 日
In this line,
x(:,k) = x(:,k-1) + dt*(A*x(k-1) + B*u(k-1)); % Error
The term A*x(k-1) would give you a 2x2 matrix. You should be using x(:,k-1) instead.
x(:,k) = x(:,k-1) + dt*(A*x(:,k-1) + B*u(k-1));
  1 件のコメント
George Green
George Green 2015 年 5 月 29 日
Thanks for your help Nobel,
Thats what I meant to do but bit of an oversight on my part. I made the changes (Rev2) but got: " Error using * Inner matrix dimensions must agree." for x(:,k) = x(:,k-1) + dt.(A*x(:,k-1) + B*u(:,k-1));"
I then changed B from " B = [0;1] " to " B = [0 0 ;1 1] " ....Does this look right?

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by