Maxima for maxima (and minima) of functions z=f(x,y)

 

surfacecontours

Together with the 3D graphing capabilities of Maxima, we can bring  symbolic differentiation and the numerical solver to bear when we seek local extrema of a surface.

Here is a link to the html export of a wxMaxima session where I work on two examples from my multivariable calculus class.  And here is the wxMaxima session.

:=, ”(), define and div, grad, curl

I recently posted about : and :=  for defining functional expressions.  I’m starting to enjoy these emoji-like constructions 😉

This is another  colon-equals post.  This time for defining functions involving the maxima differentiation command diff.

Notice below that if we define a function with :=, the naive use of :=diff doesn’t produce a derivative with the expected results upon evaluation.

colonequaldiff

In fact, it’s a good thing that :=diff works like that.  The error with fp(3) above comes from the fact that we’ve actually defined an operator that differentiates the function with respect to the argument we pass…in the case above, differentiating with respect to the symbol u makes sense, while differentiating with respect to the constant 3 doesn’t.

So how to make the derivative function do what we want?  Two ways, that are subtly different, in ways I’m not completely sure of.  More about that when I learn more :-).

First is define,

definediff

Also you can use    ”()       quote-quote with parens around the whole right hand side:

quotequotediff

I used define to write functions for vector valued 3D curves in an earlier post.   In figuring this out, I also learned that the :=diff form is really useful.  Below are three little functions in which I use :=diff to define the vector calculus operators grad, div and curl.  Notice that we pass the function f as an argument, and the :=diff form allows Maxima to differentiate them behind the scenes and return the results of the grad, div, and curl operators as you’d expect. These versions of div, grad and curl behave differently, and for me more as expected, than the functions of those names included in the Maxima vect package.  You can download the .mac file here.

/* Three Maxima functions for the multivariable calculus operators  grad, div, and curl
TheMaximaList.org, 2016
*/

grad(f,x,y,z):=[diff(f,x),diff(f,y),diff(f,z)]$

div(f,x,y,z):=diff(f[1],x)+diff(f[2],y)+diff(f[3],z)$

curl(f,x,y,z):=[ diff(f[3],y)-diff(f[2],z),
diff(f[1],z)-diff(f[3],x),
diff(f[2],x)-diff(f[1],y) ]$

Here is a screenshot showing how to call these functions:

divgradcurl

$latex \int x^2 dx$

I’m testing the latex math-mode utility in WordPress.com.  The first thing to know is that based on the way the title of this post appears, the latex utility doesn’t work in post titles.

The rub here is that MathJax would be the natural solution, but WordPress.com isn’t compatible with JavaScript.  

That leaves us with $, followed by the word latex, followed by my latex markup, followed by a closing $ , which I illustrate  below with a screenshot because I don’t know how to quote a string verbatim in WordPress:

preform

This works OK as inline style math \int x^2 dx and I think we could simulate display math mode like this with manual paragraph centering and with the optional size argument &s=1 inside the dollar signs (the default size is 0)

\int x^2 dx

and then back to inline for f(x).

And there it is.

The official guide to the  WordPress \LaTeX utility  can be found here.

Functions, :, and :=

I’m really posting this for myself to act as a big paperweight.  Whenever I forget about :=  and : for functions in Maxima, I can refer to this page.

The gist:

  • g(x):=x^2  results in a function you can evaluate as g(3)
  • f: x^2 results in an expression you can evaluate with ev(f,x=3)
  • ev works with both:  ev(g(x),x=3)  or ev(f,x=3)
  • also subst(x=3,f) and subst(x=3,g(x))
  • based on our definition, g doesn’t have a meaning…only  g(x) does
  • likewise, f(3) doesn’t have a meaning here…only the symbol f does
  • finally h:=x^2 gives an error — explicit dependent variable needed with :=

colonequals

subst

colonequalserror

A few notes about exporting to .tex in Windows

23 March, 2016 Update!  The following was true for my windows installation of version wxMaxima 15.8.1-git.  I’ve just updated to wxMaxima 15.8.2 64 bit windows version and all is as expected — put TeX: at the beginning of the each text cell, then write latex like a boss.  I’m still not using dollar signs, for compatibility with mathJax when I export to html format 🙂


I really became a believer in the usefuless of Maxima as a productive part of my (and my students) workflow because of the easy way wxMaxima allows the user to document their work in an html or pdf (via pdflatex) document that include LaTeX markup for beautiful mathematical notation.

On my linux machine (Ubuntu 14.04) exporting my wxMaxima session to .tex behaves pretty nicely — maybe I’ll make a future post about that.  In particular, simply putting the string “TeX:” at the beginning of the document allows free use of LaTeX markup to be correctly rendered in the exported .tex document.  This doesn’t work in my Windows version, as well as a few other things described here.

In this post, I’ll describe what I’ve been doing to work around the problems in .tex export in Windows while being consistent with typesetting markups that make for good-looking HTML exports as well.

Goodbye dollar signs

After 25 years of using $…$ for inline math mode and $$…$$ for basic display math mode in LaTeX, I’ve begun using \( … \)  and \[ … \] respectively, because MathJax  (which can be a part of a delicious HTML export from wxMaxima) doesn’t respond to $…$, probably because the dollar sign has other non-LaTeX uses (!)   Another good reason to avoid $…$ in writing  wxMaxima documents is that dollar sign has a specific purpose of suppressing output for a Maxima command, although that problem goes away after the second of the fixes I describe below.

While avoiding $ for LaTeX math mode in wxMaxima documents doesn’t solve all the problems with export to .tex, it does reduce the number of post-processing fixes I need to do to the .tex file to get it to properly compile.

First, my wxMaxima document, with text cells that contain math formulas is shown in this screenshot:

maximaconsolecapture

After export to .tex , I get a file that includes the following latex source snippet.  Notice where I’d like inline math, there is in every case \ensuremath{\backslash}.

texsourcecapture

What is \ensuremath{\backslash} and why does wxMaxima insist on it?

I don’t have an answer for what or why, but it turns out all those \ensuremath{\backslash} statements don’t compile in pdfLaTeX.  A lot of error messages emanate from pdfLaTeX.  The resulting pdf is given below.  Notice the \( isn’t recognized as the start of LaTeX inline math.  The compiler gets the hint about math mode when it sees the superscript, but the \ensuremath{\backslash} gets in the way again and again:

maximapdfcapture

Yuck!  And all I really wanted in that latex source was \(  and \)…

Find and Replace All

I edited the .tex source with a find-replace-all every occurrence of \ensuremath{\backslash} with \    and the result is much nicer:

pdftextmathsnippet

However, there are still a bunch of pdflatex errors from the lines corresponding to the maxima input and output. Turns out anything that LaTeX finds in the maxima that wants to be in math mode, in this case the superscript x^2, results in an “inserted missing $”, kind of error.  Lots of them, in fact.

You can see that in the blue text in the graphic above:  the first occurrence of the variable x is not in math mode, then everything after the ^ is, which then makes the sin(x) appear incorrectly because latex would have wanted \sin, which wouldn’t make sense as a Maxima command.

Maxima input looks better verbatim

To remedy the compiler errors and, in my opinion, make the maxima input clearer for the reader than it would have been if the LaTeX would have somehow known what to make of it, I further edited the latex code, inserting the LaTeX “verbatim” command \verb in each occurrence of maxima input:

texsourcecaptureverbatim

And the resulting pdfLaTeX compiled pdf looks great:

pdfgoodcapture