Ordinary Differential Equations (advanced)

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

The MotionGenesis™ command file below auto-generates the MATLAB® file MGProjectileMotion.m that solves ODES() for a baseball's projectile motion. The command OutputPlot instructs MATLAB® to plot y vs. x.
Constant m = 145 grams, g = 9.8 m/s^2, b Variable x'' = -b*x'/m % Horizontal location. Variable y'' = -g - b*y'/m % Vertical location %---------------------------------------------------------- % Set initial values for variables. Input x = 0 m, x' = 44.7*cosDegrees(30) m/s Input y = 0 m, y' = 44.7*sinDegrees(30) m/s %---------------------------------------------------------- % List output quantities (for ODE command and plotting). OutputPlot x meter, y meter %---------------------------------------------------------- % Set numerical integration parameters. Input tFinal = 3.8 sec, tStep = 0.02 sec, absError = 1.0E-7 %---------------------------------------------------------- % Auto-generate MATLAB code (or C, Fortran, etc.). ODE() MGProjectileMotion.m( b ) % Pass the value of b.
To run the .m file, start MATLAB® and type MGProjectileMotion( 0.05 )
IPBaseballHomeRunBySnagItMitiguyB.jpg
To instead auto-generate the C code MGProjectileMotion.c, use ODE() MGProjectileMotion.c( b )  After compiling the C program, run from the operating system by typing: MGProjectileMotion 0.05

Solve coupled 2nd-order ODEs

The MotionGenesis™ command file below solves ODEs for a swinging-spring. The command OutputPlot causes ODE() to Output data and Plot θ vs. t (time).
Constant m = 1 kg, g = 9.8 m/s^2, k = 100 N/m, Ln = 0.3 m Variable y'' % Spring stretch, 1st & 2nd derivatives. Variable theta'' % Swing angle, 1st & 2nd derivatives. y'' = g * cos(theta) + (Ln+y) * theta'^2 - k*y/m theta'' = -(g*sin(theta) + 2*theta'*y') / (Ln+y ) %---------------------------------------------------------- % Set initial values for variables. Input y = 0.2 m, y' = 0, theta = 1 deg, theta' = 0 %---------------------------------------------------------- % List output quantities (for ODE command and plotting). OutputPlot t sec, theta degrees %---------------------------------------------------------- % Set numerical integration parameters. Input tFinal = 15 sec, tStep = 0.02 sec, absError = 1.0E-7 %---------------------------------------------------------- ODE() MGSwingingSpring
Optional: To instead auto-generate a MATLAB® .m file, change the last line to ODE() MGSwingingSpring.m
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™.

MotionGenesis™ also does vector algebra, matrix algebra and solves sets of linear equations or nonlinear equations.
Simpler: Check out solve ODE basics.