Cite as:
Saad, T. "Solving Differential Equations with Mathematica - Roundup".
Weblog entry from
Please Make A Note.
http://pleasemakeanote.blogspot.com/2008/05/solving-differential-equations-with_29.html?m=0
Please Make a Note is a collection of science & technology tips and derivations that will make it easier for research scientists & engineers to perform the various tasks they are faced with. These notes cover a wide range of scientific topics, software, media, and data analysis utilities.
Wednesday, May 28, 2008
Solving Differential Equations with Mathematica - Roundup
Solving Differential Equations with Mathematica - Part IV: Equation Trekker
Since we can only use a single ODE, I will employ Ueda's oscillator model for illustration. The governing ODE is given by
Here's the EquationTrekker code in Mathematica to generate the Poincaré section for Ueda's equationsEquationTrekker[{x''[t] + k x'[t] + x[t]^3 == B Cos[t]}, x, {t, 0, 10000}, PlotRange -> {{-5, 5}, {-5, 5}}, TrekParameters -> {k -> 0.3, B -> 11.5}, TrekGenerator -> {PoincareSection, "SectionCondition" -> Mod[t, \[Pi]], "SectionVariables" -> {x, x'}, MaxSteps -> \[Infinity]}]

Voila!
Once the equation trekker interface opens, you have to click on the little pencil icon at the top and then click inside the plane to specify the initial conditions. Of course, there's much more to say about equation trekker, but I leave that for your curiosity. To change the sampling interval (more or less points), just modify the integration range specified by t. The sampling frequency is specified by the "SectionCondition".
One can also easily generate the phase space using equation trekker. All you have to do is remove the TrekGenerator specification from the code given above, i.e.
EquationTrekker[{x''[t] + k x'[t] + x[t]^3 == B Cos[t]}, x, {t, 0, 100}, PlotRange -> {{-7, 7}, {-8, 8}}, TrekParameters -> {k -> 0.3, B -> 11.5}]

Download Mathematica notebook [right click / save as]
Cite as:
Saad, T. "Solving Differential Equations with Mathematica - Part IV: Equation Trekker".
Weblog entry from
Please Make A Note.
http://pleasemakeanote.blogspot.com/2008/05/solving-differential-equations-with_28.html?m=0
Sunday, May 25, 2008
Solving Differential Equations with Mathematica - Part III: Frequency Domain
(* Generate a table containing the numerical solution *)Voila!
yvalues = Table[(x[t] /. s1)[[1]], {t, Tend}];
(* Apply a discrete Fourier transform on that data and plot it*)
ListLinePlot[Abs[Fourier[yvalues]], PlotRange -> All]
Download Mathematica notebook [right click / save as]
Cite as:
Saad, T. "Solving Differential Equations with Mathematica - Part III: Frequency Domain".
Weblog entry from
Please Make A Note.
http://pleasemakeanote.blogspot.com/2008/05/solving-differential-equations-with_25.html?m=0
Friday, May 23, 2008
Solving Differential Equations with Mathematica - Part II: Phase Space
To generate the 2D phase space, use this simple code
ParametricPlot[Evaluate[{x[t], y[t]} /. s1], {t, 0, Tend}, PlotRange -> All]

To generate the 3D phase space, use the ParametricPlot3D[] as follows
ParametricPlot3D[Evaluate[{x[t], y[t], z[t]} /. s1], {t, 0, Tend},PlotRange -> All]
Voila!Download Mathematica notebook [right click / save as]
Cite as:
Saad, T. "Solving Differential Equations with Mathematica - Part II: Phase Space".
Weblog entry from
Please Make A Note.
http://pleasemakeanote.blogspot.com/2008/05/solving-differential-equations-with_24.html?m=0
Thursday, May 22, 2008
Solving Differential Equations with Mathematica - Part I: Time Series
where x, y, and z are functions of time and sigma, rho, and beta are control parameters determined a priori. One could implement a fourth order Runga-Kutta method with adaptive time stepping to solve the above set of equations (and I would really recommend doing that). But in this article, we will use Mathematica which offers a super neat function called NDSolve[] that performs the numerical integration of ODEs. Without further adue, only a couple of lines of code are required in Mathematica to solve the above system of equations(* Define control parameters *)Voila!
\[Sigma] = 3; \[Beta] = 1; \[Rho] = 10;
(* Define initial conditions for later use *)
x0 = 0; y0 = 1; z0 = 1;
(* Define interval of integration *)
Tend = 20 \[Pi];
(* Lump the initial conditions in one variable *)
initialConditions = {x[0] == x0, y[0] == y0, z[0] == z0};
(* Lump the Lorenz equations in one variable *)
LorenzEquations = {x'[t] == \[Sigma] (y[t] - x[t]),
y'[t] == \[Rho] x[t] - x[t] z[t] - y[t],
z'[t] == x[t] y[t] - \[Beta] z[t],
initialConditions};
(* Use NDSolve to integrate the Lorenz equations *)
s1 = NDSolve[LorenzEquations, {x[t], y[t], z[t]}, {t, 0, Tend}, MaxSteps -> \[Infinity]];
(* Plot the solution *)
Plot[Evaluate[x[t] /. s1], {t, 0, Tend}, PlotRange -> All]
Note that \[Sigma] will automatically convert to the greek symbol sigma. The same applies for the rest. You can also generate the greek letters by pressing escape, typing a letter on the keyboard, and then pressing escape. For example, escape, s, escape will turn into sigma.
Going back to the previous code, the two important statements are the NDSolve[] and the Plot[Evaluate[]].
In the first one, we are solving the Lorenz equations for x[t], y[t], and z[t] from t = 0 to t = Tend with an infinite number of time steps (MaxSteps->Infinity).
As for the Plot[Evaluate[]], the "x[t] /. s1" means replace all x[t] with the data contained in s1, which holds the results of the numerical integration. One could have also chosen to plot y[t] or z[t].
For first or higher order ODEs, it is advisable to get rid of all derivatives by definig them as new variables. This will be helpful for phase space diagrams to be discussed in the next article. For example, if you have the following system (Ueda's oscillator)
it can be converted to
Download Mathematica notebook [right click / save as]
Cite as:
Saad, T. "Solving Differential Equations with Mathematica - Part I: Time Series".
Weblog entry from
Please Make A Note.
http://pleasemakeanote.blogspot.com/2008/05/solving-differential-equations-with.html?m=0