Exploring Advanced Math with Maxima

When I took Calculus in college, my Professor would give us substantial partial credit for test problems that we got wrong for minor arithmetic errors, and rightfully so, too. Sometimes even simple-sounding problems resulted in a full page, or more, of calculations. Simply changing a -1 to a +1 early on in a problem could be completely devastating. Avoiding simple errors like that is what Computer Algebra Systems (CAS) are all about and I've found Maxima to be an extremely powerful CAS program.

Maxima is licensed under the GPL, so it has to be good! However, Maxima is a text based program, which means it's a bit clumsy by today's standards. Fortunately, there are a few front ends which make the program much easier to use and more polished. For this article, I'm using the wxMaxima front end.

Anyone who's taken a high school Algebra class remembers spending endless hours factoring polynomials, multiplying them together, and evaluating them for given values of x. Factoring polynomials always seemed to have an element of guess work built in, and I found it to be quite frustrating. Of course, the teacher spent weeks teaching various ways to factor these formulas, and then they finally introduced the Quadratic Equation, which made factoring second order polynomials almost trivial. For those of you who don't completely geek out over Math, a second order polynomial is one like this: ax^2 + bx + c, where the highest exponent is 2.

Manipulating polynomials with Maxima is very easy, as shown in Figure 1. Here you see 7 steps that I've asked Maxima to perform. My commands are indicated by the “%i” and a step number. The corresponding output is indicated by the “%o” and then the step number. So, (%i90) is my 90th input step and the result is on the line indicated by (%o90). So, at step 90, you see that I entered a simple polynomial equation. Then I asked Maxima to factor it. Here you see that I used a shortcut. The “%” character represents the return value of the previous step. So, when I entered “factor(%);” Maxima retrieved the previous step's value (that polynomial) and factored it. The expected result is on the next line. In step 92, I entered two polynomials multiplied by each other. As you can see, Maxima doesn't immediately do anything special with this formula. But when I asked it to expand the formula, it multiplied the two together, as you can see in step 93. At step 94, I asked Maxima to factor the resulting 4th order polynomial, and behold, we get the original equation back, as we expect. I don't think we even discussed factoring 4th order polynomials in high school Algebra! But we did a lot of evaluating polynomials at a given value of x, which I demonstrate in steps 95 and 96. In step 95, I assign a value to x. In step 96, I re-enter the polynomial, and Maxima calculates it's value at the given value of x.

Figure 1
Figure 1. Manipulating Polynomials

Of course, Maxima can also solve equations, both symbolically, and numerically, as in Figure 2. Here we see that I've asked it to solve a quadratic equation, and it gives me the answer in symbolic form. Then I used the “numer” directive to ask Maxima to give it's answers in numerical form, which it does in step 13. Finally, I ask Maxima to use 25 digits of accuracy, instead of the default 16, and recalculate. Maxima will calculate to any degree of accuracy that you want and can handle arbitrarily large numbers. For example, Maxima can easily handle 2^200, and can even factor (2^200)-1.

Figure 2
Figure 2. Symbolic and Numeric Manipulation

Maxima will even handle calculations that you'd never even think about doing manually. For example, you can ask Maxima to solve a 4th order polynomial equation, and it will give you all 4 solutions, both real and complex. However, in symbolic form, these solutions are often so ugly that they scroll off your screen! But, you can then turn around and ask it to give you the same answers in numerical form, to any degree of accuracy that you need.

Maxima uses either GnuPlot or Xmaxima to plot expressions and can plot 2D, 3D and parametric equations. In Figure 3, I entered a fairly complex expression, and asked Maxima to plot it on a 2D plot. In Figure 4, I took an example from Maxima's documentation and plotted a 3D image. You can rotate this image and view it from any angle you like.

Figure 3
Figure 3. 2D Plot of an Expression

Figure 4
Figure 4. 3D Plot of an Expression

