Converting equations to first order system for ODE45
Show older comments
Hi,
I'm trying to convert a set of equations of motion for a simple vehicle model into a set of first order differential equations for use with ODE45. This model is more complicated than previous ones I’ve used, with some derivatives appearing multiple times in the equations to be solved. This seems to give a problem with the order in which variables are presented in the code.
If I simplify the equations to make this discussion easier by removing all the constants, then they are:
- v' + r + phi'' = alpha1 + alpha2
- r' + phi'' = alpha1 + alpha2
- phi'' + v' + r + r' + phi = phi'
- alpha1' = v + r + phi + alpha1 + input
- alpha2' = v + r + phi + alpha2
Since there are 6 derivatives, I think there are 6 states that need initial conditions:
- v
- r
- phi
- phi'
- alpha1
- alpha2
The 6 equations in the ODE are:
- dx(1) = phi’
- dx(2) = r + phi'' + alpha1 + alpha2 % v’
- dx(3) = phi'' + alpha1 + alpha2 % r’
- dx(4) = v' + r + r' + phi + phi' % phi’’
- dx(5) = v + r + phi + alpha1 + input % alpha1’
- dx(6) = v + r + phi + alpha2 % alpha2’
Running this returns the error “Index in position 1 exceeds array bounds (must not exceed 1).” because dx(2) contains a reference to dx(4) (phi’’) which isn’t defined as yet. Which ever way I arrange equations dx(2), dx(3) and dx(4) I will have a problem with precedence.
How should I arrange the equations to avoid such problems?
Thanks.
Accepted Answer
More Answers (1)
William Rose
on 23 Nov 2022
I think you are corect tht a state vector with six elements, as you have defined above, will suffice. If we call the state vector x, then its six elements are:
x=[v, r, phi, phi', alpha1, alpha2]
v, r, phi are related by equations that are not obviously separateable into explicit equaitons for their derivatives. The derivatives are defined implicitly by a set of algebraic equaitons. Therefore you should read about ode15i() and the example for the Robertson system. I think you will be able to solve your system with this approach.
With the ode15i approach, we dfine a vector which is the derivative of the state vector:
xp=[v', r', phi', phi'', alpha1', alpha2']
You have explicit equations for the derivatives of aplha1, alpha2 and phi':
dx(3) = x(4) % v'
dx(5) = x(1)+x(2)+x(3)+x(5)+input % alpha'=v+r+phi+alpha1+input
dx(6) = x(1)+x(2)+x(3)+x(6) % alpha2'=v+r+phi+alpha2
The equaitons above can be rewritten as
0=xp(3)-x(4) % v'
0=xp(5)-(x(1)+x(2)+x(3)+x(5)+input) % alpha'=v+r+phi+alpha1+input
0=xp(6)-(x(1)+x(2)+x(3)+x(6)) % alpha2'=v+r+phi+alpha2
I recommend that you rearrange your equaitons 1,2,3 so that phi'' only appears in one of the equations. You had:
- v' + r + phi'' = alpha1 + alpha2
- r' + phi'' = alpha1 + alpha2
- phi'' + v' + r + r' + phi = phi'
You could write these as
1a. v' + r - r' = 0
2a. (phi')' - alpha1 - alpha2 + r' = 0
3a. alpha1 + alpha2 + v' + r + phi - phi' = 0
These can be rewritten in terms of x() and xp(): (recall x=[v, r, phi, phi', alpha1, alpha2])
0=xp(1)+x(2)-xp(2); % v'+r-r'=0
0=xp(4)-x(5)-x(6)+xp(2); % phi''-alpha1-alpha2+r'=0
0=x(5)+x(6)-xp(2)+xp(1)+x(2)+x(3)-xp(3); % alpha1+alpha2+v'+r+phi-phi'=0
Now you follow the example for the Robertson problem in the ode15i help: combine the equaitons into a function which returns a vector. Each row of the vector should be an equaiton that equals zero.
function res = simonidae(t,y,yp)
%SIMONIDAE: Simon's implicit differential algebraic equation
%Assume input=sin(t); adjust that part as desired.
res = [xp(3)-x(4); % v'
xp(5)-(x(1)+x(2)+x(3)+x(5)+sin(t)); % alpha'=v+r+phi+alpha1+input
xp(6)-(x(1)+x(2)+x(3)+x(6)); % alpha2'=v+r+phi+alpha2
xp(1)+x(2)-xp(2); % v'+r-r'=0
xp(4)-x(5)-x(6)+xp(2); % phi''-alpha1-alpha2+r'=0
x(5)+x(6)-xp(2)+xp(1)+x(2)+x(3)-xp(3)] % alpha1+alpha2+v'+r+phi-phi'=0
end
Then you will set options, find a consistent initial conditions x0 and xp0, set options, set the time span, and finally, call ode15i to solve the system:
[t,x] = ode15i(@simonidae,tspan,x0,xp0,options);
Good luck.
6 Comments
William Rose
on 23 Nov 2022
@Simon Aldworth, check all my equations and code carefully. It is alway possible that I made a typo somewhere.
William Rose
on 23 Nov 2022
Note that simonidae() has six independent equations, each of which equals zero. This equals the number of elements of the state vector. That is as it should be.
Simon Aldworth
on 23 Nov 2022
William Rose
on 23 Nov 2022
Edited: William Rose
on 23 Nov 2022
[edit: I forgot to include a closing bracket in funciton simonidae. I have added it now.]
You're welcome.
You are referring to equations
- v' + r + phi'' = alpha1 + alpha2
- r' + phi'' = alpha1 + alpha2
- phi'' + v' + r + r' + phi = phi'
which I rearranged. I rearranged them because I suspect the solver will work better with a simplified set of equations. But you do not have to rearrange them, and you can still apply the approach which I recommended earlier.
- 0 = v' + r + phi'' - alpha1 - alpha2
- 0 = r' + phi'' - alpha1 - alpha2
- 0 = phi'' + v' + r + r' + phi - phi'
Recall x=[v, r, phi, phi', alpha1, alpha2], xp=[v', r', phi', phi'', alpha1', alpha2']
0 = xp(1)+x(2)+xp(4)-x(5)-x(6)
0 = xp(2)+xp(4)-x(5)-x(6)
0 = xp(4)+xp(1)+x(2)+xp(2)+x(3)-x(4)
I have a feeling that in the last equation, it is important to use "-x(4)" rather than "-xp(3)". Now you use the three equations above in simonidae(), instead of the ones in my previous posting:
function res = simonidae(t,y,yp)
%SIMONIDAE: Simon's implicit differential algebraic equation
%Assume input=sin(t); adjust that part as desired.
res = [xp(3)-x(4); % v'
xp(5)-(x(1)+x(2)+x(3)+x(5)+sin(t)); % alpha'=v+r+phi+alpha1+input
xp(6)-(x(1)+x(2)+x(3)+x(6)); % alpha2'=v+r+phi+alpha2
xp(1)+x(2)+xp(4)-x(5)-x(6); % v'+r+phi''-alpha1-alpha2
xp(2)+xp(4)-x(5)-x(6); % r'+phi''-alpha1-alpha2
xp(4)+xp(1)+x(2)+xp(2)+x(3)-x(4)] % phi''+v'+r+r'+phi-phi'
end
Try something like that.
Maybe of help to get the system explicit in the derivatives:
syms xp1 xp2 xp4 x5 x6 x2 x3 x4
eqn1 = xp1 + x2 + xp4 - x5 - x6 ==0;
eqn2 = xp2 + xp4 - x5 - x6 == 0;
eqn3 = xp4 + xp1 + x2 + xp2 + x3 - x4 == 0;
sol = solve([eqn1,eqn2,eqn3],[xp1,xp2,xp4])
William Rose
on 23 Nov 2022
@Torsten and @Steven Lord are giving you excellent suggestions. They are such good resources on this site.
The mass matrix approach of @Steven Lord is an alternative way of specifying and solving implicit differential algebraic equations. It works when the implicit equaitons are linear in the derivative of the state vector. And your equations are.
When the implicit equations are linear, then they are solvable, i.e. the explicit equaitons can be found. The solution may not be very practical, which is why the mass matrix approach can be useful. @Torsten shows that in your case, the set of three equations has a nice simple solution. Therefore, using @Torsten's result, you can write six explicit equations for the derivative of the state vector, and solve in the standard way, with ode45() for example.
See here for a nice explanation of implicit, explicit, mass matrix, and which solvers work with what.
You said you have given a simplified version of the equations, with some constants removed. Maybe the explicit solution of @Torsten will not be so simple when the true equations are used.
My approach and the approaches of @Steven Lord and @Torsten should give the same results in the end.
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!