computing state space equation using ode45 solver

25 views (last 30 days)
Hi everyone,
I have a state space equation for a circuit of the form:
Dx = Ax + Bu
y = Cx + Du
I can solve it already using:
sys = ss(A,B,C,D);
system = lsim(sys,u,t);
Can this be solved using an ode45 solver?
we have 10 states, 1 input and 10 outputs. u is a pulsed waveform and we have alot of time steps (over 1 million)
Thanks
Feras

Accepted Answer

Star Strider
Star Strider on 30 Jan 2016
This example solution will do what you want. You will have to make the necessary changes to it for your system:
A = [0.1 -0.2; 0.3 0.4];
B = [0.4; 0.5];
x0 = [0.1; 0.2]; % Initial Conditions Vector For ‘ode45’
SysFun = @(t,x,u) A*x + B*u; % Dynamical System Differential Equation
T = 5; % Length (Time Units) Of Each Segment
Td = 10; % Length Of ‘tspan’ Vector For Each Time Segment (Number Or Elements In The Vector)
tspan = linspace(0, T, Td);
for i=1:50 % Choose Number Of Time Segments (Here, 50)
u = (i^4*(-1)^i); % Choose Appropriate Input For Each Time Interval
[t(:,i),x(:,:,i)]=ode45(@(t,x) SysFun(t,x,u),tspan,x0);
x0=x(end,:,i);
tspan = linspace(t(end,i), t(end,i)+T, Td);
uv(i) = u;
end
tv = reshape(t, [], 1); % Aggregate Time Vector
xp = permute(x, [1 3 2]); % Permute ‘x’
xv = reshape(xp, [], size(x,2)); % Correctly-Formatted Output Matrix For ‘x’, Matching ‘tv’
figure(1)
plot(tv, xv)
grid
It allows you to define the length of each time interval (the ‘T’ value), the number of samples in each time interval (the ‘Td’ value), and the value of the input ‘u’ at each time interval. It then creates an appropriate-sized array ‘yv’ for the output as a function of time vector ‘tv’.
It does not include the output equation:
y = C*x + D*u;
so you will have to add that.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!