# How do I use MATLAB to obtain a continuous state equation from a transfer function and ode45 to solve it?

I have the following transfer function that I am trying to model in MATLAB with some additional unity white noise:

$$G(s)=\frac{1}{s^3 + \beta s^2}$$

I used the following to obtain the continuous state space equation:

beta = 0.2;
b = [0 0 0 1];
a = [1 beta 0 0];
[A,B,C,D] = tf2ss(b,a);


My understanding is that the tf2ss() function provides me with the values A and B which can be used in the following equation:

dxdt = Ax + Bu, where x is the initial condition and u is the unity white noise.

Upon running the above code snippet, I obtain the following values for A and B:

A = [-0.2,0,0; 1,0,0; 0,1,0]
B = [1; 0; 0]

Here is where I am running into trouble (although I may have run into an issue before with my assumptions regarding tf2ss()). I do not understand how to write the differential equation function required to use ode45(). I have tried the following:

function dx = mydiffeq(t,x)
A = [-0.2,0,0;1,0,0;0,1,0];
B = [1;0;0];

dx = A*x + B*randn(1);

end


And then I call it as follows:

tspan = [0 10];
xnot = 100;

[t,y] = ode45(@mydiffeq, tspan, xnot);
plot(t,y);


Obviously, I have a problem due to the size of my A and B matrices. I know that I am doing this wrong, but I do not know how to fix it.

You're doing pretty much everything right. The only problem is that your system is a third order system (s^3) and hence, the state x in

dx = A*x + B*u


is 3x1 but your initial condition xnot is a scalar. Replace

xnot = 100


with

xnot = [0 0 0].';


(or whatever initial condition you have) and it should work.

• Thank you. Such a simple thing to miss... but I didn't even think to look there. – embedded_guy Nov 17 '15 at 18:37