A 10 minute tutorial for solving Math problems with Maxima

About 50,000 people read my article 3 awesome free Math programs. Chances are that at least some of them downloaded and installed Maxima. If you are one of them but are not acquainted with CAS (Computer Algebra System) software, Maxima may appear very complicated and difficult to use, even for the resolution of simple high school or calculus problems. This doesn’t have to be the case though, whether you are looking for more math resources to use in your career or a student in an online bachelor’s degree in math looking for homework help, Maxima is very friendly and this 10 minute tutorial will get you started right away. Once you’ve got the first steps down, you can always look up the specific function that you need, or learn more from Maxima’s official manual. Alternatively, you can use the question mark followed by a string to obtain in-line documentation (e.g. ? integrate). This tutorial takes a practical approach, where simple examples are given to show you how to compute common tasks. Of course this is just the tip of the iceberg. Maxima is so much more than this, but scratching even just the surface should be enough to get you going. In the end you are only investing 10 minutes.

Maxima as a calculator

You can use Maxima as a fast and reliable calculator whose precision is arbitrary within the limits of your PC’s hardware. Maxima expects you to enter one or more commands and expressions separated by a semicolon character (;), just like you would do in many programming languages.

(%i1) 9+7;
(%o1) [tex]16[/tex]
(%i2) -17*19;
(%o2) [tex]-323[/tex]
(%i3) 10/2;
(%o3) [tex]5[/tex]

Maxima allows you to refer to the latest result through the % character, and to any previous input or output by its respective prompted %i (input) or %o (output). For example:

(%i4) % - 10;
(%o4) [tex]-5[/tex]
(%i5) %o1 * 3;
(%o5) [tex]48[/tex]

For the sake of simplicity, from now on we will omit the numbered input and output prompts produced by Maxima’s console, and indicate the output with a => sign. When the numerator and denominator are both integers, a reduced fraction or an integer value is returned. These can be evaluated in floating point by using the float function (or bfloat for big floating point numbers):

8/2;
=> [tex]4[/tex]
8/2.0;
=> [tex]4.0[/tex]
2/6;
=> [tex]\displaystyle \frac{1}{3}[/tex]
float(1/3);
=> [tex]0.33333333333333[/tex]
1/3.0;
=> [tex]0.33333333333333[/tex]
26/4;
=> [tex]\displaystyle \frac{13}{2}[/tex]
float(26/4);
=> [tex]6.5[/tex]

As mentioned above, big numbers are not an issue:

13^26;
=> [tex]91733330193268616658399616009[/tex]
13.0^26
=> [tex]\displaystyle 9.1733330193268623\text{ }10^_{+28}[/tex]
30!;
=> [tex]265252859812191058636308480000000[/tex]
float((7/3)^35);
=> [tex]\displaystyle 7.5715969098311943\text{ }10^_{+12}[/tex]

Constants and common functions

Here is a list of common constants in Maxima, which you should be aware of:

  • %e – Euler’s Number
  • %pi – [tex]\displaystyle \pi[/tex]
  • %phi – the golden mean ([tex]\displaystyle \frac{1+\sqrt{5}}{2}[/tex])
  • %i – the imaginary unit ([tex]\displaystyle \sqrt{-1}[/tex])
  • inf – real positive infinity ([tex]\infty[/tex])
  • minf – real minus infinity ([tex]-\infty[/tex])
  • infinity – complex infinity

We can use some of these along with common functions:

sin(%pi/2) + cos(%pi/3);
=> [tex]\displaystyle \frac{3}{2}[/tex]
tan(%pi/3) * cot(%pi/3);
=> [tex]1[/tex]
float(sec(%pi/3) + csc(%pi/3));
=> [tex]3.154700538379252[/tex]
sqrt(81);
=> [tex]9[/tex]
log(%e);
=> [tex]1[/tex]

Defining functions and variables

Variables can be assigned through a colon ‘:’ and functions through ‘:=’. The following code shows how to use them:

