Physics 209

Differential Equations in Mathematica

 

You have already encountered the results of working with partial differential equations when you studied the RC and LRC transients in first term. In this short set of notes you'll shown some key features of differential equations and how to use Mathematica to help you solve them. We'll use the simple RC transient to introduce you to some of the techniques. The equation governing a discharging capacitor like that encountered in Lab #3 is:

This is a separable differential equation where one gathers everything to do with voltage on one side and everything involving the time on the other,

and then solve by integrating.

The constant const is then determined from some other piece of information, for instance the initial condition V(t=0).

Mathematica offers many important tools for solving differential equations and we'll try two of these using exponential decay as an example. First, start a fresh notebook that includes the package for differential equations.

Clear["Global`*"]
Needs["Calculus`DSolveIntegrals`"]
Needs["Graphics`Graphics`"]

If you suspect that a differential equation might have an analytical solution, a solution that can be expressed as a known function, then you can try DSolve. The differential equation shown above for exponential decay falls into this class of problem and can be solved in Mathematica with the following lines.

Res=1000
Cap=1*10^-6
DSolve[{V'[t] + 1/(Res*Cap)*V[t] == 0}, V[t], t]

Executing these lines gives an output that shows the "replacement rule" for V[t]. In this case, the rule is that subsequent appearances of V[t] will correspond to the exponetial solution to this decay problem. You'll notice in the output that the constant, denoted C[1] is unspecified because we didn't specify an initial condition. The initial condition can be added as an additional constraint on V and then you can plot an actual result.

DSolve[{V'[t] + 1/(Res*Cap)*V[t] == 0, V[0] == 1}, V[t], t]
Plot[Evaluate[V[t] /. %], {t, 0, .01}]

If you discover that a differential equation can't be solved analytically then you have to try a numerical technique. We won't get into the details of how this works just yet, but will simply introduce Mathematica's tool for numerical solution of differential equations NDSolve.

Res=1000
Cap=1*10^-6
NDSolve[{V'[t] + 1/(Res*Cap)*V[t] == 0, V[0] == 1}, V[t], {t, 0, 0.01}]

There are a couple of key differences in how you use this. You'll notice that you must specify the initial conditions so that the numerical algorithm has a starting point. You also must specify the range of t (or whatever your independent variable is) over which the numerical solution will be calculated. Finally, you will notice that the output is no longer some recognizable function, although you can still plot the numerical result in the same way. Just be sure that you don't plot it over a range that is greater than the range chosen when you used NDSolve