Showing posts with label Mathematica. Show all posts
Showing posts with label Mathematica. Show all posts

Friday, May 15, 2009

Modifying Mathematica Menus

UPDATE - FRIDAY MAY 15, 2009
The same procedure applies for Mathematica 7.x - just look in the 7.0 directory. For convenience, I posted the Mathematica 7.0.1 modified menu file. Of course, there is no guarantee that this will work on your end, so by all means do backup your copy of the MenuSteup.tr file before using this one. You can download it from here.

UPDATE - MONDAY JUNE 02, 2008
As was kindly pointed out by an anonymous comment, Mathematica version 6.0.2 includes the "Evaluate Notebook" menu item. Also, removing the backup files from the MenuSetup directory is a must. - Thanks

In Mathematica 6.x, a couple of nice features are missing, namely, the "Evaluate Notebook" menu command and the placement of the "Quit Kernel" command. First, the "Evaluate Notebook" is completely missing form the "Evaluation" menu - and I don't see the reason for removing such a useful command from the new version. Regarding the "Quit Kernel", which is probably the most used command during a Mathematica session, is now located under Evaluation/Quit Kernel/Local. Instead of keeping that command one level higher (i.e. Evaluation/Quit Kernel), for some reason it was placed under a sub-menu.

Yesterday, I found out that one can modify the whole Mathematica menu, and in the process, add the "Evaluate Notebook" command and modify the location of the "Quit Kernel" command. Thanks to the Walking Randomly blog for sharing this information - you can find the original post here.

So here's what I have done on my end to make my Mathematica workspace fit to my needs.

  1. Locate the file "MenuSetup.tr" - This is usually found somewhere within the "Wolfram Research" directory. A simple search would be enough to find that file. (try C:\Program Files\Wolfram Research\Mathematica\6.0\SystemFiles\FrontEnd\TextResources\Windows\)
  2. Open "MenuSetup.str" with a text editor. In windows, I use wordpad to do that because it keeps the formatting correct and wraps the text quite nicely.
  3. Scroll down to the end of the file and locate the last brace-then-bracket "}]" then add the following before that
    1. Type a comma then go to a new line
    2. Insert the following
      Delimiter,
      Item["Evaluate &Notebook", "EvaluateNotebook"],
      Item["&Quit Local Kernel", "MenuListQuitEvaluators", MenuAnchor->True]
Voila!

You should be able to see two new menu items on the menu bar. Make sure also to backup your original MenuSetup.tr. If nothing shows up on the Mathematica menu, you might have to move the backup copy from the MenuSetup directory.

Cite as:
Saad, T. "Modifying Mathematica Menus". Weblog entry from Please Make A Note. http://pleasemakeanote.blogspot.com/2008/05/changing-mathematica-menus.html

Sunday, June 8, 2008

CPU Time Used in a Mathematica Session

To compute the CPU time used to perform computations/evaluations in Mathematica, simply invoke the following command
TimeUsed[]
Voila!

You can insert it at any location in your Mathematica notebook. It will give you the CPU time in seconds.

Cite as:
Saad, T. "CPU Time Used in a Mathematica Session". Weblog entry from Please Make A Note. http://pleasemakeanote.blogspot.com/2008/06/cpu-time-used-in-mathematica-session.html

Wednesday, May 28, 2008

Solving Differential Equations with Mathematica - Roundup

This is a roundup of the posts for solving differential equations with Mathematica. This was illustrated using the Lorentz equations. The topics that were discussed are as follows:
  1. Part I - Time Series
  2. Part II - Phase Space
  3. Part III - Frequency Domain
  4. Part IV - Equation Trekker

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

Solving Differential Equations with Mathematica - Part IV: Equation Trekker

Download Mathematica notebook [right click / save as]

There is great package in Mathematica 6.x that enables us to solve differential equations with a snap. But that's not all, this package will automatically generate a user interface with real-time control of the parameters in the governing equations. This package is called EquationTrekker and is available in Mathematica 6.x. However, the only limitation of this package is that it works with a single first order or second order ODE. Also, EquationTrekker only displays phase diagrams and Poincaré sections. I have already discussed how one can generate the time series and the phase space diagram for a given dynamical system. In this article, I'll discuss how one can generate the Poincaré section using EquationTrekker.

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 equations
EquationTrekker[{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

Sunday, May 25, 2008

Solving Differential Equations with Mathematica - Part III: Frequency Domain

Download Mathematica notebook [right click / save as]

In this article, we use a Fast Fourier Transform to study a dynamical system in the frequency domain. This conversion is very illuminating on the behavior of the dynamical system as it will point out the frequencies at which the system might become unstable or nonlinear. Using the results obtained in the first article of this series, Mathematica can easily convert the data into the frequency domain. This is accomplished by first generating a table containing the values obtained in the numerical solution and then applying a discrete Fourier transform to that data. The code that does this is given by
(* Generate a table containing the numerical solution *)
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]
Voila!

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

Friday, May 23, 2008

Solving Differential Equations with Mathematica - Part II: Phase Space

Download Mathematica notebook [right click / save as]

In a previous article, I discussed how one can use the NDSolve[] utility in Mathematica to obtain a numerical solution for a set of ODEs using the Lorenz equations as a representative example. In this article, I will show how easy it easy to plot the phase space diagram which represents all possible states for a given system. In the case of the Lorenz equations, the phase space is three dimensional because there are three variables: x, y, and z. But of course, one can always study subsets of the 3D phase space, i.e. x vs y for instance. To accomplish this, we make use of the numerical data generated in the previous article, and employ two function in Mathematica; ParametricPlot[] for 2D and ParametricPlot3D[] for 3D spaces.

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!

Of course, s1 was obtained in Part I of this series and represents the numerical solution for the Lorenz equations.

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

Thursday, May 22, 2008

Solving Differential Equations with Mathematica - Part I: Time Series

Download Mathematica notebook [right click / save as]

In this series, I will summarize some of the quickest methods to solve ordinary differential equations (ODEs hereafter) in Mathematica. To illustrate this, I will make use of the Lorenz dynamical system which is a set of three coupled ODEs. This dynamical system is notorious for its chaotic behavior and has given Lorenz the opportunity to be one of the first scientists who discovered chaos. In the process, I will also illustrate some of the techniques used to investigate chaotic behavior in dynamical systems; specifically, sensitivity to initial conditions, frequency domain transformations using the fast Fourier transform, and Poincaré sections.

The Lorenz equations are given bywhere 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 *)
\[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]
Voila!

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

Sunday, May 18, 2008

Interactive Plots in Mathematica

To study the behavior of an analytical solution that has several control parameters, one would usually plot that function under different combinations of these parameters. There is a super nice function in Mathematica that allows the user to dynamically plot the solution by changing the control parameters using sliders. The function is called Manipulate and here's an example
Manipulate[Plot[Exp[-w x] Cos[a x], {x, 0, 3 Pi}, PlotRange -> Full], {w, 0, 10}, {a, 0,20}]
Voila!
The Manipulate[] function performs a job similar to that of the Table[] function, but with a graphical user interface. In the above code, we are manipulating a plot subject to two control parameters, w and a. The Plot[] function follows the standard Mathematica syntax. Here's a screen recording of how the result looks like



Of course, you can plot as many functions and accomodate as many control parameters as you want. This also works with 3D plots. Of course, you will have to use Plot3D to do that.

For a complete description of the Manipulate function, you can visit its Mathematica documentation page
http://reference.wolfram.com/mathematica/tutorial/IntroductionToManipulate.html

Cite as:
Saad, T. "Interactive Plots in Mathematica". Weblog entry from Please Make A Note. http://pleasemakeanote.blogspot.com/2008/05/interactive-plots-in-mathematica.html

Saturday, May 17, 2008

Animations in Mathematica

Download Mathematica notebook [right click / save as]

A good way of presenting your work is to use animations. This is true when you have an analytical model where you need to explore the behavior of the solution under a change of certain control parameters. There's an easy way of doing this using Mathematica. Here is a sample code in mathematica
(* Define function to be animated *)
f[x_, L_] = Cos[L*x];
(* Create a table of plots *)
theTable = Table[ Plot[{f[x, L]}, {x, -4, 4}], {L, 1, 10}];
(* Export this table into an animated GIF - Replace gif with swf to export as a flash swf file*)
Export["C:\\TestAnimation.gif", theTable, ImageSize->350];
The animation will look like this





Voila!

Of course, you need to set the parameters that are relevant to your animation. You can also animate several functions. In this case, include the functions in the Plot section of the table. For example
(* Define functions to be animated *)
f[x_, L_] = Cos[L*x];
g[x_, L_] = Sin[L*x];
(* Create a table of plots *)
theTable = Table[ Plot[{f[x, L], g[x, L]}, {x, -4, 4}], {L, 1, 10}];
(* Export this table into an animated GIF - Replace gif with swf to export as a flash swf file*)
Export["C:\\TestAnimationCombo.gif", theTable, ImageSize->350];
The animation in this case looks like this




Download Mathematica notebook [right click / save as]

Cite as:
Saad, T. "Animations in Mathematica". Weblog entry from Please Make A Note. http://pleasemakeanote.blogspot.com/2008/05/create-mathematica-swf-animation.html