Solving ODEs with Mathematica Anchors: Scandiumδ Jocelyn Anleitner Diane Feldkamp Mathematica Basics Purpose: Mathematica solves difficult mathematical formulas Focus: solving ordinary differential equations (ODEs) of 1st and higher order using Mathematica dy 1st Order ODE: f ( x, y ) dx dzx Higher Order ODE: f ( x, y ) z dy Mathematica Syntax Details about important syntax for Mathematica can be reviewed on the wiki page titled “Solving ODEs with Mathematica” For entering ODEs, use double equal signs (==) to define the functions When finished with a line, use “Shift”+ “Enter” for Mathematica to compute the answer Dsolve and NDSolve For ODEs without initial conditions, use Dsolve Type of Equation Syntax Example Equation(s) Dsolve for Example One ODE Dsolve[eqn,y,x] y''+16y=0 Dsolve[y''[x]+16y[x]==0,y,x] Multiple ODEs Dsolve[{eqn1,eqn2,…},{y1,y2,…},x] y1'-y2-x=0 y2'-y1-1=0 Dsolve[{y1'[x]-y2[x]-x==0,y2'[x]-y1[x]1==0},{y1,y2},x] For ODEs with initial conditions, use NDSolve Type of Equation Syntax Example Equation(s) Dsolve for Example One ODE NDSolve[{eqn,i1,i2…},y,{x,xmin,xma x}] y''+16y=0,y(0)=1, y'(0)=0 NDSolve[{y''[x]+16y==0,y[0]==1,y'[0]==0},y,{x, 0,30}] Multiple ODEs NDSolve[{eqn1, eqn2, ..., i1, i2, …},{y1,y2},{x,xmin,xmax}] y1'-y2-x=0 y2'-y1-1=0 y1(0)=0,y2(0)=0 NDSolve[{y1'[x]-y2[x]-x==0,y2'[x]-y1[x]1==0,y1[0]==y2[0]==0},{y1,y2},{x,20}] Example: Semi-batch Reactor (Worked Out Example 2) Reaction: AB Rate Law: rA Mole Balance: Given Variables: k 2.7 kC A 1 CB rV dX A dt N A0 N (1-X) Stoichiometry: C A = A0 V0 0t CB = N A0 X V0 + 0 k(1-X)(V0 + 0 t) dX Combine: = dt N A0 X V0 5 0 0.05 N A0 2500 X0 0 First-order ODE with Initial Conditions Use NDSolve in Mathematica Entering in Mathematica Open Mathematica (Version shown here is Mathematica 6) Use the following steps to solve the ODE in Mathematica: 1. Input ODE 2. Define given variables Entering in Mathematica (cont.) 3. Define a variable to NDSolve and enter inputs X is conversion, and X[0] is entered as 0.0001 instead of 0 so that the ODE will not be undefined 4. Plot the solution Sources Solving ODEs with Mathematica (wiki): http://controls.engin.umich.edu/wiki/index.php /Solving_ODEs_with_Mathematica