# Maximum Calculus with Maxima

We looked at Maxima in the February 2011 issue to do algebra and rearrange some equations. But those aren't the only tricks up Maxima's sleeve. This month, I describe how Maxima can help with differential equations, but I'm going to leave out some of the intermediate results to save some space.

A lot of science involves figuring out how systems change over time and what causes those changes. When you start looking at changes, and especially rates of change, that is essentially calculus. Calculus and rates of change also are linked to slopes of lines on graphs. When you plot data and find an equation that describes it, you can find the slope of the line by taking the derivative of the equation. Let's look at a falling object and see what theory has to say about it.

You should start by looking at how you get a derivative. Let's say you have the equation:

(%i1) f(x):= 2 + x^2; 2 (%o1) f(x) := 2 + x

You would find the first derivative by calling the function diff, giving it the equation to differentiate along with the variable to differentiate by. So, you would write:

(%i2) answer:diff(f(x),x); (%o2) 2x

Maxima can do differentiation of expressions too. If you have a couple equations, you can derive their ratio with:

(%i3) g(x):= x^(1/2); (%i4) ratio_diff:diff(g(x)/f(x),x); 3/2 1 2 x (%o4) ----------------- - ----------- 2 2 2 2 sqrt(x) (x + 1) (x + 1)

This might be a bit messy to work with, so you might want to refactor it to a more concise form:

(%i5) factor(ratio_diff); 2 3 x - 1 (%o5) - ------------------ 2 2 2 sqrt(x) (x + 1)

Maxima also can handle trigonometric functions, but there are lots of identities you can use to help simplify equations with trig functions in them. By default, Maxima does not try to apply these unless you specifically say so, using special functions. As an example, let's say you have the following equation:

(%i6) diff(sin(x)/(1 + cos(x)),x); 2 sin (x) cos(x) (%o6) ------------- + ---------- 2 cos(x) + 1 (cos(x) + 1) (%i7) factor(%); 2 2 sin (x) + cos (x) + cos(x) (%o7) -------------------------- 2 (cos(x) + 1)

That's still not very simple. If you then apply the function trigsimp, you can force Maxima to apply trigonometric simplification rules to the equation and see what you get:

(%i8) trigsimp(%); 1 (%o8) ---------- cos(x) + 1

You should be aware of some important caveats regarding how Maxima treats trig
functions. The first is that `sin(x)^(-1)`

is the reciprocal of sine, not
arcsine. To get the arcsine, you would use `asin(x)`

. The other is another
trig simplification function, trigreduce. This function is used to reduce
the powers of trig functions by using the multiple angle formulas. For
example:

(%i9) trigsimp(cos(x)^2 + 2*sin(x)^2); 2 (%o9) sin (x) + 1 (%i10) trigreduce(cos(x)^2 + 2*sin(x)^2); cos(2 x) + 1 1 cos(2 x) (%o10) ------------ + 2 (- - --------) 2 2 2

That may not look simpler than what you would get from trigsimp, but it is an easier form of the equation to use with other functions, like integration.

Maxima can apply the chain rule when doing a derivative. Say you have the equation:

(%i11) f(x):= x^3); 3 (%o11) f(x) := x (%i12) depends(x,u)$ (%i13) diff(f(x),u); 2 dx (%o13) 3 x -- du

The line at `%i12`

uses a new function,
`depends`

. This is a way of telling
Maxima that x is a function of u, without explicitly defining a function
describing this relationship. If you decide later that you want to define an
actual equation for this relation, you can use:

(%i14) remove([x,u],dependency); (%o14) done (%i15) x:sin(u); (%o15) sin(u) (%i16) diff(f(x),u); 2 (%o16) 3 cos(u) sin (u)

Along the same lines, Maxima can handle implicit differentiation. Say you have the equation x^2 + y^2 = 25, and you want to find dy/dx. You need to use the depends function I just mentioned to handle this:

