file mbe1start.txt e. woollett, May 23,2008 cut and paste code for Maxima by Example: Ch. 1, Getting Started available for download at http://www.csulb.edu/~woollett 1.1 Introduction ========================= 1.1.1 What is Maxima? ------------------ 1.2 Using the XMaxima Interface =========================== 1.2.1 Using Console - Command Line Maxima 1.2.2 Text Editors 1.2.3 The Maxima Manual 1.3 Expressions in Maxima ============================= 1.3.1 Operators in Maxima ---------------- %i1) x^2+3; (%i2) 2^3^4; (%i3) 2^(3^4); (%i4) (2^3)^4; 1.3.2 Numbers in Maxima -------------------- (%i1) [x,y,z,w] :[123.0, 1.23e2, 1.23d2, float(123) ]; (%i2) map( floatnump,[x,y,z,w] ); (%i3) [sin(1),sin(1.0),float(sin(1)),ev(sin(1),float),ev(sin(1),numer)]; 1.3.3 Variables in Maxima ------------------------- (%i1) values; (%i2) a : 3 + 7; (%i3) a; (%i4) values; (%i5) c : a + b; (%i6) b : 5; (%i7) c; (%i8) ''c; (%i9) remvalue(a); (%i10) values; (%i11) [b,c]; (%i12) remvalue(all); (%i13) values; (%i14) [c,b]; 1.3.4 Constants in Maxima --------------------------- (%i1) float( [%e,%pi,%phi,%gamma] ); (%i2) fpprec; (%i3) bfloat(%pi); (%i4) bfloat(%pi),fpprec:100; 1.3.5 Rational Expressions in Maxima, rat(...) ---------------------------- (%i5) example(ratsimp); (%i13) example(); ------------------- (%i1) e1: (x + y)^4; (%i2) rat(e1); (%i3) rat(e1,x); 1.4 List Creation and Manipulation ============================================ 1.4.1 The Maxima List and the Lisp List ---------------------------------- (%i1) mylist:[a,a*b,3*%i,4*%pi,[1,2] ]; (%i2) :lisp $mylist (%i2) a1 : mylist[1]; (%i3) :lisp $a1 1.4.2 cons, endcons, append ---------------------------- (%i1) v1 : [a,b,c]$ (%i2) v2 : [1, 2, 3]$ (%i3) v1 + v2; (%i4) v1 - v2; (%i5) v1*v2; (%i6) v1.v2; (%i7) v1 / v2; (%i8) v2 / v1; (%i9) v1^v2; (%i10) length(v1); (%i11) first(v1); (%i12) rest(v1); (%i13) last(v1); (%i14) cons(a0,v1); (%i15) endcons(zz,v1); (%i16) append(v1,v2); 1.4.3 makelist and map --------------------------- (%i1) makelist(concat(x,i),i,1,6); (%i2) makelist(x=y,y,[a,b,c]); (%i3) makelist(x^3,x, v1); (%i4) makelist(sin(j*%pi/3), j, 1, 6 ); ------------------- (%i1) f; (%i2) map(f,[a,b,c]); (%i3) map(f,[a,b,c], [aa,bb,cc] ); ------------------- (%i1) v1 : [a,b,c]$ (%i2) v2 : [1,2,3]$ (%i3) v3:[ aa,bb,cc]$ (%i4) map("*",v1,v2); (%i5) map("/",v1,v2); (%i6) map("+",v1,v2); (%i7) map("=",v1,v2); (%i8) map("[",v1,v2); (%i9) map("[",v1,v2,v3); ---------------------- (%i1) solns : [ [a = 1, b = 2], [a = 2, b = 1 ] ]; (%i2) solns[1]; (%i3) [a,b] : map( rhs, solns[1] )$ (%i4) [a,b]; ---------------------- (%i1) fpprintprec:5$ (%i2) xlist : makelist(j,j,0,20)/20.0; (%i3) map( sin, xlist); (%i4) f(x) := x^3$ (%i5) map( f, xlist ); (%i6) g(x) := sin(x*%pi/3); (%i7) jlist : makelist(j,j,1,6); (%i8) map(g,jlist); (%i9) functions; 1.4.4 Anonymous Unnamed Function lamda ----------------------------------- (%i10) jlist; (%i11) map(lambda([x],sin(x*%pi/3) ), jlist); (%i12) functions; (%i13) map(lambda([x],sin(x*%pi/3) ), makelist(j,j,1,6) ); --------------- (%i1) v1 : [a,b,c]$ (%i2) f(x) := x^3$ (%i3) g : z^4$ (%i4) makelist(f(y),y,v1); (%i5) makelist(g,z,v1); (%i6) makelist(''g,z,v1); (%i7) functions; 1.4.5 Accessing List Elements Using part(...) or Bracket Pairs ------------------------------- (%i1) v1 : [a,b,c]; (%i2) v1[2]; (%i3) part(v1,2); (%i4) subst(bb,v1[2], v1); (%i5) v1; (%i6) xlist : [1,2,3,4]; (%i7) ylist : [1,4,9,16]; (%i8) xylist : makelist([xlist[i],ylist[i]],i,1,length(xlist) ); 1.4.6 Creating a List of Fractional Values ------------------------------------ (%i1) fpprintprec:5$ (%i2) xfrac(n) := makelist(j,j,0,10*n)/float(n)$ (%i3) xfrac(2); (%i4) xfrac(3); (%i5) xfrac(4)/10.0; 1.5 Data Files: Read, Write, Fit, and Plot ========================================= 1.5.1 file_search 1.5.2 file_search_maxima 1.5.3 maxima-init.mac and maxima_userdir ----------------------------- (%i1) maxima_userdir; ------------------ restart (%i1) maxima_userdir; (%i2) file_search_maxima; 1.5.4 Viewing a File with printfile --------------------------- (%i1) file_search("windows-user.txt"); (%i2) printfile("windows-user.txt")$ 1.5.5 Creating a Data File using with_stdout ------------------------------------- (%i2) file_search("tmp.dat"); (%i3) with_stdout( "tmp.dat", for i:1 thru 4 do print(i, i^2, i^3) )$ (%i4) file_search("tmp.dat"); (%i5) printfile("tmp.dat")$ 1.5.6 Creating a List from a Data File: read_nested_list ----------------------------- (%i6) datalist1 : read_nested_list("tmp.dat"); (%i7) printfile("text.dat")$ (%i8) textdata : read_nested_list("text.dat"); (%i9) printfile("text2.dat")$ (%i10) textdata2 : read_nested_list("text2.dat"); (%i11) pp : part(textdata2,1,5); (%i12) atom(pp); (%i13) :lisp $pp 1.5.7 Write List to Data File with write_data --------------------------------- (%i14) datalist1; (%i15) write_data(datalist1,"tmp2.dat")$ (%i16) printfile("tmp2.dat")$ 1.5.8 Using Random Numbers to Create Noisy Data -------------------------------- (%i1) set_random_state ( make_random_state (654321) )$ (%i2) makelist(random(11),j,1,20 ); (%i3) fpprintprec : 5$ (%i4) makelist(random(1.0),j,1,20 ); (%i5) makelist( -0.2 + random(0.4), j, 1, 20 ); (%i6) doline():=block([x,y,r], set_random_state (make_random_state (654321)), with_stdout( "fit1.dat" , for i:1 thru 10 do ( x : i, r: -0.2 + random(0.4), y: 1 + x + r, print(x,y) ) /* end do */ ) /* end with_stdout */ )$ (%i7) doline()$ (%i8) printfile("fit1.dat")$ (%i9) [x,y,r]; 1.5.9 Using lsquares_estimates for a Fit ------------------------------------ (%i10) datamatrix: read_matrix("fit1.dat"); (%i11) grind(%)$ (%i12) load(lsquares); (%i13) lsquares_estimates( datamatrix, [x, y], y = a*x + b, [a, b] ); (%i14) soln : float(%)[1]; (%i15) [a,b] : map( rhs, soln)$ (%i16) [a,b]; (%i17) datalist : substpart("[", datamatrix, 0 ); (%i18) plot2d( [ a*x+b, [discrete,datalist] ], [ x, 0, 10] , [style, [lines], [points] ], [legend, "fit", "data"] )$ 1.5.10 Nested List <--> Matrix using apply and substpart ------------------------------- (%i1) mylist : [[a,b],[c,d]]; (%i2) mymatrix : apply('matrix, mylist); (%i3) grind(mymatrix)$ (%i4) length(mylist); (%i5) makelist(part(mylist,j),j,0,2 ); (%i6) part(mylist,0); (%i7) mymatrix : substpart(matrix,mylist,0); (%i8) grind(mymatrix)$ (%i9) part(mymatrix,0); (%i10) substpart("[", mymatrix, 0 ); 1.5.11 The Syntax of lsquares_estimates -------------------------------------- 1.5.12 Fit to a More Complicated Equation ----------------------------------------- (%i1) fpprintprec : 5 $ (%i2) xlist : float( makelist(j/10,j,0,10 ) ); (%i3) set_random_state ( make_random_state (654321) )$ (%i4) y(z) := -0.2 + random(0.4) + 2*exp(-3*z)$ (%i5) ylist : map(y,xlist); (%i6) datalist : map( "[", xlist, ylist ); (%i7) datamatrix : apply('matrix, datalist )$ (%i8) grind(datamatrix)$ (%i9) load(lsquares); (%i10) solnlist: lsquares_estimates(datamatrix,[x,y],y=a*exp(-b*x),[a,b],iprint=[-1,0] ); (%i11) solnlist : solnlist[1]; (%i12) [a,b] : map( rhs, solnlist )$ (%i13) [a,b]; (%i14) plot2d( [ a*exp(-b*x), [discrete,datalist] ], [ x, 0, 1] , [style, [lines], [points] ], [legend, "fit", "data"] )$ 1.5.13 plot2d: Squaring the Circle -------------------------------------- (%i1) plot2d ([parametric, cos(t), sin(t), [t,-%pi,%pi], [nticks,80]], [x, -4/3, 4/3])$ ---------------- /* file start.mac */ print("start.mac: try to adjust parameter r to get round circle of radius 1")$ print("squarelist")$ squarelist : [ [-1,1],[1,1],[1,-1],[-1,-1], [-1,1] ]$ print("docircle(r)")$ docircle(r) := block([dy:1.5,dx ], dx : r*dy,display([dy,dx]), plot2d ( [ [discrete,squarelist], [parametric, cos(t), sin(t), [t,-%pi,%pi],[nticks,80]] ], [style, [lines,2,0], [lines,2,0] ], [x, -dx,dx ], [y, -dy,dy], [gnuplot_preamble, " set xzeroaxis lw 2; set yzeroaxis lw 2; set nokey "] ) )$ print("starting value for r is 4/3 = 1.333 ")$ /* record of use (%i1) load("start.mac"); start.mac: try to adjust parameter r to get round circle of radius 1 squarelist docircle(r) starting value for r is 4/3 = 1.333 (%o1) start.mac (%i2) docircle(1.333)$ [dy, dx] = [1.5, 1.9995] (%o2) (%i3) docircle(1.43)$ [dy, dx] = [1.5, 2.145] */ /* console version set of boxed circles */ print("console version squarelist(r),dsq(r),circle(r), docircles()")$ squarelist(r) := [ [-r,r],[r,r],[r,-r],[-r,-r], [-r,r] ]$ dsq(r) := [discrete, squarelist(r) ]$ circle(r) := [parametric, r*cos(t), r*sin(t), [t,-%pi,%pi],[nticks,80]]$ docircles() := block([dy:1.5,dx ], dx : 1.43*dy, display([dy,dx]), plot2d ( [ dsq(1.4), circle(1.4),dsq(1.2), circle(1.2),dsq(1.0),circle(1.0), dsq(0.8),circle(0.8),dsq(0.6),circle(0.6) ], [style, [lines,4,1], [lines,4,1], [lines,4,2],[lines,4,2],[lines,4,3], [lines,4,3],[lines,4,4],[lines,4,4], [lines,4,6],[lines,4,6] ], [x, -dx,dx ], [y, -dy,dy], [gnuplot_preamble, " set nokey "] ) )$ /* record of use (%i2) load("start.mac"); console version squarelist(r),dsq(r),circle(r), docircles() (%o2) start.mac (%i3) docircles()$ [dy, dx] = [1.5, 2.145] */ /* end file start.mac */ ---------------------- 1.6 Latex Figures from Maxima plot2d ======================================= (%i21) plot2d( [ a*x+b, [discrete,datalist] ], [ x, 0, 10] , [style, [lines], [points] ], [legend, "fit", "data"], [gnuplot_term,ps], [gnuplot_out_file,"c:/work2/fit1.eps"])$