a:7; b:8;
=> [tex]7[/tex]
=> [tex]8[/tex]
sqrt(a^2+b^2);
=> [tex]\sqrt{113}[/tex]
f(x):= x^2 -x + 1;
=> [tex]x^2 -x + 1[/tex]
f(3);
=> [tex]7[/tex]
f(a);
=> [tex]43[/tex]
f(b);
=> [tex]57[/tex]

Please note that Maxima only offers the natural logarithm function log. log10 is not available by default but you can define it yourself as shown below:

log10(x):= log(x)/log(10);
=> [tex]\displaystyle log10(x):=\frac{log(x)}{log(10)};[/tex]
log10(10)
=> [tex]1[/tex]

Symbolic Calculations

factor enables us to find the prime factorization of a number:

factor(30!);
=> [tex]\displaystyle 2^{26}\,3^{14}\,5^7\,7^4\,11^2\,13^2\,17\,19\,23\,29[/tex]

We can also factor polynomials:

factor(x^2 + x -6);
=> [tex](x-2)(x+3)[/tex]

And expand them:

expand((x+3)^4);
=> [tex]\displaystyle x^4+12\,x^3+54\,x^2+108\,x+81[/tex]

Simplify rational expressions:

ratsimp((x^2-1)/(x+1));
=> [tex]x-1[/tex]

And simplify trigonometric expressions:

trigsimp(2*cos(x)^2 + sin(x)^2);
=> [tex]\displaystyle \cos ^2x+1[/tex]

Similarly, we can expand trigonometric expressions:

trigexpand(sin(2*x)+cos(2*x));
=> [tex]\displaystyle -\sin ^2x+2\,\cos x\,\sin x+\cos ^2x[/tex]

Please note that Maxima won’t accept 2x as a product, it requires you to explicitly specify 2*x. If you wish to obtain the TeX representation of a given expression, you can use the tex function:

tex(%);
=> $$-\sin ^2x+2\,\cos x\,\sin x+\cos ^2x$$

Solving Equations and Systems

We can easily solve equations and systems of equations through the function solve:

solve(x^2-4,x);
=> [tex]\displaystyle \left[ x=-2 , x=2 \right][/tex]
%[2]
=> [tex]x=2[/tex]
solve(x^3=1,x);
=> [tex]\displaystyle \left[ x={{\sqrt{3}\,i-1}\over{2}} , x=-{{\sqrt{3}\,i+1}\over{2}}  , x=1 \right][/tex]
trigsimp(solve([cos(x)^2-x=2-sin(x)^2], [x]));
=> [tex]\displaystyle \left[ x=-1 \right][/tex]
solve([x - 2*y = 14,  x + 3*y = 9],[x,y]);
=> [tex]\left[ \left[ x=12 , y=-1 \right]  \right][/tex]

2D and 3D Plotting

Maxima enables us to plot 2D and 3D graphics, and even multiple functions in the same chart. The functions plot2d and plot3d are quite straightforward as you can see below. The second (and in the case of plot3d, the third) parameter, is just the range of values for x (and y) that define what portion of the chart gets plotted.

plot2d(x^2-x+3,[x,-10,10]);

2dplot.png

plot2d([x^2, x^3, x^4 -x +1] ,[x,-10,10]);

many_2dplot.png

f(x,y):= sin(x) + cos(y);
plot3d(f(x,y), [x,-5,5], [y,-5,5]);

3dplot.png

Limits

limit((1+1/x)^x,x,inf);
=> %[tex]e[/tex]
limit(sin(x)/x,x,0);
=> [tex]1[/tex]
limit(2*(x^2-4)/(x-2),x,2);
=> [tex]8[/tex]
limit(log(x),x,0,plus);
=> [tex]-\infty[/tex]
limit(sqrt(-x)/x,x,0,minus);
=> [tex]-\infty[/tex]

Differentiation