(%i17) eqn := x^2 + y^2 = 25; 2 2 (%o17) y + x = 25 (%i18) depends(y,x); (%o18) [y(x)] (%i19) deriv_of_eqn:diff(eqn,x); dy (%o19) 2 y -- + 2 x = 0 dx (%i20) solve(deriv_of_eqn,'diff(y,x)); dy x (%o20) [-- = - -] dx y

The other side of calculus is integration. The basic function to do that in
Maxima is called `integrate`

. This function can do both definite and
indefinite integrals. Indefinite integrals are the symbolic form of
integration you likely learned in school. For example:

(%i21) integrate(x^2,x); 3 x (%o21) - 3

A definite integral actually is evaluated over an interval. This form of an integral can be visualized as the area under the curve defined by the equation you are integrating. To do definite integrals, simply add two arguments giving the start and end points of the interval:

(%i22) integrate(x^2,x,0,1); 1 (%o22) - 3

Putting all these techniques together, you can solve a differential
equation for a given variable—for example, solve dy/dx = f(x) for y. You can do
this by doing all the required algebra and calculus, but you don't
really need to. Maxima has the very powerful function,
`ode2`

, which can do
it in one step. Start with your equation:

(%i23) eq: 'diff(y,x) = sqrt(1/x^2 - 1/x^3); dy 1 1 (%o23) -- = sqrt(-- - --) dx 2 3 x x (%i24) ode2(eq,y,x); 2 2 2 sqrt(x - x) (%o24) y = log(2 sqrt(x - x) + 2 x - 1) - ------------- + %c x

This one function call does the integration and the solve steps and gives you a final answer to the differential equation.

Let's say you're doing an experiment dropping a coin and timing how long it takes to fall. How do you know whether the times you are measuring actually make sense? Let's start with the most basic law: force = mass * acceleration.

The mass of the coin is a constant, so ignore that for now. The force is the force due to gravity, pulling the coin down to the ground, and the acceleration describes the coin's motion due to this force. The force due to gravity is a constant, at least here on Earth, and it depends linearly on the mass, so you can define the force as:

(%i1) force: mass * g; (%o1) g mass

The acceleration also is a constant, because both the mass and the force are constants. Acceleration is simply the rate of change of the velocity, and the velocity is the rate of change of the position, so you can set that up as:

(%i2) depends(y,t); (%o2) [y(t)] (%i3) acceleration: 'diff('diff(y,t),t); 2 d y (%o3) --- 2 dt

Putting it all together, you get:

(%i4) eq_of_motion: force = mass * acceleration; 2 d y (%o4) g mass = mass --- 2 dt (%i5) solve(eq_of_motion, y); 2 d y (%o5) [--- = g] 2 dt

You can see right away that how fast an object falls doesn't depend on the mass at all. Galileo was right! The next step is to do some integrating and see what you end up with:

(%i6) integrate(%,t); dy (%o6) [-- = g t + %c1] dt

At this step, you would be able to find out the velocity (dy/dt) at time t.
The additional term `%c1`

is a constant of
integration. In this case, you can
see that it represents the initial velocity of your penny. One more round of
integration gives this:

(%i7) integrate(%,t); / 2 [ dy g t (%o7) [I -- dt = ---- + %c1 t + %c2] ] dt 2 /

Now you can find the position, y, of your coin at any time, t. Again, a new
constant of integration is introduced, `%c2`

. In this
case, you can see that
this represents the starting height of your coin. But that's not what you
were measuring. You were measuring how long it took the coin to drop a given
distance. So you need to do a bit of rearranging. Because you are dropping
your
coin, you know that the start velocity is 0 (that is,
`%c1=0`

). You can rewrite
things a little to make it a bit clearer:

(%i8) eqn: y = (g * t^2)/2 + y0; 2 g t (%o8) y = y0 + ---- 2 (%i9) solve(eqn,t); y y0 y y0 (%o9) [t = - sqrt(2) sqrt(- - --), t = sqrt(2) sqrt(- - --)] g g g g

