I am getting an error using lsim for discrete time system?

Error :
Error using DynamicSystem/lsim (line 85) When simulating the response to a specific input signal, the input data U must be a matrix with as many rows as samples in the time vector T, and as many columns as input channels.
Error in hw4 (line 26)
[x,k] = lsim(sys1,U,x0);
Here is my code :
clc
clear all
close all
%%Q2 a
w = sqrt(1)*randn(500,1);
v = sqrt(0.5)*randn(500,1);
wmean = mean(w);
wcov = cov(w');
vmean = mean(v);
vcov = cov(v');
a = [-0.08 -1;0.7 0.1];
b = [0.34; 0.3];
bw = b;
c = [0 3];
d = 0;
sys1 = ss(a,b,c,d,1)
N = 500;
u = 10*ones(1,N);
k = (0:1:N-1)';
x0 = [2 2]';
U = u' + w;
[x,k] = lsim(sys1,U,x0);
I don't know where I am getting wrong?
System description :
X(k+1) = a.X(k+1) + b.(10 + w(k));
a = [2X2];
b = [2X1];
w is process noise
y(k) = c.X(k) + v(k); c = [1X2]; v is measurement noise.
Want to simulate the system with w,v and u.

Answers (1)

You did not specify the time vector while using lsim function
Ts=1% your sample time
t=0:Ts:(numel(U)-1)*Ts; % vector time with same size as U
[x,k] = lsim(sys1,U,t,x0);
stem(k,x)

2 Comments

Earlier in my U definition, I just included u and w and not v. I figured out that v should also be the part of my inputs. Now, I have modified the system matrices.
System description :
States : X(k+1) = a.X(k+1) + b.(10 + w(k)); a = [2X2]; b = [2X1]; w is process noise
Output : y(k) = c.X(k) + v(k); c = [1X2]; v is measurement noise
I have modified a modified B and D matrix for the system that includes u,w,v.
A = [-0.08 -1;0.7 0.1];
B = [0.34 0.34 0;0.3 0.3 0]; [2X3]
D = [0 0 1]; [1X3]
U = [u' w v]' [3X1]
sys1 = ss(A,B,C,D,1)
N = 500;
u = 10*ones(1,N);
Ts = 1;
t = (0:Ts:N-1)';
x0 = [2 2]';
U = [u' w v]';
[y,t,x] = lsim(sys1,U,t,x0);
figure(1);
stem(y,t);
figure(2);
stem(x(:,1),t);
hold on;
stem(x(:,2),t);
Is this the correct way to define the system and perform simulations?
It seems correct, don't forget to define your noise signals v and w
A = [-0.08 -1;0.7 0.1];
B = [0.34 0.34 0;0.3 0.3 0];
C=[0 3 ]
D = [0 0 1];
sys1 = ss(A,B,C,D,1)
N = 500;
u = 10*ones(N,1);
v=rand(N,1);
w=rand(N,1);
U = [u w v]
t = (0:N-1)';
x0 = [2;2];
[y,t,x] = lsim(sys1,U,t,x0);
figure(1);
stem(y,t);
figure(2);
stem(x(:,1),t);
hold on;
stem(x(:,2),t);

Sign in to comment.

Tags

Asked:

on 18 Nov 2013

Commented:

on 18 Nov 2013

Community Treasure Hunt

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

Start Hunting!