I am a novice user of MATLAB. I have code that is trying to find the time history of a state space model. There are four first order ODEs that I want to solve simultaneously using ode45
. The essence of the equations to be solved is as follows:
x1_dot = x2
x2_dot = -[M] * [K] * x1 - [M] * [C] * x2 + constant*[M] * [P3] * x3 + constant*[M] * [P4] * x4
x3_dot = x2 - constant*x3
x4_dot = x2 - constant*x4
Where [M]
, [K]
, [C]
, [P3]
, and [P4]
are 3x3 matrices; x1
, x2
, x3
, x4
are all 3x1 vectors; and x1_dot
etc. represent the time derivatives (which are 3x1 vectors). I have initial conditions only for x1
.
The MATLAB code I've written is below. This code is within my overall program. I am not calling a separate function because I do not know how to pass all of the matrices/vectors into ode45
through a function. I am getting the error: "Index exceeds matrix dimensions."
tspan = 0:1:20;
initial = [0 0.03491 0];
f = @(t,x) [x(2);
-inv(M_Dbl_Bar_Matrix)*K_Dbl_Bar_Matrix*x(1) - inv(M_Dbl_Bar_Matrix)*C_Dbl_Bar_Matrix*x(2) + (0.5*rho*U^2)*inv(M_Dbl_Bar_Matrix)*P3_Matrix*x(3) + (0.5*rho*U^2)*inv(M_Dbl_Bar_Matrix)*P4_Matrix*x(4);
x(2) - Beta_1*x(3);
x(2) - Beta_2*x(4)];
[t,xp] = ode45(f,tspan,initial);
Questions:
How do I address the
x(1)
,x(2)
,x(3)
, andx(4)
inode45
being 3x1 vectors?How do I apply initial conditions for this system of equations? For example, do I use a vector format such as:
initial = [0 0.03491 0; 0 0 0; 0 0 0; 0 0 0]
?Am I correctly using / written the function (
f
) andode45
?
1.) What do you mean by "being 3x1 vectors"?
x(i)
has to be a single variable in order for this to work, so size(x)=1x4. Havingx(i)=(x,y,z)
does not really make any sense in the context of ODE's.2.) Your variable vector
x
should have length 4, so your initial conditions should reflect this.Works fine for me.
3.) Yes, I think so.
Could you maybe provide more information on what you are trying to do?
EDIT
Ok, I think I understand now what you are trying to do.
You have 4 vectors for instance: x(x,y,z), x'(x,y,z), p(x,y,z), q(x,y,z) and a large matrix 4x4 matrix consisting of 3x3 matrices. So I guess
(forgive the hosted images, since im a stackoverflow noob I'm not allowed to post html or images directly, or multiple links)
Reference for Math 1,2,3: https://postimg.org/gallery/1qh2ywiqq/
Math 1 from link
with
Math 2 from link
your state space equation.
So to solve this you have to set up a 12 dimensional ode45 problem, instead of the 4 dimension problem you have now, since each vector has 3 components. What you have to do is change the 4x4 matrix into a 12x12 matrix by specifying each of the entries explicitly. You also have to give
f = @(t,x)
a 1x12 vector:Math 3 from link
Then set
initial
to a 1x12 vector of initial conditions (one for each dimension in our vector).With the matrix form and initial conditions we have now, we can use this source: https://nl.mathworks.com/help/symbolic/solve-a-system-of-differential-equations.html#buxuujb
which tells us how to set this up properly.
I currently don't have the time to give you the code, but I hope I understood correctly what you are trying to do and this will be useful.