Solve ODEs
(Ordinary Differential Equations)

Solve a 2nd-order ODE

Shown below is the MotionGenesis™ command file to solve the ODE shown right with initial values of x = 1 m and x' = 0.2 m/s for 20 seconds and to Output and Plot  x(t) vs. t (with units), type:
Variable x'' = cos(2*t) + sin(x) Input x = 1 m, x' = 0.2 m/s Input tFinal = 20 sec, tStep = 0.1 sec OutputPlot t seconds, x meters ODE() MG2ndOrderODE
Note: The prime symbol ' denotes differentiation with respect to time t. To auto-generate a MATLAB® .m file, change ODE() MG2ndOrderODE  to  ODE() MG2ndOrderODE.m
x'' = cos(2*t) + sin(x)

Solve coupled 1st-order ODEs

Shown below is the MotionGenesis™ command file that solves ODEs for the angular velocity measures  wx, wy, wz  of a rotating rigid body. Initial values and numerical integration parameters are entered with the Input command. The OutputPlot command plots wx, wy, wz vs. time t.
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/s, wy = 7.0 rad/s, wz = 0.2 rad/s Input tFinal = 4 sec, tStep = 0.02 sec OutputPlot t sec, wx rad/sec, wy rad/sec, wz rad/sec ODE() MGCoupledODEs
Note: The prime symbol ' denotes differentiation with respect to time t. To auto-generate a MATLAB® .m file, change ODE() MGCoupledODEs to ODE() MGCoupledODEs.m
MotionGenesis™ forms and solves these  dynamics equations here. See 3D spin-stability, video, and Dzhanibekov effect here.

Solve coupled 2nd-order ODEs

Shown below is the MotionGenesis™ command file that simulates a swinging-spring. Initial values of y (stretch) and θ (swing angle) are entered in the Input command. The OutputPlot command plots θ vs. t (time).
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() MGSwingingSpring
MATLAB®: To auto-generate the file MGSwingingSpring.m, change ODE() MGSwingingSpring to ODE() MGSwingingSpring.m
MotionGenesis™ forms and solves these  dynamics equations here.
MotionGenesis™ generates fast compact
symbolically-optimized codes to solve ODEs
Code Command and code file Comments
MATLAB® ODE() MGSwingingSpring.m Modify input values in  MGSwingingSpring.m
Interpreted .m codes are slower than compiled codes.
C
Fortran
ODE() MGSwingingSpring.c
ODE() MGSwingingSpring.f
Compile and link source code.
Modify input values in  MGSwingingSpring.in
Compiled codes optimize for its host processor.
Depending on your  license,  these  valuable versatile  codes are independent of MotionGenesis™.

Solve an ODE with on/off if-like statement

Shown below is the MotionGenesis™ command file that solves the ODE:  if( t < 𝛑 ) y' = sin(t) else y' = 0
isTLessThanPi = isPositive(pi-t) % true if t < 𝛑, else 0. sintOrZero = sin(t) * isTLessThanPi % sin(t) if t < 𝛑, else 0. Variable y' = sintOrZero Input tFinal = 4, y = 0 % Arbitrary values. Output t, y, y' ODE() MGSimulateWithIfStatement % Plot MGSimulateWithIfStatement.1[1, 2, 3]

Solve an ODE by auto-generating MATLAB® or C code and pass an argument to that code.

Shown below is the MotionGenesis™ command file to auto-generate the MATLAB® file MGProjectileMotion.m that simulates projectile motion of a baseball. The baseball's initial x (horizontal) and y (vertical) location are specified by the Input command. The OutputPlot command instructs MATLAB® to plot y vs. x.  To run the .m file, start MATLAB® and 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 )
IPBaseballHomeRunBySnagItMitiguyB.jpg
MotionGenesis™ forms dynamics equations.
To auto-generate MGProjectileMotion.c, use ODE() MGProjectileMotion.c( b )  After compiling the C program, run from the operating system by typing: MGProjectileMotion 0.05

Solve an ODE with various initial values.

Shown below is the MotionGenesis™ command file to solve an ODE with various initial values.
Variable y' = cos(t) + sin(y) % Arbitrary ODE. Output t sec, y m, y' m/s % List output quantities (for ODE command). Input y = 3 m % Set initial value for y (for ODE command). %-------------------------------------------------------------------- % Set integration parameters and solve the ODE with initial value y = 3. Input tFinal = 5 sec, tStep = 0.02 sec, absError = 1.0E-07 ODE() ODEWithInitialValueA % Solve the ODE and output results. %-------------------------------------------------------------------- Input y := 0 m % Modify the initial value for y. ODE() ODEWithInitialValueB % Solve the ODE and output results. %-------------------------------------------------------------------- % Optional: Plot results Plot ODEWithInitialValueA.1 [1,2] ODEWithInitialValueB.1 [1,2]