There you go. You now have an equation for the time, given a height that your coin is dropping. With this theoretical relation under your belt, you can check to see whether gravity is working correctly in your local lab. If not, you should contact the Nobel committee straightaway.

This only scratches the surface of Maxima's capabilities in dealing with calculus and differential equations, but hopefully, this article gives you a starting point. Happy integrating.

Joey Bernard has a background in both physics and computer science. This serves him well in his day job as a computational research consultant at the University of New Brunswick. He also teaches computational physics and parallel programming.

## Comments

## two little mistake in article?

Yours article is very good, but I'm find some

little problems.

instead of :

(%i11) f(x):= x^3);

shouldn't be?:

f(x):= x^3;

And when I'm writing (%i17) I'm get this error:

(%i17) eqn := x^2 + y^2 = 25;

define: argument cannot be an atom or a subscripted memoizing function; found:

eqn

-- an error. To debug this try: debugmode(true);

I'm using:

Maxima version: 5.22.1

Maxima build date: 21:51 11/8/2010

Host type: i686-pc-linux-gnu

Lisp implementation type: GNU Common Lisp (GCL)

Lisp implementation version: GCL 2.6.7

regards

mb

## Great article! :)

Great article Joey! I use wxMaxima and your article was helpful in expanding my knowledge of this most useful too. Thank you! :)

## http://itshrunk.com/6832c5 Hi

http://itshrunk.com/6832c5

Hi,Dear Ladies and Gentlemen,

1. sport shoes : Jordan ,Nike, adidas, Puma, Gucci, LV, UGG , etc. including women shoes and kids shoes.

2. T-Shirts : BBC T-Shirts, Bape T-Shirts, Armani T-Shirts, Polo T-Shirts,etc.

3. Hoodies : Bape hoody, hoody, AFF hoody, GGG hoody, ED hoody ,etc.

4. Jeans : Levis jeans , Gucci jeans, jeans, Bape jeans , DG jeans ,etc.

http://itshrunk.com/6832c5

http://itshrunk.com/6832c5

Service is our Lift.

enjoy yourself.

thank you!!

::∴★∵**☆．∴★∵**☆．∴★∵**☆．

█████.::∴★∵**☆．∴★∵**☆．

█田█田█::∴★∵**☆．∴★∵**☆．

█田█田█.∴★∵**☆．∴★∵**☆．

█田█田█∴★∵**☆．∴★∵**☆．

█田█田█.★∵**☆．∴★∵**☆．

█████．*******************

◢██□██◣．~~~~~*^_^*

## i cant see the relation?

I know the above article is not an everyday read and i maybe not so clear to us all but i really cant see a relation of your post with the article?

____________________________

tanden bleken

## Maxima resources

Hi, here are a few links to the Maxima project's resources.

There is also an active help list, available as a newsgroup on gmane.

It is unfortunate that the comments thus far have largely centred on Sage. I don't really understand the attention-seeking, for two reasons: Maxima is a core component of Sage; and (as pointed out) Sage is not available to Linux users through their package manager while Maxima is (it is called

LinuxJournal, after all).Let me point out that Maxima has several mature GUI frontends and it is used as a computer-algebra backend in several open-source projects.

## Garrick, shrink below?

Garrick, shrink below?

## Imaxima for Maxima

Those lovers of the beauty of Tex take a look of Imaxima as a frontend for Maxima.

## A nice frontend

I recommend using wxMaxima as a frontend. Is quite simple, but is easier to read the equations produced by it, and if you are a newcomer to Maxima, you have the multiple posible functions in different menus easily available.

## It would be great if Linux

It would be especially great if shining that spotlight would get the Sage devs to figure out how to play well with package management on sane operating systems [1,2].

Pull Your Heads Out

[1] http://fedoraproject.org/wiki/SIGs/SciTech/SAGE

