Solving ODEs using Mathematica

Solving ODEs using Mathematica

I have found that the documentation that comes with Mathematica is not always very helpful. At least it is not very helpful when you want to know the most common operations. One of those is solving systems of first order ordinary differential equations (odes) with initial conditions. I don’t know why but I always spend a little time reading the docs to figure out how to do this when I need this functionality. To help me and perhaps others, I give the Mathematica recipe here.

Here are two examples of how to solve a single ode and a simple system of two odes. The function we will use is DSolve[]. This function takes three arguments:

DSolve[{odes, initial conditions}, {variable names}, independent variable]

Note the curly brackets, they must be present. For example to solve the single ode with initial condition, y(0) = 1

    \[\frac{dy}{dt} = y (k_1 - k_2) - k_2\]

we need to input the following Mathematica command:

DSolve[{y'[t] == y[t] (k1 – k2) – k2, y[0] == 1}, y[t], t]

Note that the dependent variable y is denoted by y[t]. The derivative of the dependent variable is denoted by y'[t]. Also note how the initial condition is entered, y[0] == 1. Don’t forget to use == instead of = in the syntax. The solution yields:

    \[\left\{\left\{y[t]\to \frac{e^{(k_1-k_2) t} k_1+k_2-2 e^{(k_1-k_2) t} k_2}{k_1-k_2}\right\}\right\}\]

To solve a set of first order differential equations with initial equations, for example the two equations:

    \[\frac{dA}{dt} = v_o - k1 A\]

    \[\frac{dB}{dt} = k1 A -  k2 B\]

with initial conditions A(0) = 0 and B(0) = 0 we write the following Mathematica code where we enter the multiple equations inside the curly brackets separated by commas. Also note that because there is more than one dependent variable, we must also put these in curly brackets.

DSolve[{A'[t] == vo - k1 A[t], B'[t] == k1 A[t] - k2 B[t], 
    A[0] == 0, B[0] == 0}, {A[t], B[t]}, t]

This will yield:

    \[A(t)\to \frac{v_o-v_o e^{-k_1 t}}{k_1},B(t)\to \frac{v_o \left(k_1 \left(-e^{-k_2 t}\right)+k_2 \left(e^{-k_1 t}-1\right)+k_1\right)}{k_2 (k_1-k_2)}\]

This entry was posted in General Science Interest, Math. Bookmark the permalink.

5 Responses to Solving ODEs using Mathematica

  1. Maryam says:

    That was a great help. Thanks so much!!

  2. hsauro says:

    Its not so much hard but for some reason I always forget the syntax, particularly setting the initial conditions. Never tried it in Matlab, I presume you mean the Matlab symbolic toolbox. I’ll take you word for it and give it miss….

  3. michael says:

    If you really think solving ODE’s in Mathematica is hard or little confusing, then try to do the same in Matlab. Then you will know what hard is.

  4. hsauro says:

    You’re absolutely right, too much LateX is my head….now fixed.

    Many thanks

  5. Bill Simpson says:

    “we need to input the following Mathematica command:
    DSolve[{y'[t] == y[t] (k1 – k_2) – k_2, y[0] == 1}, y[t], t]”

    Those two “_” are incorrect and will deeply confuse someone trying to follow your directions.

Leave a Reply

Your email address will not be published.