I am going to show an example of how to use Mathematica to solve ordinary differential
equations.
I am going to pick a particularly simple example to show the bare bones approach, and then
subsequent screencasts we'll add in some of the details to make graphs look nicer, and
adding comments.
We're looking at a constant density isothermal batch reactor, and the reaction is second-order.
We're given initial conditions, we can write down the mass balances for this system, which
are differential equations, change in a concentration of A is the rate constant times the concentration
of A and the concentration of B, and then the change in the concentration of B is pretty
similar, except we have a stoichiometric coefficient of two.
The initial conditions, C(A(0))=10, and C(B(0))=12, and the only other information we need for
the solution is the rate constant, 0.1.
I purposely not used units in this calculation to simplify as much as possible because what
we want to concentrate on is how the write the Mathematica program to solve these two
simultaneous differential equations.
To solve the differential equations in Mathematica I go to "File", "New" and opened a notebook,
and then I am going to type in an equation that says my solution to the differential
equation is obtained using the NDSolve function that's built into Mathematica, and it's important
N,D, and S are capitalized, and then we need square brackets that contain the various parameters,
including the equation.
We need to include the equation now in these, what I'll call squiggly brackets.
We're going to write the first equation, C(A) is my dependent variable for concentration.
All my dependent variables have to indicate the independent variable in brackets, in this
case it's t, and then I need to use double equal signs in these equations, and then I
can type in using * for multiplication.
Again when I write C(A), and likewise when I write C(B) I need to indicate the dependent
variables.
With a comma I am going to hit enter here just to put the next equation on a different
line, and I've left one important part out.
Prime indicates this is a derivative, so the left side is now the derivative concentration
of A with respect to time.
I have essentially the same type equation for B, so here's the equation for B.
The only difference is we have a stoichiometric coefficient of two here.
Again a comma.
New line where I am going to indicate the initial condition.
This is A evaluated at t=0, double equals signs, value of 10, and then likewise B evaluated
at 0, brackets to indicate that, a double equal sign, and a value of 12.
So now I've entered the equations, including initial conditions within these curly brackets.
I need a comma here.
I am going to use enter again to make it clear any list needs to be in these squiggly brackets,
and here I am listing my dependent variables C(A) and C(B), a comma, and then again another
list where I am going to list my dependent variable, the initial time, and the final
time that I want to do the integrations.
Now I have everything that I need to solve this differential equation, I am going to
put a semicolon here to indicate we're not going to do the calculation yet.
What we want to do is plot the output, and the function is named Plot, again square brackets
are needed.
Again a list within square brackets of what variables I am plotting, in this case I am
going to plot C(A) and C(B), and so I use a comma to separate them.
I have to indicate the independent variable, and then the notation in Mathematica to refer
to the data is as follows: a slash, a period, and now the name of the equation that I am
plotting, followed by a comma, again a list that says t is my independent variable, and
I want to plot it from 0 to 5.
If I want now Mathematica to do the calculations I use Shift+Enter.
Here's the output from Shit+Enter, it's plotting concentration versus time.
I did not put in any of the options to label the x and y axis, make the graphs look nicer,
just the very basics to show how to solve more than one differential equation simultaneously
using Mathematica.
Again, an initial value problem for ordinary differential equations.