[2] https://help.ubuntu.com/community/SAGE

## Package management

Yes, it is unfortunate that there are no Debian or Fedora packages for Sage.

However, it is very easy to download it from the website, and well worth it.

## no, it is not "unfortunate"

Unfortunate implies some sort of accident. The reason you have to install a whole mess of libraries you probably already have installed blobbed with a UI and glue instead of just the UI and glue is caused directly by incompetence on the part of Sage devs. Sage devs would prefer you download and install from the website, because the software is so unstable that the release rate is ridiculous, and people running packaged versions annoy them with their bug reports. So the Sage devs are unsupportive of packaging efforts.

Well worth it? Hardly. A crappy notebook UI to a bunch of libraries I already use aint worth breaking my distribution.

Sage Upstream is Teh Sux

## Re:

How it would break your distribution is beyond me; Sage is self-contained. If you managed to break your distribution using Sage, it would indicate extreme and determined incompetence on your part.

I guess if you do something specialized and aren't interested in the GUI then Sage isn't for you. Many people do find the all-in-one approach useful, because if you're going to be doing a variety of mathematics it isn't convenient to go to Maxima for symbolics, NumPy for matrixes, R for statistics, NetworkX for graph theory, and Sage for combinatorics and number theory. In contrast, Sage wraps all of this in an easy-to-use Python interface, with a GUI, extremely well-documented source code, and lots of new functionality.

I've used Sage for a variety of projects and for a scientific paper. I have even helped in development, and never had a problem with stability.

So if you can't get past the minor inconvenience of downloading a binary version of Sage from the website, extracting it to a folder, and running it, then that's your problem. But please don't spread FUD about it "breaking my distribution".

## That's 10x more complicated

That is far more complicated than the usual way of installing software, e.g., "yum install NAME" or similar. Why should I look for where to download it? Why should I have to "put it" anywhere? It's not the usual way to install software on many systems, which is a problem in itself; users prefer a single common way of doing things, including installs. It also doesn't automatically upgrade, which is no big deal if Sage is the only system you run, but it's a problem when you run many programs.

This deployment approach is easier for the Sage developers, I understand that. But it's not easier for the end-users.

I sympathize with the Sage developers' issues, too.

## Sage

I would strongly urge people who are interested in doing math to try out Sage (http://www.sagemath.org/). It's very comprehensive, and can do a ton of things, including linear algebra, numerical math, plotting, number theory, combinatorics, and, of course, algebra and calculus (powered by Maxima!).

In addition, the user language is Python! So you can use a real programming language, unlike the specialty languages many math packages offer.

It also offers a notebook interface, which you can run from the browser, for those who want to use a GUI.

It would be great if Linux Journal could spotlight Sage as well.

## Why would one want to use a

Why would one want to use a *programming* language to do interactive math? Languages designed for performing mathematics (e.g. Mathematica or Matlab) are clear, concise and follow standard math syntax. Just like one shouldn't use Matlab for serious programming, one should not use Python for serious interactive math!

Also, *every* math textbook I have seen uses one-based indexing, not zero-based indexing.

## "Languages designed for

"Languages designed for performing mathematics" should read "expensive and slow programs for doing math"

Want to look at a math language, check out octave. Many folks I've supported that used Matlab were very impressed and eventually preferred octave over either matlab or mathematica, and octave is *free* in every sense of the word.

And, regarding "one should not use Python for serious interactive math," I'd like to offer the adage that "best tool is relative to the user, resources, and job at hand" and comment that sometimes being able to do "real" math from a scripting language is not only useful, but *sane* to those of us who normally work with such tools.

## AFAIK, Octave is for

AFAIK, Octave is for numerical, especially matrix-based calculations. The strength of Maxima (and Mathematica) is in symbolic manipulations. Sage is trying to do everything by using other programs (like Maxima) as backends.

If you're interested in symbolic manipulations, there's also Axiom, Yacas, etc.