MotionGenesis: F=ma Software, textbooks, training, consulting.    Solve ODEs   (Ordinary Differential Equations)


   Solve nonlinear 2nd-order ODE
To solve the ODE shown right with initial values of
x = 1 m   and   x' = 0.2 m/s,   for   t = 20 seconds, and to
Output   and   Plot   various quantities (with units),   type:
Variable x'' = cos(2*t) + sin(x)
Input    x = 1 m,   x' = 0.2 m/s,  tFinal = 20 sec     
OutputPlot  t sec,  x m,  x' m/s,  x'' km/s^2
ODE()  SecondOrderODE 
Note: The prime symbol ' denotes differentiation with respect to t.
Commands may be entered from a text file, e.g.,   SecondOrderODE.txt.
Change   SecondOrderODE   to   SecondOrderODE.m   to auto-generate a MATLAB® .m file.
x''   =   cos(2 * t)   +   sin(x)
MotionGenesis Second Order ODE .jpg
Plot with   MotionGenesis™ or MATLAB®.
The output file   SecondOrderODE.1   stores x(t), etc.


   Solve coupled nonlinear 1st-order ODEs
Shown below are ODEs for the angular velocity measures   wx, wy, wz
for the spinning-book (shown right) along with the commands to
Input  initial values and numerical integration parameters and to
Output  and   Plot   wx, wy, wz   via the  ODE  command.
 
Constant  Ixx = 1 kg*m^2,  Iyy = 2 kg*m^2,  Izz = 3 kg*m^2
Variable  wx',  wy',  wz'
wx' =  (Iyy-Izz) * wy * wz / Ixx
wy' =  (Izz-Ixx) * wx * wz / Iyy
wz' =  (Ixx-Iyy) * wx * wy / Izz
Input  wx = 0.2 rad/sec,   wy = 7.0 rad/sec,  wz = 0.2 rad/sec
Input  tFinal = 4 sec,   tStep = 0.02 sec
OutputPlot  t sec,  wx rad/sec,  wy rad/sec,  wz rad/sec
ODE() CoupledODESFirstOrder
Note: The prime symbol ' denotes differentiation with respect to t.
Note: Commands may be entered from a text file, e.g.,   CoupledODEs.txt.
Note: Change   CoupledODEs   to   CoupledODEs.m   to auto-generate a MATLAB® .m file.
Note: MotionGenesis™ forms these   dynamics equations here.
MotionGenesis: Solving coupled nonlinear first-order ODEs
 
MotionGenesis: Solving coupled nonlinear first-order ODEs
Plot with   MotionGenesis™ or MATLAB®.
The output file   CoupledODEs.1   stores t, wx, wy, wz.


   Solve Coupled 2nd-order ODEs
Shown below are the ODEs for   y   and   theta   that govern motions of the
swinging-spring (shown right) along with the MotionGenesis™ commands to
Input  initial values and numerical integration parameters and to
Output  and   Plot   theta vs. t   via the  ODE  command.
 
Constant  m = 1 kg,  g = 9.8 m/s^2,  k = 100 N/m,  Ln = 0.3 m
Variable  y'',  theta''
y'' = g * cos(theta)  +  (Ln+y) * theta'^2  -  k*y/m
theta'' = -(g*sin(theta) + 2*theta'*y') / (Ln+y )
Input  y = 0.2 m,  y' = 0,  theta = 1 deg,  theta' = 0
Input  tFinal = 15 sec,  tStep = 0.02 sec
OutputPlot  t sec,  theta degrees 
ODE() SwingingSpring
Note: Commands may be entered from a text file, e.g.,   SwingingSpring.txt.
Note: Change   SwingingSpring   to   SwingingSpring.m   to auto-generate a MATLAB® .m file.
Note: MotionGenesis™ forms these   dynamics equations here.
MotionGenesis: Solving coupled second-order nonlinear ODEs
 
MotionGenesis: Solving coupled second-order nonlinear ODEs
Plot with   MotionGenesis™ or MATLAB®.
The output file   SwingingSpring.1   stores the solution.
MotionGenesis™ generates fast compact symbolically-optimized code to solve ODEs
Code
Command and code file
Comments
MATLAB® ODE() SwingingSpring.m Modify input values in   SwingingSpring.m
Note: Interpreted .m codes may be slower than compiled codes.
C
Fortran
ODE() SwingingSpring.c
ODE() SwingingSpring.f
Compile and link source code.
Modify input values in   SwingingSpring.in
Note: Compiled codes can optimize for its host processor.
Depending on your   license,   these   valuable versatile   codes are independent of MotionGenesis™.


   Auto-generate MATLAB® or C code and pass an argument to that program.
Shown below are the MotionGenesis™ commands to solve the
ODEs for x and y that govern projectile motion of a baseball.
Initial values are specified in the input command and the OutputPlot
command plots   y vs. x   after the  ODE  command is run.
These commands auto-generate the MATLAB® file   MGProjectileMotion.m
To run from the MATLAB® prompt, type:   MGProjectileMotion( 0.05 )
 
Constant  m = 145 grams,  g = 9.8 m/s^2,  b
Variable  x'' = -b*x'/m
Variable  y'' = -g - b*y'/m
%-------------------------------------------
Input  x = 0 m,  x' = 44.7*cosDegrees(30) m/s
Input  y = 0 m,  y' = 44.7*sinDegrees(30) m/s
Input  tFinal = 3.8 s
OutputPlot  x meter,  y meter
ODE() MGProjectileMotion.m(  b  )
MotionGenesis Projectile motion with air-resistance
Plot with   MotionGenesis™ or MATLAB®.
Commands may be entered from a   text file.
MotionGenesis™ forms these   dynamics equations here.
Note:  To pass  b,  to the C-program   MGProjectileMotion.c,  type:
ODE() MGProjectileMotion.c( b )
After compiling, run from the operating system prompt by typing:
MGProjectileMotion   0.05