/* [wxMaxima batch file version 1] [ DO NOT EDIT BY HAND! ]*/ /* [ Created with wxMaxima version 19.05.7 ] */ /* [wxMaxima: title start ] 0101intro.wxm [wxMaxima: title end ] */ /* [wxMaxima: comment start ] Maxima by Example, Ch. 1, Introduction to Maxima Ted Woollett, fall 2020. http://web.csulb.edu/~woollett/mbe.html We are using the 5.43.0 version of wxMaxima here. (19.05.7) [wxMaxima: comment end ] */ /* [wxMaxima: section start ] Preface [wxMaxima: section end ] */ /* [wxMaxima: comment start ] This worksheet is an introduction partly based on the worksheet 0101intro.wxmx downloaded [ and heavily modified] from the webpage: http://www.wxmaximaecon.com/about-the-book/exercises/ which contains multiple worksheets which supplement the text: Microeconomic Theory and Computation: Applying the Maxima Open-Source Computer Algebra System, by Mike Hammock and Wilson Mixon, Springer, 2013. Their home page, called wxMaxima in Economics, Applying the open-source computer algebra program to economic analysis, is at: http://www.wxmaximaecon.com/ -------------------------------------------------------------------------- [wxMaxima: comment end ] */ /* [wxMaxima: section start ] Maxima Manual and other online resources [wxMaxima: section end ] */ /* [wxMaxima: comment start ] A good summary of Maxima abilities (with examples) can be found in the Maxima Overview webpage by Burkhard Bunk: http://www-com.physik.hu-berlin.de/~bunk/kurs/maxima/maxima.html We refer the the simplification section of this webpage in the last section. ------------------------------------------------------------------------------- An excellent 245 page pdf online introduction to Maxima can be found at https://www2.palomar.edu/users/cchamberlin/Math%20205%20pages/Maxima/MaximaBook.pdf -------------------------------------------------------------------------------------- An excellent 52 page pdf with the titile: Multivariable Calculus with Maxima, by G. Jay Kerns, is at http://gkerns.people.ysu.edu/maxima/maximaintro/maximaintro.pdf -------------------------------------------------------------------------------------------- The current Maxima html manual is available at http://maxima.sourceforge.net/docs/manual/maxima_singlepage.html [wxMaxima: comment end ] */ /* [wxMaxima: section start ] Input and Output Numbering System, Evaluating a Cell or Multiple Cells [wxMaxima: section end ] */ /* [wxMaxima: comment start ] Enter these two commands: 5*4; and 5*4$ in a single cell, and then evaluate the cell, 1. either by left-clicking on the end of the last command (just to the right of the dollar sign) and then using the two-key command shift-enter via the keyboard, or 2. by right-clicking on the vertical bar at the left edge of the cell and then left-clicking on the choice Evaluate cell(s). Why do they yield different results? [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ 5*4; 5*4$ /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] The results differ because the command 5*4; which ends with the semi-colon first executes the command five times four, then binds the result to the symbol %o1, (here I am assuming you evaluated the above cell before any other cell, and also assuming that you haven't evaluated the cell more than once) and then prints the result of the multiplication. In contrast, the command 5*4$ which ends with a dollar sign first executes the command, then binds the result of the multiplication to the symbol %o2 , but does **not** print the result. You can later use those symbols to recover the output. The output from the symbols %o1 and %o2 should remain 20 even if you have executed the cell more than once (provided that was the first cell evaluated). [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ %o2; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ [%o1, %o2]; /* [wxMaxima: input end ] */ /* [wxMaxima: subsect start ] Selecting and Evaluating Multiple Cells [wxMaxima: subsect end ] */ /* [wxMaxima: comment start ] A. To EVALUATE multiple cells, first select the cells, then right click one of the cell brackets highlighted, and then left click on the choice "Evaluate Cell(s)". After selecting, you can also use the drop-down menu which occurs if you click on the Cell choice at the top of the wxMaxima screen; the top item will be Evaluate Cell(s). B. To SELECT multiple cells, there are two methods. 1. Method 1 works best if you need to select many cells. Click below the bottom cell to be selected to get a horizontal cursor line. Then use the two key command shift-up enough times to select a group of cells; you will see the highlights advance up the window. Don't worry about the comment cells you see - they won't interfere with the subsequent commands like "Evaluate cells". 2. Method 2 is usable if only a few contiguous cells are to be selected. Left click (and hold down) on the bottom cell of the group. Then drag up the screen highlighting more cells as you go. Then release the left mouse button. [wxMaxima: comment end ] */ /* [wxMaxima: section start ] maxima_userdir, maxima-init.mac, values, functions, grind, stringp [wxMaxima: section end ] */ /* [wxMaxima: comment start ] On my Windows 10 system, the symbol maxima_userdir produces the string "C:/Users/wooll/maxima" until the binding is changed using our maxima-init.mac file located in the folder C:/Users/wooll/maxima. You actually won't see the double quotes using the default display setting of wxMaxima (display2d : true) unless you use grind(%)$ (see below). [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ maxima_userdir; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] The wxMaxima default display setting (display2d : true) hides the fact that the symbol maxima_userdir is bound to a string (something enclosed in double quotes). To see clearly the string nature, you can always use the command grind(%)$ to obtain the display2d: false output of the previous cell temporarily. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ grind(%); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ display2d; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] In addition to, or instead of, using grind, you can use the predicate function stringp. stringp (obj) --> true IF obj is a string. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ stringp (maxima_userdir); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] The next cells won't reflect the definition of rnddp in the Maxima list values unless you either use my maxima-init.mac file (suitably edited to used the location of your current work folder) OR place the definition of the rnddp function in your own maxima-init.mac file. My maxima-init.mac file is included with the Ch. 1 files on the Maxima by Example webpage: http://web.csulb.edu/~woollett/ You should either use a modified version of my file, or include the definition of rnddp in your own maxima-init.mac file. [wxMaxima: comment end ] */ /* [wxMaxima: comment start ] My start-up file maxima-init.mac introduced some additional symbol bindings and also the function rnddp, in addition to defining maxima_userdir to be bound to the string "c:/work5" The Maxima keywords 'values' and 'functions' each refer to a separate list maintained by the Maxima engine for the current session. [wxMaxima: comment end ] */ /* [wxMaxima: comment start ] A. values is a list of all bound user variables (not Maxima options or switches). The list comprises symbols bound by :, or :: If the value of a variable is removed from the current session, using the commands kill, remove, or remvalue, the symbol is deleted from values. [wxMaxima: comment end ] */ /* [wxMaxima: comment start ] B. functions is the list of ordinary user defined Maxima functions (as opposed to core Maxima functions like float) in the current session. An ordinary function is a function constructed by either using the define construct or (more frequently) the := construct. A function may be defined either at the Maxima prompt or in a Maxima file which is loaded by using the load command or the batch command. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ values; functions; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] From the list returned by functions, we see that a function was defined in my startup file maxima-init.mac. Without looking at the file contents, we can see the definition of the function rnddp using the Maxima core function fundef: [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ fundef (rnddp); /* [wxMaxima: input end ] */ /* [wxMaxima: subsect start ] Loading mfiles.mac from Maxima by Example, Ch. 2 [wxMaxima: subsect end ] */ /* [wxMaxima: comment start ] You can read the contents of your maxima-init.mac file inside your current Maxima session if you first place two utility files from Maxima by Example, Ch 2, namely mfiles.mac and mfiles1.lisp (which mfiles.mac automatically loads), in your work folder. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ load(mfiles); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] One of the functions defined in mfiles.mac is print_file. You shold edit this next line so the path matches your system. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ print_file ("c:/Users/wooll/maxima/maxima-init.mac"); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Near the middle of my own file I see the definition of the user defined function rnddp. [wxMaxima: comment end ] */ /* [wxMaxima: section start ] fpprintprec, disp, display, ratsimp, rationalize, rat, print, %th(j) [wxMaxima: section end ] */ /* [wxMaxima: comment start ] %pi represents the ratio of the perimeter of a circle to its diameter. The numeric value of %pi is the double-precision floating-point value 3.141592653589793d0. First check the default value of fpprintprec [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ fpprintprec; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Now look at Maxima's double precision floating point value of %pi [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ %pi; float (%pi); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] We want to set the floating point print precision (fpprintprec) to 5 decimal places. But first look at the Maxima manual entry for fpprintprec using the ? symbol syntax. Notice you must leave a space between ? and the symbol. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ ? fpprintprec; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Change now to the value 5 decimal places. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ fpprintprec : 5; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Now look agian at what Maxima returns for float(%pi) [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ float (%pi); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] We get a five (5) digit printout of a number which is carried internally with 16 digit accuracy. Maxima continues to use 16 digit arithmetic for calculations. [wxMaxima: comment end ] */ /* [wxMaxima: comment start ] "printed" in the manual entry (above) really means more than just using the Maxima core function print. Here we show uses of disp, display, and print. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ disp ( float (%pi) )$ /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ display ( float (%pi) )$ /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ print (" float (%pi) = ", float (%pi) )$ /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Check on the default value of ratprint (which can be either true or false). When ratprint is true, a message informing the user of the conversion of floating point numbers to rational numbers is displayed [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ ratprint; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ 2.123/3.456; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ ratsimp(%); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ rat(2.123/3.456); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ rationalize (2.123/3.456); /* [wxMaxima: input end ] */ /* [wxMaxima: subsect start ] Working with pieces of an expression [wxMaxima: subsect end ] */ /* [wxMaxima: comment start ] Simplify this computation: (16/(8-6)+9-4)/(2^5+3^2). First what does Maxima produce? [wxMaxima: comment end ] */ /* [wxMaxima: hide output ] */ /* [wxMaxima: input start ] */ (16/(8-6)+9-4)/(2^5+3^2); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] by hand method 1. Note: %th(2) is the output value just previous to the output value selected by just using % [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ 16/(8-6); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ % + 9; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ % - 4; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] that's the numerator. Now for the denominator. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ 2^5; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ 3^2; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ % + %th(2); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] that's the denominator. Now execute the division. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ %th(4)/%; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] which agrees with the answer Maxima produced. by hand, method 2 [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ 16/(8-6)+9-4; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ 2^5+3^2; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ %th(2) / %; /* [wxMaxima: input end ] */ /* [wxMaxima: section start ] sqrt, log, factorial, imaginary numbers [wxMaxima: section end ] */ /* [wxMaxima: input start ] */ sqrt (x); x^(1/2); log (x); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Maxima can work with radicals, including fractional roots. Recall that sqrt(x) = x^(1/2), and that other roots can be expressed this way. Evaluate these: sqrt(49), -sqrt(49), sqrt(-49), (32/243)^(5), (32/243)^(-5), (32/243)^(1/5), (32/243)^(0.2). Note that the third value is a complex number, where %i is Maxima's representation of sqrt(-1). Why are the third and fourth values reciprocals of each other? Why are the last two items expressed differently? [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ sqrt(49); -sqrt(49); sqrt(-49); (32/243)^5; (32/243)^(-5); (32/243)^(1/5); (32/243)^(0.2); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Assign (bind) the name bb to 5*4. Enter the commands bb:5*4$, bb;, bb^2;, and bb!;. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ bb:5*4$ bb; bb^2; bb!; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ factorial (bb); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Enter the commands log (bb);, log (1.0*bb);, and float (log ( b));. Note that Maxima's log function returns the natural logarithm. Why doesn't Maxima provide a floating-point decimal representation of log (bb)? Why does Maxima yield a floating-point decimal representation of log(1.0*bb)? [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ log(bb); log(1.0*bb); float (log (bb) ); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ 1.0 * bb; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Confirm that Maxima can nest commands by comparing the following results: 1. sqrt(144); log(%);, 2. sqrt(log(144)); 3. log(sqrt(144));. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ sqrt(144); log(%); sqrt ( log (144 ) ); log ( sqrt (144) ); /* [wxMaxima: input end ] */ /* [wxMaxima: section start ] factorial function related to gamma function [wxMaxima: section end ] */ /* [wxMaxima: comment start ] In the next cell, we examine the factorial function, and the related gamma function. First, use the simple command 4!; to determine that 4! = 24. Then use the command gamma(5); to confirm that gamma(n) = (n-1)!. Then enter the command (9/2)!; and then enter a command that renders this result as a floating-point representation. (Obviously Maxima does not compute n! as n*(n-1)*...*2*1, as one would do with integers. Rather it uses the gamma function.) [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ 4!; gamma (5); (9/2)!; float (%); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Use the commands 5!;, factorial(5);, and gamma(6); as ways to confirm that 5! = 120. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ 5!; factorial (5); gamma (6); /* [wxMaxima: input end ] */ /* [wxMaxima: section start ] Binding a Symbol to an Expression, ev, subst, listp, kill [wxMaxima: section end ] */ /* [wxMaxima: comment start ] Create the bindings yy : log(x)$ and zz : sqrt(x)$, where x is some real parameter. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ yy : log (x)$ zz : sqrt (x)$ /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ yy; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ zz; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Now use ev and subst commands to evaluate the symbols numerically. First enter the command ev (yy, x = 25.0) to evaluate the symbol yy for a particular vale of the parameter x. Note that inside the ev environment, x = 25.0$ temporarily assigns the value 25.0 to x, but does not replace x with 25.0 in Maxima's memory. Similarly, evaluate the symbol zz when x = 25. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ ev ( yy, x = 25.0 ); yy, x = 25.0; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] The second method used in this last cell is a shortcut way of making use of the ev function technology. You can have multiple switches and conditions set inside the ev function use. [wxMaxima: comment end ] */ /* [wxMaxima: comment start ] The symbol x has not been bound to anything: [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ x; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] The Maxima experts strongly reccommend using subst instead of ev. Here are two equivalent ways to use subst to do the same job as we did with ev above: [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ subst (25.0, x, yy); subst (x = 25.0, yy); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Now we compare ev with subst to return a numerical value for the symbol zz. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ ev ( zz, x = 25); zz, x = 25; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ x; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ subst ( 25, x, zz); subst (x = 25, zz); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ x; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] The Maxima list returned by values should now be populated (non-emptly). Also the predicate function listp can be used, and listp(obj) --> true IF obj is a Maxima list. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ listp (values); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ values; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ [ bb, yy, zz]; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] You can use kill (yy) to remove the binding of yy. There are many different ways to use the kill function; you should read the Maxima manual entry for kill. [wxMaxima: comment end ] */ /* [wxMaxima: section start ] Simple examples of user defined functions [wxMaxima: section end ] */ /* [wxMaxima: comment start ] Although much of Maxima use is simpler when using expressions, rather than specially defined functions, sometimes creating explicit Maxima functions results in more flexibility that just binding a symbol to an expression. Enter the function definition in the form: f(x) := log (x);. Also, create a function that defines g(x) as the square root of x. Then ask for the value of the list: [f (25.0), g (25.0) ];. Evaluate the two functions f and g when x = 5 and when x = 5.0. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ f(x) := log (x)$ g(x) := sqrt (x)$ [ f (x), g(x) ], x = 25.0; [ f (x), g(x) ], x = 5; [ f (x), g(x) ], x = 5.0; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Extend the use of functional forms to include two variables and a parameter. Use the function f(x,y,a) = a*sqrt(x*y). Evaluate these: f (x,y,50), f (16,y,50), f (16,36,50). [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ f (x, y, a) := a*sqrt (x*y)$ f (x, y, 50); f (16, y, 50); f (16, 36, 50); /* [wxMaxima: input end ] */ /* [wxMaxima: section start ] More examples of ev and subst with a two parameter binding [wxMaxima: section end ] */ /* [wxMaxima: comment start ] bind the symbol expr1 to the two parameter expression: 5*x + 4*y - 20. Now use subst ( [x = 5, y = 3], expr1) (or ev) to evaluate this expression. Repeat this process, evaluating expr1 for x = 0 and y = 5, and again for x = 10 and y = -15/2. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ expr1 : 5*x + 4*y - 20; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ expr1, x = 5, y = 3; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ subst ( [x = 5, y = 3], expr1); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ expr1, x = 0, y = 5; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ subst ( [x = 0, y = 5], expr1); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ expr1, x = 10, y = -15/2; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ subst ( [x = 10, y = -15/2], expr1); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ expr1; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] Recall that values returns a list of symbols which currently are bound to something. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ values; /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] The use of the kill function in the form kill (all) is fairly drastic. [wxMaxima: comment end ] */ /* [wxMaxima: input start ] */ kill (all); /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ values; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ functions; /* [wxMaxima: input end ] */ /* [wxMaxima: input start ] */ rnddp (1.23456, 2); /* [wxMaxima: input end ] */ /* [wxMaxima: comment start ] We see that kill (all) removed the definition of our rnddp function as well as the symbol bindings made above. [wxMaxima: comment end ] */ /* [wxMaxima: section start ] Simplification of expressions [wxMaxima: section end ] */ /* [wxMaxima: comment start ] A good summary of methods which can be used to simplify expressions in Maxima is provided by the webpage section: http://www-com.physik.hu-berlin.de/~bunk/kurs/maxima/maxima.html#simp Perhaps the most used strategy to first try is to first use expand, and then use ratsimp on the result of expand. As in the following cell contents: expand (expr); ratsimp (%); or equivalently: ratsimp ( expand (expr)); [wxMaxima: comment end ] */ /* Old versions of Maxima abort on loading files that end in a comment. */ "Created with wxMaxima 19.05.7"$