Calculate displacement of a mdof sytem with ODE45?
Show older comments
I am modelling a 2D half-car model in SimMechanics and MapleSim (to compare these two multibody software packages) for my project. My supervisor also want me to make a analytical model for comparing the outputs of both programs. So I have derived the mass matrix and stiffness matrix of this half-car model and I now want to calculate the displacement in vertical direction of the car. My supervisor told me I could do this with ODE45. I do not have any experience with MATLAB, so I have no idea what to do now.
This is my equation:
Mx"+Kx= 0 where M and K are a 4x4 matrix and they change every timestep(they are dependent of angle phi)
I have four generalised coordinates, so I have the initials conditions y0 which is a column of 8 scalars(displacement and velocity at the start)
My supervisor told me something like this: y = [x; xdot] so ydot=[xdot; xddot] where xddot is equal to: inv(M)-Kx, this is clear to me. This only has to be done for all the 4 generalised coordinates. so y = [x1; x1dot; phi; phidot; x2; x2dot; x3; x3dot] and for ydot the same. Am I right?
But what next? What do I have to do in MATLAB to generate the displacement using the ODE45 solver... Maybe I am a bit vague, so if you want to know something, just ask.
Accepted Answer
More Answers (3)
Bas
on 6 Sep 2012
Edited: Sean de Wolski
on 6 Sep 2012
3 Comments
Bas
on 6 Sep 2012
Matt Tearle
on 6 Sep 2012
Sorry, I was skipping bits of the code (doc style). Check the documentation for the full syntax for ode45. There should be more stuff (a timespan vector and initial conditions) in place of the "...". MATLAB treats a literal "..." as "this line is incomplete, see the next line for the rest" (and ignores the rest of the line). That's what's causing the error.
Bas
on 6 Sep 2012
Bas
on 7 Sep 2012
5 Comments
Matt Tearle
on 7 Sep 2012
Don't worry about the intermediate error messages. That's just the full trace from your initial script to the final error. The error is, of course, happening when you call ode45, so you're seeing an error in a function inside a function inside a function...
The problem is what you've identified: K is 4-by-4 and y is 8-by-1. Note in my original answer I said K and M would have to be 8-by-8 because you need to write the equations as My' = -Ky where y is the 8-element vector [x1; x1dot; phi ...]
That means that y' = [x1dot; x1dotdot; phidot; phidotdot; ...] = [y2; 50*x1 + 5*phi + x2 + x3; ...] = [y2; 50*y1 + 5*y3 + y5 + y7;...]. So K should be something like
[0 1 0 0 0 0 0 0]
[50 0 5 0 1 0 1 0]
[0 0 0 1 0 0 0 0]
[5 0 50 0 1 0 1 0]
[0 0 0 0 0 1 0 0]
[1 0 1 0 25 0 0 0]
[0 0 0 0 0 0 0 1]
[1 0 1 0 0 0 25 0]
(I think, based on what you have for K currently). And similarly for M.
Bas
on 10 Sep 2012
Matt Tearle
on 12 Sep 2012
Inside odefun you have t and y, so you can calculate anything based on those values at the current timestep. It looks like phi is y_2, so
K = [50*cos(y(2)) 5 1 1;...]
Also, don't use inv(M). Use the Mass option in ode45. But if you really don't want to do that, at least use -M\(K*y(5:8)) instead of inv(M).
I also think you may have your indices for y back-to-front somewhere. In the calling script, it seems like y_2 is phi and y_6 is phi'. If that's the case, then, in odefun, dydt(2) would be phi', so dydt(2) = y(6). And dydt(8) would be phi'' (calculated from M\K*y). In other words, in the script, y(1:4) are the primitive variables and y(5:8) are the derivatives; but in the function it's the other way around.
Bas
on 17 Sep 2012
Matt Tearle
on 18 Sep 2012
Your supervisor may have been thinking about just getting it working, and using inv is simple, conceptually and practically. Furthermore, in your example, the inverse is probably pretty painless.
But inv is, in general, slow and numerically sensitive. There are almost always better ways to do it, in terms of accuracy and efficiency. If an alternative is offered (such as the Mass property), you can bet that it's better.
(But, again, "better" can be a subjective term. If your goal is just to make this work, the simplest implementation may be the best.)
Bas
on 10 Sep 2012
Categories
Find more on Programming 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!