This workbook contains the wxMaxima commands that appear in 
Sections 9 and 10 of Chapter 1.

The cell below is optional. The purpose of each of the commands
is indicated in the final section of Chapter 1 in the text.

load(draw)$ 
set_draw_defaults(  fill_color=gray40,
draw_realpart=false,font_size=10)$
fpprintprec:5$ ratprint:false$kill(all)$

# Chapter 1. Introduction

## Basics

## The wxMaxima Screen

## Mathematical Functions

## Referring to and Evaluating Expressions

## Sequences, Lists, and Sets 

## Solving Equations

## Calculus

## Differential Equations

## Simplifying and Manipulating 

This section shows some commands that can be used to make 
expressions more informative. 

### Simplification

The cell below shows ratsimp( ), which is used to simplify 
rational expressions. Applying ratsimp( ) to a does not 
replace a in Maxima's memory automatically. We repeat this
cell below, making one change from that in the text. As a 
result, a is replaced by the simplification.

matrix(["Expression","Simplification","Expression"],
  [a: (x^2 - 4)/(x + 2),  ratsimp(a),  a] );

Now a is replaced by a new expression x - 2 in Maxima's memory.

matrix(["Expression","Simplification","Expression"],
  [a: (x^2 - 4)/(x + 2),  a:ratsimp(a),  a] );

ratsimp( ) puts an expression into the form of numerator over 
denominator and divides out the greatest common divisor of 
numerator and denominator, as the next example illustrates.

kill(all)$ matrix(["Expression","Simplification"],
 [f(x) := 2+(x^2+3*x+2)/((x+2)*(x-1)), 
 ratsimp(f(x))] );

Other simplfication commands include radcad( ) and trigsimp( ).
Often, but not always, the three provide the same representation
of the expression, although trigsimp( ) is, as the name suggests, 
optimized for trigonometric expressions.

radcan(f(x));   trigsimp(f(x));

### Factoring and Expanding Expressions

factor( ), as its name suggests, factors polynomials and 
expressions containing polynomials. gfactor( ) extends the 
analysis to complex factors.

kill(all)$  transpose(matrix(["Expression", 
"w/factor", "w/gfactor"],[f(x) := (x^4 - 16),
 factor(f(x)),gfactor(f(x))] ));

The expand( ) command, as its name suggests distributes operations. 
The command works for many types of expression. When expand( ) 
cannot expand the expression, Maxima returns the initial expression. 

kill(a,b)$   expand(sin(a+b));
expand(exp(y+log(x+1)));
ratsimp(exp(y+log(x+1)));

Sometimes, the highest allowable positive exponent (or negative 
exponent) should be specified in order to apply expand( ). Use
the commands expop (positive) or expon (negative).

expand(x+1)^3; 
expop:30; expand(x+1)^3; 
expop:0;
expand(x+1)^3;

### Collecting Terms and Retrieving Coefficients

collectterms( ) collects the terms of a polynomial by its powers.

kill(all)$ expop:3$  p:expand(1+a-x)*(x+a)^2;
collectterms(p, x);

## Plotting (Text Section 1.10)

This section addresses the use of draw( ) to draw 2- and 
3-dimensional graphs. The command wxdraw2d( ) places 
2-dimensional graphs inside the workbook. Omitting the wx
results in a graph that can be edited and saved. The
3-dimensional counterpart is wxdraw3d. Omitting the wx
again generates an external graph. Sometimes, it is useful
to generate such a graph, which can be rotated, in order to
determine which view works best for the expression at hand.

### Two-Dimensional Graphs

The graph below is for this probability distribution function,
which applies to non-negative x values..

kill(f)$ f(x,a):=sqrt(2/%pi)*(x^2*exp(-x^2/(2*a^2))/a^3);

The explicit( ) option defines the type of function to be 
graphed. draw( ) is a rather verbose but quite intuitive 
interface for the gnuplot program. Theoretically, x can 
take on very large values, but we limit the graph to the 
range from 0 to 10, because the values above 10 are too
small to show up on the graph.

wxdraw2d(explicit(f(x,2),x,0,10) )$

The cell below extends the material the book. The first
output list shows the heights of the expression at four 
x values. The second output line shows that this expression
satisfies the requirement that the area under the distribution
function equal 1.

float([f(5,2),f(10,2),f(25,2),f(75,2)]);
integrate(f(x,1),x,0,inf);

One attraction of using the functional notation f(x,a) 
is that it facilitates graphing expressions.

wxdraw2d(key="a = 1",explicit(f(x,1),x,0,20),
   key="a = 2",line_width=2,explicit(f(x,2),x,0,20),
   key="a = 5",color=red,explicit(f(x,5),x,0,20) )$

The cell below extends material from the book. It animates
the graph above, showing how this distribution changes as
the parameter a changes. We limit the analysis to 5 values
of a. The command with_slider_draw( ) creates one graph 
per value assigned to a, so graphing a large number can 
result in slow completion of the task.

The only diffference between wxdraw2d( ) and with_slider_draw( )
is the first two items must be the name assigned to parameter
that varies and a command that generates a set of values. 

Once the command is executed, click on the graph. Then 
select the "start animation" button (the triangle) to start 
a slide show, or on the scroll bar at its right to control
changes in the parameter value.

Look up concat( ) and string( ) in the Manual.

with_slider_draw(a, makelist(i,i,1,5),
yrange=[0,.6], key = concat("a = ",string(a)),
line_width=2, explicit(f(x,a),x,0,20) )$

Complicated expressions do not cause problems with draw( ).
Note that we have used string( ) again. We have also 
added labels to the axes.

kill(all)$ /*Define the function*/
   f(a,x):=if x<a then 0 elseif x<2*a then x^2
     else (2*a)^2 + (x - 2*a)$
