How to use Runge Kutta method to solve state space equation

40 views (last 30 days)
Eilidh Graham
Eilidh Graham on 25 Nov 2021
Commented: Walter Roberson on 15 Dec 2021
I am trying to use the Runge Kutta method to solve
xdot = Ax + Bu
Matrices A and B are constants and the input vector u is a known function of time. I am looking to get values for x. I have seen lots of posts about runge kutta but am very confused as to how to get it to work in matlab.
  2 Comments
Eilidh Graham
Eilidh Graham on 8 Dec 2021
% Solve the equations of motion using a Runge-Kutta routine
[t,x2]=ode45(@LinearEoM,t,uc);
save (fname, 't', 'x2')
function xdot2 = LinearEoM(t, x2)
global Ac Bc uc
xdot2 = Ac*x2 + Bc*uc;
end
Ac is a 4x4 matrix, Bc 4x12, uc is a timeseries with 12 rows. I am looking to get x2 as a timeseries, it will have 4 rows. I am just not sure if i am doing this right at all

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 8 Dec 2021
Edited: Walter Roberson on 8 Dec 2021
(4 by 4) * (4 by 1) --> 4 by 1
(4 by 12) * (12 by 1) --> 4 by 1
4 by 1 + 4 by 1 --> 4 x 1
So that calculation sounds plausible -- but only if your uc timeseries has only 1 column.
However... you are passing in uc as the initial conditions, as well as using uc independently inside the code. If uc has 12 rows and it also forms the initial conditions, then the first x2 value passed into the code would have 12 rows, and you would be ding (4 by 4) * (12 x something) which is going to fail.
When you are looking to get an output that has 4 rows, then your initial conditions should have 4 rows.
Also we recommend against using global. See http://www.mathworks.com/help/matlab/math/parameterizing-functions.html . Oh and remember that Bc*uc is independent of your x2 value, so there is no point re-calculating Bc*uc each iteration: might as well calculate it once outside the function and pass the combination in.
  8 Comments
Walter Roberson
Walter Roberson on 15 Dec 2021
You indicate that your time series has 12 rows and 21 columns. Timeseries have one row per time instance so that would describe a time series with 12 time instances and 21 elements for each time. But your code requires that you find 12 values at any given time, which would imply that the columns are time and the rows are the values you need.
To be more correct, timeseries objects can have two different organizations:
  1. 2 dimensional data in which number of rows matches number of times, and columns are the data; OR
  2. 3 or more dimensional data in which the final dimension must match the number of times, and the previous dimensions are the data for that time
As you have 2 dimensional data that is 12 x 21 then the number of time instances for you data must be 12. But the structure of your problem implies that the number of time instances should be 21 and that you want 12 values at each time.

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by