diff(sin(x), x);
=> [tex]\displaystyle cos(x)[/tex]
diff(x^x, x);
=> [tex]\displaystyle x^{x}\,\left(\log x+1\right)[/tex]

We can calculate higher order derivatives by passing the order as an optional number to the diff function:

diff(tan(x), x, 4);
=> [tex]\displaystyle 8\,\sec ^2x\,\tan ^3x+16\,\sec ^4x\,\tan x[/tex]

Integration

Maxima offers several types of integration. To symbolically solve indefinite integrals use integrate:

integrate(1/x, x);
=> [tex]\displaystyle log(x)[/tex]

For definite integration, just specify the limits of integrations as the two last parameters:

integrate(x+2/(x -3), x, 0,1);
=> [tex]\displaystyle -2\,\log 3+2\,\log 2+{{1}\over{2}}[/tex]
integrate(%e^(-x^2),x,minf,inf);
=> [tex]\sqrt{\% pi}[/tex]

If the function integrate is unable to calculate an integral, you can do a numerical approximation through one of the methods available (e.g. romberg):

romberg(cos(sin(x+1)), x, 0, 1);
=> 0.57591750059682

Sums and Products

sum and product are two functions for summation and product calculation. The simpsum option simplifies the sum whenever possible. Notice how the product can be use to define your own version of the factorial function as well.

sum(k, k, 1, n);
=> [tex]\displaystyle \sum_{k=1}^{n}{k}[/tex]
sum(k, k, 1, n), simpsum;
=> [tex]\displaystyle {{n^2+n}\over{2}}[/tex]
sum(1/k^4, k, 1, inf), simpsum;
=> [tex]\displaystyle {{\%pi^{4}}\over{90}}[/tex]
fact(n):=product(k, k, 1, n);
=> [tex]fact(n):=product(k,k,1,n)[/tex]
fact(10);
=>  [tex]3628800[/tex]

Series Expansions

Series expansions can be calculated through the taylor method (the last parameter specifies the depth), or through the method powerseries:

niceindices(powerseries(%e^x, x, 0));
=> [tex]\displaystyle \sum_{i=0}^{\infty }{{{x^{i}}\over{i!}}}[/tex]
taylor(%e^x, x, 0, 5);
=> [tex]\displaystyle 1+x+{{x^2}\over{2}}+{{x^3}\over{6}}+{{x^4}\over{24}}+{{x^5}\over{120 }}+\cdots[/tex]

The trunc method along with plot2d is used when taylor’s output needs to be plotted (to deal with the [tex]+\cdots[/tex] in taylor’s output):

plot2d([trunc(%), %e^x], [x,-5,5]);

taylor.png

I hope you’ll find this useful and that it will help you get started with Maxima. CAS can be powerful tools and if you are willing to learn how to use them properly, you will soon discover that it was time well invested.

