Ordinary Differential Equations (basics)

Solve a 2nd-order ODE

The MotionGenesis™ command file below solves a 2nd-order ODE for x(t). The command OutputPlot causes ODE() to Output data and Plot  x(t) vs. t (time).
Variable x'' = cos(2*t) + sin(x) %---------------------------------------------------------- % Set initial values for variables. Input x = 1 m, x' = 0.2 m/s %---------------------------------------------------------- % List output quantities (for ODE command and plotting). OutputPlot t seconds, x meters %---------------------------------------------------------- % Set numerical integration parameters. Input tFinal = 20 sec, tStep = 0.1 sec, absError = 1.0E-7 %---------------------------------------------------------- % Solve ODEs and plot data file MGSolve2ndOrderODE.1 ODE() MGSolve2ndOrderODE
Optional: To instead auto-generate a MATLAB® .m file, change the last line to ODE() MGSolve2ndOrderODE.m
Note: The prime symbol ' denotes differentiation with respect to time t.
x'' = cos(2*t) + sin(x)
The output file MGSolve2ndOrderODE.1 can be plotted with MotionGenesis™ or MATLAB®, etc.

Solve a 1st-order ODE with various initial values.

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

Solve an ODE with on/off if-like statement

The MotionGenesis™ command file below solves the 1st-order 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 sec, y = 0 m % Arbitrary input values. Output t sec, y meters %-------------------------------------------------------------- ODE() MGSolveODEWithIfStatement Plot MGSolveODEWithIfStatement.1 [1, 2] % Plot y vs. time t.

Solve coupled 1st-order ODEs

The MotionGenesis™ command file below solves ODEs for the angular velocity measures  wx, wy, wz  of a rotating rigid body.
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 %---------------------------------------------------------- % Set initial values for variables. Input wx = 0.2 rad/sec, wy = 7.0 rad/sec, wz = 0.2 rad/sec %---------------------------------------------------------- % List output quantities (for ODE command and plotting). OutputPlot t sec, wx rad/sec, wy rad/sec, wz rad/sec %---------------------------------------------------------- % Set numerical integration parameters. Input tFinal = 4 sec, tStep = 0.02 sec, absError = 1.0E-7 %---------------------------------------------------------- ODE() MGCoupledODEs
Optional: To instead auto-generate a MATLAB® .m file, change the last line to ODE() MGCoupledODEs.m
MGSpinStability3DRigidBodyWxyz.jpg
The output file MGCoupledODEs.1 can be plotted with MotionGenesis™ or MATLAB®, etc.
Curious? See 3D spin-stability videos:

MotionGenesis™ also does vector algebra, matrix algebra and solves sets of linear equations or nonlinear equations.
Advanced: Solve additional ODEs and auto-generate MATLAB® C, etc., codes that solve ODEs and take command-line arguments.