/*Values for graphing */ [a1,a2,a3]:[2,5,8]$   
   a_max:max(a1,a2,a3)$  y_max:f(a_max,2.5*a_max)$ 
/*Draw the graph*/    wxdraw2d( yrange=[0,1.25*y_max],
 xlabel="x",ylabel="y",key=string(f(a1,x)),
 explicit(f(a1,x),x,0,2.5*a_max),line_width=2,key=
 string(f(a2,x)),explicit(f(a2,x),x,0,2.5*a_max),color=
 gray50,key= string(f(a3, x)),explicit(f(a3,x),x,
  0,2.5*a_max),dimensions=[640,360]   )$

### Graphing Points

The next cell creates two sets of points. It then graphs the 
points, along with an explicit( ) representation.

kill(all)$ 
/*Data*/ 
xL:makelist(x*%pi/10,x,0,40)$ 
yL:makelist(sin(x*%pi/10),x,0,40)$
rL:makelist(random(1.0),k,0,40) $
/*Graph*/
wxdraw2d(user_preamble="set key bottom center",
  key="sine,points",points(xL,yL),
  key="sine, line",explicit(sin(x),x,0,4*%pi),
  point_type=6,color=gray40, key = "random points",
  points_joined=true,points(xL,rL)        )$

The next cell replicates the one above, except that 
the explicit function has been removed, so we see the
difference that points_joined=true makes. The point_size
option is used twice.

wxdraw2d(user_preamble="set key bottom center",
  color=red, point_size=2,
  key="sine,points",points(xL,yL),
  point_size=1,
  point_type=6,color=black, key = "random points",
  points_joined=true,points(xL,rL)        )$

### Three-Dimensional Graphs

The example below shows a simple application of wxdraw3d( ). 
In it a Gaussian function intersects a plane. 

kill(all)$ 
matrix(["Gaussian function:  ",g(x,y):=20*exp(-x^2-y^2)-10], 
  ["Plane:  ",p(x,y):=x+y]);
wxdraw3d(xlabel="x", zlabel="z",ztics=10,
 user_preamble="set key top left",
 key ="Gauss", explicit(g(x,y),x,-3,3,y,-5,5),
 color = "light-green", key = "Plane",surface_hide=true,      
 explicit(p(x, y), x, - 5, 5, y, - 5, 5)  )$

Three-dimensional graphs can also be animated. The 
graph below causes a surface to change shape in response
to changes in a parameter's value. 

kill(all)$  f(x, y):=y^2 - x^2;
with_slider_draw3d(t, makelist(0.2*i,i,-5,5),
xaxis=true, yaxis=true,zrange=[-12,12], 
ztics={-9, 0, 9}, xlabel="x", ylabel="y",zlabel="z",
title=concat("t = ",string(t)), view=[70,135],
explicit(f(t*x, y),x, -3, 3, y, -3, 3)   )$

### Graphing Implicit and Parametric 
Functions

The graph below depicts two circles using the implicit( ) option.
Implicit relationships appear frequently in subsequent analysis.

f(x,y, a):= x^2+y^2-a$
wxdraw2d(key="Little Circle",xlabel="x",ylabel="y",
 implicit(f(x,y,5),x,-5,5,y,-5,5),line_width=2,key=
"Big Circle",implicit(x^2+y^2=12,x,-5,5,y,-5,5) )$

To impose equal units on the x and y axes, add "set size
ratio = -1" in a user_preamble. This is seldom an issue in
economics, because the units on the two axes are not typically
the same.

f(x,y,a):=x^2+y^2-a$    wxdraw2d(key="Little Circle",
 xlabel="x",user_preamble="set size ratio -1",ylabel="y",
 implicit(f(x,y,5),x,-5,5,y,-5,5),line_width=2,
 key ="Big Circle",implicit(x^2+y^2=12,x,-5,5,y,-5,5),
 dimensions=[400,400] )$

Sometimes x and y are related to each other, but not directly.
Rather each of the two is a function of a third variable. 

Suppose that x = sin(t) and y = cos(t), where t is 
an angle. Then t is a parameter in both expressions.
Parametric relationships appear in a few places in 
subsequent analysis.

wxdraw2d(user_preamble="set size ratio -1", 
parametric(sin(t), cos(t), t, 0, 2*%pi),xlabel="x",
ylabel="y", dimensions=[400,400] )$

### Scenarios

Replacing draw with gr in commands can generate scenarios. Assign
a name to a scenario and then use wxdraw( ) -- not wxdraw2d( ) or 
wxdraw3d( ) -- to draw the graphs. 
Let y = -0.5*x^3 + 25*x^2 + 100*x. Assign the name "original"
to the graph of this expresssion and "perUnit" to the graph
of y/x and dy/dx. The units will differ between the two

y: -0.5*x^3 + 25*x^2 + 100*x;  
original:gr2d(xlabel="x",ylabel="y", explicit(y,x,0,50))$
perUnit: gr2d(xlabel="x",ylabel="y per unit of x",
 yrange = [-1000,1000], xaxis=true, key="average", 
 explicit(y/x, x, 0,50), line_width=2,key="derivative",
 explicit(diff(y,x),x,0,50)  )$
wxdraw(original,perUnit,columns=2,dimensions=[640,360])$

Stacking the two can emphasize important relationships. In 
particular we see that y's maximum value occurs just below
x = 40, where dy/dx = 0.

wxdraw(original,perUnit,/*columns=2,*/dimensions=[640,480])$

We can determine that x = 3.226, approximately, yields maximum
y. Also, subst( ) shows the maximum value of y. Finally, we
see that the dy/dx = y/x when x = 25 (and at x = 0).

float(solve(diff(y,x)));
subst(%[2],y);
float(solve(diff(y,x)=y/x),x);