122 Comments

  1. Josh June 4, 2007
  2. Will June 4, 2007
  3. bastianazzo June 4, 2007
  4. Beni June 4, 2007
  5. dzer0 June 4, 2007
  6. Sexy Girl June 5, 2007
  7. Bogititus June 5, 2007
  8. jasoncrowther June 5, 2007
  9. budlight June 5, 2007
  10. unikuser June 5, 2007
  11. Birch Leafminer June 5, 2007
  12. Ian June 5, 2007
  13. rwinston June 5, 2007
  14. Will June 5, 2007
  15. Antonio June 5, 2007
  16. easan June 5, 2007
  17. Antonio June 5, 2007
  18. DaedriX June 5, 2007
  19. Machuidel June 5, 2007
  20. mart1n June 12, 2007
  21. alex June 19, 2007
  22. sewlai July 17, 2007
  23. Me July 21, 2007
  24. Jacobus August 15, 2007
  25. JiK September 7, 2007
  26. rajendra September 7, 2007
  27. Antonio Cangiano September 8, 2007
  28. Mz. DiC3 October 3, 2007
  29. Tom Carnevale October 28, 2007
  30. Heraldo November 28, 2007
  31. Hr.stein November 29, 2007
  32. Antonio Cangiano November 29, 2007
  33. Hr.stein November 30, 2007
  34. ENYI JOHNSON December 8, 2007
  35. Alex Tzeros December 9, 2007
  36. Richard January 13, 2008
  37. Jeffrey S Fox January 23, 2008
  38. macosala February 2, 2008
  39. Baha' Alsaify February 4, 2008
  40. Ray Pereda February 12, 2008
  41. Javed Alam February 24, 2008
  42. Dotan Cohen March 3, 2008
  43. zorzal March 7, 2008
  44. Jon March 25, 2008
  45. BWB March 27, 2008
  46. J.C.Pizarro April 7, 2008
  47. J.C.Pizarro April 7, 2008
  48. magnus April 19, 2008
  49. Lim Ee Hai April 30, 2008
  50. Sylvia May 13, 2008
  51. Antonio Cangiano May 13, 2008
  52. Bilge Tutak June 5, 2008
  53. alfahd June 12, 2008
  54. yeswanth June 15, 2008
  55. Malte June 24, 2008
  56. Giorgio June 30, 2008
  57. Gerry August 17, 2008
  58. Joseph August 20, 2008
  59. Martin Ettl September 10, 2008
  60. required September 19, 2008
  61. Angelo October 3, 2008
  62. pascal October 25, 2008
  63. jane October 26, 2008
  64. Abdurrahman November 20, 2008
  65. noname December 9, 2008
  66. John Simmonds December 16, 2008
  67. Zxoch.m December 25, 2008
  68. Uriel Avalos January 15, 2009
  69. Gijs January 23, 2009
  70. zslevi March 6, 2009
  71. Francesc March 14, 2009
  72. Lucas Gallindo March 26, 2009
  73. phil March 27, 2009
  74. Mira March 31, 2009
  75. abhijit April 15, 2009
  76. Rudolf Repges May 10, 2009
  77. Hmm May 18, 2009
  78. Kyle Thomas May 30, 2009
  79. John Simmonds June 4, 2009
  80. Chris June 7, 2009
  81. Mike June 10, 2009
  82. Alex September 11, 2009
  83. Hank September 20, 2009
  84. Larry Dickson September 25, 2009
    • nic November 8, 2014
  85. Antonio Cangiano September 25, 2009
  86. Alexandros September 26, 2009
  87. samuel measho October 14, 2009
  88. David Friedman October 16, 2009
    • paulb September 12, 2011
  89. Rob November 14, 2009
    • jerzy March 10, 2012
  90. Ivan Belash December 16, 2009
  91. AKE February 22, 2010
  92. A Rubens de Castro March 24, 2010
  93. Alexander April 23, 2010
  94. Ricardo M August 24, 2011
  95. synhedionn September 6, 2011
  96. Ragbir Chana November 11, 2011
  97. Derek Pilous March 5, 2012
  98. jerzy March 10, 2012
  99. RockyRoad April 3, 2012
  100. M Kanagasabapathy May 6, 2012
  101. phobos May 8, 2012
  102. KSO May 8, 2012
    • Kevin Gregson November 12, 2012
  103. albert September 17, 2012
  104. Dante May 20, 2013
  105. Ahmed Fasih June 28, 2013
  106. mahtab September 18, 2013
  107. Huen Yeong Kong September 28, 2013
  108. Robert Dodier July 6, 2014
  109. Jim Mooney August 27, 2014
  110. Víctor August 28, 2015
  111. fetter_keks November 4, 2015
  112. Jonse Ketela February 27, 2017
  113. Jonse Ketela February 27, 2017
  114. tibo May 12, 2017
  115. Roger October 22, 2019
    • Dorian February 29, 2020
  116. Stephane October 19, 2023

Leave a Reply