Contrary to what some of my friends might think, I’ve been really sympathetic to the arguments against Mathematica that talk about how it’s not Free and Open Source Software (FOSS). I’m always happy to see what my friends and colleagues produce with FOSS to solve/model/teach/learn physics problems. Python and VPython are used quite a bit, and I’m also really interested in Sage, which puts together lots of FOSS tools in one package.

But, as I think about moving myself off of Mathematica towards those tools, there are a few things that hold me up. I thought I’d post about them to see if others could help me out.

### 1. Less syntax for ODEs

I really like the NDSolve command in Mathematica. You give it the differential equation you want to numerically solve, along with any initial/boundary conditions. You tell it the independent and dependent variables (and their limits) and hit go. With that, you get an interpolation function that can be plotted, integrated, analyzed, and on and on. Whenever I see my friends doing similar work in python, I’m often struck by how much code is necessary to accomplish what NDSolve can do in one line.

Of course, I understand and value the arguments that a) students should know what the code is doing, and b) you can always make your own functions to make it easier to repeat. My (admittedly weak) counter argument comes from the amount of time I’ve spent teaching students how to do this. If we can get to investigating some cool physics problems faster, I’m usually happy to ignore those two good arguments.

### 2. Combining analytic and numeric results

This one has stopped me in my tracks often as I think about transitioning my Theoretical Mechanics class away from Mathematica. This is such a powerful feature of Mathematica, that I treat it like a crutch I feel I can’t do without. It comes down to how Mathematica treats everything in the same manner, so that you can take derivatives of things you wouldn’t normally think of, for example.

Here’s my prototypical example. Let’s say you want to model a complex mechanical system using a Lagrangian approach. Students learn that they need to identify what the relevant variables are, and then they need to construct the kinetic and potential energy of the system. Once you have those, you can construct the Lagrangian (L=Kinetic – Potential) and you can obtain the governing equations of motion via the Euler-Lagrange equation:

where q and represent a particular variable and its time derivative. You’d have lots of those equations if you have lots of variables.

Here’s how I’d do a simple mass on a spring (no gravity) in Mathematica to determine the equation of motion:

m=1; k=100; T=1/2 m x'[t]^2; (* kinetic energy *) U=1/2 k x[t]^2; (* potential energy *) L=T-U; (* Lagrangian *) eom=D[L,x[t]]-D[L,x'[t],t]==0; (* equation of motion *)

I would then put that equation of motion into NDSolve. Now, this is a simple example, and you can do much more complicated things this way. My point is that the derivatives I’m taking in the “L” line are actually kind of complicated. They’re taken with respect to x[t], in one case, and x'[t], in another. I’ve looked around on the Sage forums for how to do similar things, and I’ve been pretty intimidated by the suggested methods. If people could help me figure this out in Python or Sage, I’d be very motivated to try to switch my spring class over.

So, yes, it’s true, I’m actually asking this: Please help me try to wean myself off of Mathematica. I’m not sure whether I can really do it, especially given my facility with it, but I think it would be cool if I could accomplish my teaching/research/fun goals with software that didn’t cost so much. I’m certainly willing to put in the time to learn new syntax, but these two points have really held me back.

Oh yeah, one more thing, you’re free to rub this all in my face if you like (you know who you are).

I haven’t had a chance to check to see if this is fully compatible with your example, but in scipy the relevant command for solving ordinary differential equations is odeint.

Here’s some information on it, including using a Dfun option which is the gradient of your function:

http://docs.scipy.org/doc/scipy/reference/tutorial/integrate.html#ordinary-differential-equations-odeint

Here’s an example of a solution to a damped spring-mass system:

http://docs.sfepy.org/euroscipy-tutorials/tutorials-epfl-unifr/data/odeint-damped-spring-mass.py

Hi Andy. In teaching Python and Mathematica simultaneously I keep on being amazed at how much nicer it is to do certain things on one platform vs. the other. If you were a Python person looking at switching Mathematica, I think you would find some roadblocks of a similar size. I’m really enjoying becoming more proficient at both and feeling like I can pick the best tool for the job, while knowing that I can also use a more awkward tool if needed.

That’s a cool way to think about it, Joss. What have you found that you like python better for?

I like VPython quite a bit more for animations (although Manipulate in Mathematica is much quicker than having to bring controls into VPython). Graphing in general is nicer in Python (especially legends). The structure and white-space (instead of Mathematica’s nearly infinite sets of square brackets) in Python make it much nicer to read student code.

For the syntax is a wash. The endless hunting for appropriate braces and using the proper equals signs in Mathematica has somewhat turned me off to it. I think, eventually, I would begin to catch all these things and I’d be more productive in Mathematica, but it was just quicker for me to learn to use Python. I didn’t have the same issues. Sure, the syntax is a bit longer, but it’s also simpler IMO. As you said, it also prepares students for using a programming language in the future, which I think is a big plus.

I’m looking forward to trying to incorporate Python into courses that I teach in the future, and carefully documenting what makes it challenging. One of the great things about this branch of physics is that there’s very few people looking into how students think about and use computation. It’s going to be a very active area of research in the next 5 years. For me, it will have to be. I’ll need to make tenure!!!

One of the things I’m not up to speed on in python is whether it’ll automatically breakdown high order diff eq’s for you. A simple example: can you directly solve x”[t]==- k x[t] or do you have to say x'[t]==v[t] and v'[t]==-k x[t]? For that example, it’s not really a big deal either way. But for some applications, it would seem onerous to always have to add those intermediate functions (v[t] in this case). Is that possible in python?

As far as I know, you use the second (e.g., x’[t]==v[t] and v’[t]==-k x[t]). http://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.odeint.html#scipy.integrate.odeint

I like Mathematica for ease of importing data, massaging it and plotting it. I can make Mathematica drop pixels of ink just where I want it (if I want to really be picky), but even without exerting maximum control over the Plot function, it makes super nice graphs right off the bat. I’ve tried lots of ways of creating plots, Mathematica’s are most easily made publication quality.

Python and VPython are definitely easier to program in. I get frustrated with programming in Mathematica, but sometimes I do find sweet, super simple ways to do things in Mathematica that make it worthwhile. I used to program and create animations more in Mathematica, but since I don’t so often now, I find it harder than I used to (and the sweet, super simple solutions don’t happen so often as a result).

I’ve noticed some frustration from students about using mathematica to do straight up programming, especially if they have experience with C or something else. The “sweet, super simple” short cuts are nice, of course, but it comes down to how willing you are to find them.

For symbolic stuff the only FOSS option I’ve found is maxima. The gui version wxmaxima uses gnuplot (which I use for publication graphics) to do the plotting. It will even explort equations into LaTeX.

It seems that Sage uses Maxima to do its symbolic manipulation. I haven’t used maxima by itself. though.

Pingback: Summer project: more twirling chains | SuperFly Physics

Pingback: Synchronous classical mechanics brainstorming | SuperFly Physics