I want to solve ode equation which is written in matrix form. A=[-0.0178 0 -10;0 -0.2352 10;1 -1 0];
and B=[0.3313 -8.9335;0 3.7245;0 0]
. Variables are x=[x1;x2;x3];
. Input are u=[v;f];
. xdot=[x1dot;x2dot;x3dot];
. Finally the ode becomes - xdot=Ax Bu, initial conditions are x0=[0;0;0];
. Inputs are v=0.05 and f=0;
How to solve this equation through anonymus function by ode45. Another question is that if there is larger matrix say 100 order, then it is very difficult to write separate form then how can I solve if they are in particular form like xdot=Ax Bu where xdot is n * 1, A is n * n, x is n * 1, B is n * m and u is m * 1.
CodePudding user response:
The default solvers assume that the ode is time dependent (which they normally aren't). Also, I like to start off very general so I would create a function in a separate file (maybe mySystem.m) with five variables: t, x, u, A and B. It would look something like
function dx = mySystem(t, x, u, A, B)
dx = ... (Insert system equations)
end
then in the script where you define A, B, and u and where you ultimately want to use something like ode45 I would create an anonymous instance of this function which has the inputs that ode45 wants, ie t and x. It would look something like:
A = ...;
B = ...;
u = ...;
f = @(t, x) mySystem(t, x, u, A, B);
now, this f can easily be used ode45:
tspan = [t0, tf];
x0 = ...;
[t, y] = ode45(@f, tspan, x0);
If you now need to change B or A or u you just do it in your script, and you can reuse mySystem for other problems.
I do realize mySystem is a one-liner and could be an anonymous function too, but other ode's may not fit on one line and will need a separate file. This way the programming becomes more consistent. It is up to you.