Solve state space equation by ODE45

Hello everybody.
I am new to state space representation. please help me to solve this question.
I have state space equation for IM motor like this:
xdot=A.x+B.u --> and their dimensions are : [50,1]=[50,50]*[50,1]+[50,50]*[50,1]
I have calculated A & B and I need to get xdot. (there are no C & D)
1- how should I write my function? (I don't use Simulink)
2- I used my solver like this:
>> [t,y] = ode45(@sys, tspan, zeros(50,1));
Thanks in advance.

 Accepted Answer

Hi Babak
In order to solve an ODE using ode45, you need to first define the function to describe the complete dynamics. In the linear state space system you provided, the definition of u is missing. In general, u can be designed as a linear feedback control law, such as u = Kx, where K is a 50-by-50 matrix. Let me give you a simpler example here. Suppose we have A = [0 1; -2 3]; B = [0;1]; K = [-1 -1]; Then, the system function can be constructed as
function dx = sys(t, x)
A = [0 1; -2 3]; B = [0;1]; K = [-1 -1];
u = K*x
dx = A*x + B*u;
end
Please save the above function in an MATLAB file and name is as sys.m
Then, in a separate MATLAB script file or in the command window, you can simulate the system by executing the following
>> tspan = [0 10];
>> iniCon = [1;-1]
>> [t, y] = ode45(@sys, tspan, iniCon);
If you would like to show the simulation result in a figure, try executing >> plot(t,y)
For more details regarding ode45, see here .

5 Comments

Hi Yu Jiang,
Thank you for your complete answer. Actually I did not get what you mean by missing U. I defined U at the beginning as a matrix [50,1].
Let me add more explanation:
Initial value of matrices U and X are defined at the beginning. When I calculate the xdot, I will use it as a feedback for U and X, and again calculate the xdot.
My codding is like this:
function xdot=sys(X,U,A,B)
xdot=A*X+B*U;
end
[t,y]=ode45('sys',tspan,X);plot(t,y)
X=y;
U=xdot*K ( Assume K is a constant value)
And now I have defined new U and X and I will repeat to calling the function and solver.
Hope this clarify that I have U & X at the beginning and I will update them step by step. But when I do this I got error like this:
Error using sys (line 3) Not enough input arguments.
Error in odearguments (line 88) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 114) [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Hi Babak
ode45 is used for simulating autonomous systems (or closed-loop system). The system has to be in the form of
xdot = f(t, x)
not
xdot = f(t, x, u) % u cannot be used as an additional argument here.
Therefore, you need to represent u in terms of x explicitly inside the system function (That was what I meant by 'missing u'). Then, please remove u from the arguments of the function. After that, you may use ode45.
I revised your code as follows
function xdot=sys(t,X,K,A,B)
u = K*x;
xdot =A*X+B*U;
end
>>[t,y]=ode45(@(t,X) sys(t,X, K, A,B),tspan,X);
>>plot(t,y)
Please notice that in your example K should be a 50-by-50 matrix. Also, notice that I used an anonymous function such that you can pass K, A, B to the system function.
How is the sys.m file altered for a two input system?
how i plot control input u with tspan

Sign in to comment.

More Answers (5)

Babak
Babak on 9 Aug 2014
Thank you Yu Jiang. After your explanation I could get answer from the solver but still I have a problem for updating. Please have a look on attached picture.
I must update X and U in next step. as you said I must update U inside the function but it didn't work.
function xdot=sys(t,X,A,B,Lsr,Tm)
p=4;Tl=0;Tm=0; J=0.03832; Vab=208;Vbc=208;
U=[Vab,Vbc,zeros(1,46),((Tm-Tl)/J),0]';
xdot=A*X+B*U;
Is=[xdot(1,1),xdot(2,1),-(xdot(1,1)-xdot(2,1))];% 1st calculation
Tm=(p/2)*Is*(Lsr)*xdot(3:48,1); % 2nd calculation
U=[Vab,Vbc,zeros(1,46),((Tm-Tl)/J),0]'; %use 1st and 2nd to update U
end
[t,y]=ode45(@(t,X) sys(t,X,A,B,Lsr,Tm),tspan,X);
X=y; % Update X
Two problems:
1- how can I update the U inside (or outside) the function? (I need xdot to update U)
2- When I solved the equation the dimension of y is not [50,1], then I can not use it as a input again.(X=y ?)

1 Comment

Yu Jiang
Yu Jiang on 9 Aug 2014
Edited: Yu Jiang on 11 Aug 2014
From what I understand, this is not an issue with MATLAB, but an issue with control theory.
In standard state-space-based control design, the control signal u is usually a function of x, not a function of xdot. If it is the later case, I think you may need to do some math to rearrange the terms with dot operation to the left-hand side of the equation.
Again, bear in mind that ode45 is used for autonomous systems in the form of dx = f(t,x). No derivative terms of x should appear on the right hand side. If you define Tm using xdot and use it as part of the input u, you are actually introducing xdot to the right hand side of the equation.

Sign in to comment.

Heloo, i have some questions to ask i have a state space, dx/dt = Ax(t) + Bu(t) , y = C x(t) + D u(t) i know all the values and the size of the matrix's
A is a (6*6)matrix B is a (6*1)matrix C is a (6*6) matrix D is a (6*1) mtrix
i need to input a signal in my state space system, the signal is a vector (6000*2),
in the first column i have the time and the second column i have the acelerations
Ts is my simple time Ts = 0.02 the interval of the integration is 0 to 120 [0 120]
so if i put in matlab --> sys = ss(A,B,C,D,Ts )
how can i input my signal and how can a solve it whit the ODE45 ?
THANKS
I have Linearized model state-space show below [I need to plot output (P&Q) in matlab ]
where
a=[-100 -628.32 -1000 0 0 0 0 0 0 0 0;226.19 -100 0 -1000 0 0 0 0 0 0 0;100000 0 0 -628.32 -104790 0 102500 0 0 0 0;0 1e6 226.19 0 0 -37726 0 36902 0 0 0;0 0 325.55 0 -45.54 -376.99 44.375 50.528 212.38 0 0;0 0 0 325.55 376.99 -45.54 -50.528 44.375 -14.612 0 0;0 0 0 0 28.905 0 -29.551 1.5489 1591.1 0 0;0 0 0 0 0 28.905 -1.5489 -29.551 -109.47 0 0;0 0 0 0 0.87472 -0.060182 -0.89748 0.014545 -1.0531 16849 42.123;0 0 0 0 0 0 0 0 -0.025 0 1;0 0 0 0 0 0 0 0 1.25 -20000 -55.302];
b=[4331.6 5243.1 0 0 0;0 -6371.2 -97980 0 0;22411 -4527.5 -7979600 0 0;-7619.9 -17920000 3041500 0 0;-222.94 0 0 0 0;24.59 0 0 0 0;-1591.1 0 0 0 0;109.47 0 0 0 0;0.39844 0 0 0 0;0 0 0 0 0;0 0 0 22.848 -5.3188];
c=[9798 0 0 0 0 0 0 0 0 0 0;0 -16330 0 0 0 0 0 0 0 0 0];
d=[219.62 0 -24528 0 0;108.44 136279 137990 0 0];
and Input, state and output variables at the operating point for linearization

1 Comment

i need to give speed bump and steering as inputs to my state space block, but my inputs are in graphical form how to give these two inputs to the same state space block.

Sign in to comment.

Yower Jymmy
Yower Jymmy on 26 Apr 2021
Yu Jiang Can you tell me how I plot input signal u from (dx/dt) = A*x + B*u with ode45?
bhargav
bhargav on 10 Dec 2024
Problem No.1
  • Handwritten work for the derivation of the state space model, the observer gains and the controller gains.
  • MATLAB: Do the whole problem using MATLAB.
  • Simulate using SIMULINK. Here, include images of the SIMULINK blocks and outputs unless you are able to incorporate the SIMULINK work into the MATLAB published output.
For a mechanical control system, the transfer function of the plant is given by:
H(s)=1400s(s+2)(s+120)H(s) = \frac{1400}{s(s + 2)(s + 120)}H(s)=s(s+2)(s+120)1400
Design a controller to result in a 10% overshoot and a settling time of 1 second. Place the third pole at 10 times as far from the imaginary axis as the dominant poles.
Assume that the state variables of the plant are not accessible, design an observer for the states.
MATLAB:
Observer Design:
  • Assume the states' initial conditions are:x0=[0.1,0.15,0.2]x_0 = [0.1, 0.15, -0.2]x0=[0.1,0.15,0.2]and x^(0)=[0,0,0].\hat{x}(0) = [0, 0, 0].x^(0)=[0,0,0].
  • Plot the unit step response of the system and clearly show that you have a successful observer design. Clearly show the response of the states, xi(t)x_i(t)xi(t) and x^i(t)\hat{x}_i(t)x^i(t), and the errors, xe(t)x_e(t)xe(t). Explain the results.
Controller Design:
Design a full-state feedback controller based on the stated requirements in the problem and the observer designed above since the states are not accessible via measurements. Simulate for the unit step response of the system.

1 Comment

This sounds like a homework assignment. If it is, post it as a new question. In that new question, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.

Sign in to comment.

Asked:

on 8 Aug 2014

Commented:

on 10 Dec 2024

Community Treasure Hunt

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

Start Hunting!