If all Maxima was able to do was manipulate polynomials, it would still be a powerful tool. But Maxima can do much more, as I'll try to demonstrate in Figure 5. Here, in step 16, I asked Maxima to expand a Trigonometric expression using the Double Angle Theorem. Even though I found Trig to be the easiest Math class I ever took, Maxima can manipulate Trigonometric Identities much better than I ever will. In step 17, I asked Maxima to differentiate sin(3x) with respect to x. This is a fairly simple derivative to calculate. There's nothing simple about the derivative I asked for in step 18. If we were performing this calculation manually, we would probably use a combination of the Product Rule and the Quotient Rule, or multiple instances of the Quotient Rule. Then we would have to simplify the result. Either way, “yuck” is a good way to describe the process. Maxima provides us with a nicely formatted answer. In step 19, I asked Maxima to calculate an indefinite integral with respect to x. We see Maxima's answer on the next line. Ya, that's what I came up with; how about you? In step 20, I asked Maxima to calculate the definite integral of the same function as x goes from 0 to 5. Of course, I could have asked for the answer in numerical format but the symbolic form is so much more geeky. In step 21, we see that Maxima can apply L'Hospital's rule to calculate the limit of an expression as x goes to infinity.

Figure 5
Figure 5. More Maxima Manipulations

In step 22, I asked Maxima to calculate the first several terms of the Taylor Series of sin(x) centered around x=0. This is where we start to see a weakness in Maxima. There should have been a factorial in the denominator of each term of that Taylor Series. But Maxima simply gave us the answer in numerical form.

Maxima is also able to manipulate matrices and sets, though I'm not going to demonstrate any of that functionality here. Finally, Maxima has a built-in programming language, though it's not the most elegant language you'll ever see. I show you a short example in Figure 6. Here I assign an initial value to x. Then I calculate the value of an expression 10 times, using the previous result as the next value of x. There are only a few initial values of x for which this expression ever repeats itself. Can you find them?

Figure 6
Figure 5. Built-In Programming Language

As you can see, Maxima is a very sophisticated analysis tool and is adept at solving problems in Algebra, Trigonometry, Linear Algebra, Differential Calculus, and Integral Calculus. It can also calculate using arbitrarily large and precise numbers. With all that capability, you might be tempted to ask why we even need to teach Mathematics anymore and that's the real point of this article. For all it can do, Maxima has no understanding of any of the questions you ask it; it only knows how to calculate answers. The value of teaching and learning Mathematics is in understanding concepts and thought processes. The value in using a tool like Maxima is in removing some of the tedium from the process of Math.

AttachmentSize
Fig1.png37.23 KB
Fig2.png58.2 KB
Fig3.png23.65 KB
Fig4.png33.23 KB
Fig5.png48.85 KB
Fig6.png76.02 KB
cover.png48.04 KB
______________________

Mike Diehl is a freelance Computer Nerd specializing in Linux administration, programing, and VoIP. Mike lives in Albuquerque, NM. with his wife and 3 sons. He can be reached at mdiehl@diehlnet.com

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: Something wrong with accuracy?

Anonymous's picture

No, it's just that Maxima provides a variable, fpprec, that controls the precision of its bigfloat computations and a variable, fpprintprec, that controls the number of digits printed. Here is what the correct output looks like:

(%i18) solve(x^2-5*x-3,x);
(%o18) [x = -(sqrt(37)-5)/2,x = (sqrt(37)+5)/2]
(%i19) %,numer;
(%o19) [x = -.5413812651491097,x = 5.54138126514911]
(%i20) (fpprintprec : fpprec : 25, bfloat(%o18));
(%o20) [x = -5.413812651491098444998421b-1,x = 5.541381265149109844499842b0]

I believe that %o20 agrees with what you expect.

You might also want to look

Anonymous's picture

You might also want to look into Sage, as it's a single python interface that combines a number of math packages including Maxima.

Something wrong with accuracy?

Anonymous's picture

From the logs it looks like SQRT(13) was replaced in step 13 with an approximate value that is accurate to 8 digits only, although the precision of numerical values was set to 16.
In step 15 you set fpprec to 25 digits, but again, the end results are accurate to 7 digits only. (The solutions should be: -.5413812651491098444998421 and 5.5413812651491098444998421).
Is there a way to avoid these problems?

Fantastic Tool

Salvadesswaran Srinivasan's picture

I've always loved maths, especially all the algebra and calculus, but I'd love to see others who hate maths just because of this use this fantastic tool, it might just make them love maths. When I teach(maybe 6-7 years on) I'll recommend this to my students ;)

It sure looks very nice and

Ruben's picture

It sure looks very nice and sophisticated, but how does it compare to the alternatives? And than I mean of course the commercial stuff they use in colleges and things like Wolfram Alpha, which is supposed to have some impressive calculating power as well? When I start my study of Applied Physics later this year, I can maybe find this all out by myself. But can you give a quick comparison maybe?

Btw, great article, very pleasant to read!

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState