Training in Mathematica

On Friday 13th Feb I went to Wolfram Research European Ltd in Oxfordshire for Introductory training* on Wolfram Mathematica. Although I already had some experience with Mathematica I thought that it might be better to have professional guide. Few topics included in presentation sounded very interesting, i.e. parallelisation, usage of GPU, data visualisation and dynamic modifications to formulas. As one might expect in introductory course it was a brief sweep through all possible functions, thus not much was said about mentioned points. It was useful, however, to learn about different approaches or to see what is possible in Mathematica. They provided us with executable notebooks [download below] which are quite helpful to look in for examples. Additionally, we were using Mathematica 10, which I think is much better than version 9. Most helpful is quick tool-hint pop-up with possible argument for typed command and also easier change of writing style.

For later reference, I will mention here some commands and pages:

  • Parallelize is a CPU-based parallelisation. It automatically attempts to divide problem by available cores and if it determines that it is impossible it will solve it in serial.
  • CUDAFunctionLoad and OpenCLFunctionLoad use GPU to execute function. With both commands user have to write code in CUDA/OpenCL, define inputs/outputs and specify blocks’ dimensions.
  • Tutorial on parallel tools in Mathematica.
  • Demonstrations is a webpage with large number of examples. To see them online one might need to download CDF previewer.
  • Examples used in presentation.

* If anyone is interested in attending official courses they are available from here: http://www.wolfram.com/training/courses/ .

Advertisements

Mathematica for solving coupled ordinary differential equation

Probably many know that Wolfram Mathematica is a great tool. I knew it as well, but now I’m actually observing first hand how powerful it really is. It is like with chilli pepper – everyone knows that it is hot, but you unless you taste it you won’t really understand it. My work involves solving and manipulating many ordinary differential equations (ODE) which quite often are coupled. Writing basic script in Python to do that isn’t hard. For simple cases one can use SciPy’s build-in function ode from class integrate (documentation). It isn’t very fast, and writing everything takes some time, but it was still faster than solution I saw for Matlab or C++. However, the winner, in my opinion, is Mathematica with it’s simple structure. I don’t have it often, but looking at code for generating and solving n coupled ODEs and seeing how fast it’s performed makes me simply happy. Really simple!

Below is code to generate n coupled ODEs and parameters for them. Note: Subscript[x, i] is only syntax and could be exchanged with x_i, but for copying purposes I left it in long form.


n = 7;
Do[Do[Subscript[k, i, j] = RandomReal[{0, 1}], {i, 1, n}], {j, 1, n}];
Do[Subscript[w, i] = RandomReal[{2 n, 4 n}], {i, 1, n}];
Do[Subscript[r0, i] = RandomReal[{1, 3}], {i, 1, n}];
Do[Subscript[p, i] = RandomReal[{1, 3}], {i, 1, n}];

eqns = Table[{Subscript[\phi, i]'[t] == Subscript[w, i] + Sum[Subscript[k, i, j] Sin[Subscript[\phi, j][t] - Subscript[\phi, i][t]], {j, 1, n}], Subscript[\phi, i][0] == Subscript[p, i]}, {i, 1, n}];

vars = Table[Subscript[\phi, i][t], {i, 1, n}];

sol = NDSolve[eqns, vars, {t, 0, 2 Pi}];

Whole magic is in function NDSolve (documentation), which solves numerically equations eqns for variables vars in provided range. Now all what’s left is to plot results. Again, very simple.

This code generates plots and display them in column. Output graphs below.

FreqTable = Table[Plot[Evaluate[D[Subscript[\phi, i][t] /. sol, t]], {t, 0, 2 Pi}, GridLines -> {{}, {Subscript[w, i]}}, AxesLabel -> {Time[s], InstFreq [1/s]}], {i, 1, n}];
GraphicsColumn[FreqTable, ImageSize -> Full]

kuramoto