This workbook contains some of the wxMaxima commands that appear in 
Chapter 3.

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,color=gray10, 
draw_realpart=false,font_size=10)$
fpprintprec:5$  ratprint:false$ kill(all)$

# Demand Theory: Preferences

This chapter  assumes the existence of an established utility 
function that expresses the consumer's preferences.

## A Utility Function

## Indifference Curves

We explore the Constant Elasticity of Substitution or CES 
function in the preceding sections (0301UtilityFunction.wmx and
0302IndifferenceCurves.wxm)

## "Bliss" and Negative Marginal Utility

This section considers an representation of the utility, 
one in which the marginal utility for x, y, or both can 
be negative. The function $w(x,y,a,b c,d,e)$ is defined here.

w(x,y,a,b,c,d,e):=a*x+b*x^2+c*y+d*y^2+e*sqrt(x*y^2);

The next input group specifies a set of parameter values for 
this function and evaluates the function for two x, y pairs.

assume(x>0, y>0)$  
[a1,b1,c1,d1,e1]:[10.0,-0.5,25.0,-0.8,5.0]$
w(x, y, a1, b1, c1, d1, e1);  w10:  
w(10.0,20.0,a1,b1,c1,d1,e1)$w100: 
w(100, 20,a1,b1,c1,d1,e1)$
matrix(["w, x = 10", "w, x = 100"], [w10, w100]);

That the second w value is negative is not important is that 
increasing x, y held constant, causes utility to decrease. The
marginal utility of x is negative.

The next cell derives the two marginal utility functions
and creates functional expressions for them.

mwx(x,y,a,b,c,d,e) := ''(diff(w(x,y,a,b,c,d,e), x));
mwy(x,y,a,b,c,d,e) := ''(diff(w(x,y,a,b,c,d,e), y));

If both mw functions equal zero, the consumer achieves
the maximum utility possible. (Parameters are selected to
ensure that a maximum, not a minimum is reached.) More of
either, or both, goods would make the consumer worse off.

load("mnewton")$   
soln:mnewton(
  [mwx(x,y,a1,b1,c1,d1,e1)=0,mwy(x,y,a1,b1,c1,d1,e1)=0],
  [x,y], [10,10]);

Given these values, the next cell determines the maximum
utility level.

[[x0,y0]:[rhs(soln[1][1]), rhs(soln[1][2])]];
   wmax:w(x0,y0,a1,b1,c1,d1,e1);

The graph below shows the utility surface. The curves on the
graph correspond to indifference curves. The center area 
defined by the center curve is so small as to look like a 
point. Indeed, the "bliss point" in inside that area.

wxdraw3d( zrange=[0,1.1*wmax],contour=both,
 view=[70,330],dimensions=[600,500], xlabel="Good x",
 ylabel= "Good y",surface_hide=true,
 xtics = {0,2*x0},ytics={0,2*y0},
 contour_levels={0.6*wmax,0.8*wmax,0.999*wmax},color=green,
 explicit(w(x,y,a1,b1,c1,d1,e1),x,0,2*x0,y,0,2*y0),
 point_size=2,color=black,points([[x0,y0,wmax]]))$

### Indifference Curves and the Bliss Point

The code below rephrases the information 
on the base of the graph above as a set of three 
indifference curves and a point (we add one indifference
curve.)  Two curves trace the x,y pairs at which mwx and mwy
are zero. The two curves intersect at the bliss point.

The ip_grid command is added because implicit( ) 
representations are better plotted when the default [30,30] 
is replaced with larger values. 
Four label( ) commands are used to define regions 
of the graph. Note the syntax of these commands and experiment
with the placement.

The implicit( ) commands set w( ...) equal to increasing
large values, beginning with 0.6*wmax (wmax is defined
above) and ending with 0.8*wmax. Then two implicit( )
commands generate the lines where marginal utilities are
zero. Finally, the bliss point is entered.

wxdraw2d(user_preamble="set key bottom right",
 dimensions = [600,500],  ip_grid = [100, 100],
 yrange=[0,2*y0],title= 
 "Indifference Curves and Bliss Point",
      /*Labels for the regions */
 xlabel="Good x",ylabel="Good y", 
 label(["mwx>0 and mwy>0", x0/2.5,y0/2]),
 label(["mwx<0 and mwy>0", x0,y0/4]),
 label(["mwx<0 and mwy<0", 2*x0,1.75*y0]),
 label(["mwx>0 and mwy<0", x0/2,1.75*y0]), 
 key="Lowest w",line_width=2,color=orange,implicit(
   w(x,y,a1,b1,c1,d1,e1)=.6*wmax,x,0,2.5*x0,y,0,2.5*y0),
 color=blue,key="2nd lowest w",implicit(
   w(x,y,a1,b1,c1,d1,e1)= .8*wmax,x,0,2.5*x0,y,0,2.5*y0), 
 key="Higher w",color=red,implicit(
   w(x,y,a1,b1,c1,d1,e1)=.9*wmax,x,0,2.5*x0,y,0,2.5*y0),
 color=black, key="mwx=0",implicit(
 mwx(x,y,a1,b1,c1,d1,e1)=0, x,0,2.5*x0,y,0,2.5*y0),
 
 key="mwy=0",color=gray,implicit(
 mwy(x,y,a1,b1,c1,d1,e1)=0,x,0,2.5*x0,y,0,2.5*y0), 
 color=black,key="Bliss point",
 point_type=13, point_size = 2,points([[x0,y0]])  )$