# Plotting with draw( )

Use the command wxdraw2d(explicit(x^2,x,-5,5))$ to draw the
graph of y = x^2 over the range x = -5 to x = 5. The command 
may be ended with either a dollar sign or a semicolon. Try
both to see the difference in the output. 

/*Input. Execute (ctrl-enter) for results */
wxdraw2d(explicit(x^2,x,-5,5))$

Suppose that a firm's revenue curve is r = 5*x + 2*x^2 - 0.1*x^3.
Write the expression, binding it to the name r and determine
the values of x for which r = 0.

/*Input. Execute (ctrl-enter) for results */
r: 5*x + 2*x^2 - 0.1*x^3$
solve(r,x);

Graph r over the range defined by the relevant x 
values indicated above.

/*Input. Execute (ctrl-enter) for results */
wxdraw2d(explicit(r,x,0,5*sqrt(6)+10) );

Draw the graph once more. This time add these options:
yrange=[0,220],xlabel="x",ylabel="$ per year, thousands",.
Be sure to separate options with commas.

/*Input. Execute (ctrl-enter) for results */
wxdraw2d(yrange=[0,220],
xlabel="x",ylabel="$ per year, thousands",
explicit(r,x,0,5*sqrt(6)+10) );

/*Input. Execute (ctrl-enter) for results */
c: 50 + 0.01*x^3;

Insert key = "Revenue",

/*Input. Execute (ctrl-enter) for results */
wxdraw2d(yrange=[0,220],
xlabel="x",ylabel="$ per year, thousands",
key = "Revenue",explicit(r,x,0,5*sqrt(6)+10) ,color=black,
key="Cost",explicit(c,x,0,5*sqrt(6)+10) )$

Determine the range of values ofx over which this firm earns 
a positive profit. Define the firm's profit function, name it
P, and use find_root to determine the required x values.
(Or use allroots(P).) 

/*Input. Execute (ctrl-enter) for results */
P:r-c;
find_root(P,x,0,10);
find_root(P,x,10,20);
allroots(P);

solve(diff(P,x),x);

Use the command solnP: float(solve(diff(P,x),x)); to determine the 
value of x that results in maximum profit. 

/*Input. Execute (ctrl-enter) for results */
solnP: float(solve(diff(P,x),x));

Determine the profit level at the profit-maximizing value of x. 
Substitute solnP[2] to determine that value. Label that value P1.

/*Input. Execute (ctrl-enter) for results */
P1: subst(solnP[2],P);

Assign the name x1 to the profit-maximizing output level.

/*Input. Execute (ctrl-enter) for results */
x1: rhs(solnP[2]);

Graph the expression for profits. Label the axes the same
as the preceding graph. Or, copy the cell that produces the
preceding graph, paste it below, and replace r with P.

/*Input. Execute (ctrl-enter) for results */
wxdraw2d(yrange=[0,120],
xlabel="x",ylabel="$ per year, thousands",
explicit(P,x,0,5*sqrt(6)+10) );

Add the option  ,points([[x1,P1]]) to the graph to produce
a point at the top of the profit function. Note that [x1,P1]
must be embedded in a bracket. Don't forget the comma before
points(...).

/*Input. Execute (ctrl-enter) for results */
wxdraw2d(yrange=[0,120],
xlabel="x",ylabel="$ per year, thousands",
explicit(P,x,0,5*sqrt(6)+10), points([[x1,P1]]) );

/*Input. Execute (ctrl-enter) for results */
wxdraw2d(yrange=[0,120],
xlabel="x",ylabel="$ per year, thousands",
explicit(P,x,0,5*sqrt(6)+10),points_joined=true,
line_type=dots,
 points([[0,P1],[x1,P1],[x1,0]]) );

Use gr2d to create graphs of the profit function and its
first derivatve. The gr2d has the same syntax as draw2d. It
creates a graphic object that can be named and recalled later.
Name the first object profit and the second object dPdx.
Then use wxdraw(profit,dPdx) to generate the graph.

The set of options in dPdx can be this:
explicit(diff(P,x),x,0,5*sqrt(6)+10). That is, the 
derivative can be embedded within the gr2d command. If you
put units on the vertical axis, be aware that the vertical
axis for the profit function is in $ per time period (or
whatever currency you prefer), while the vertical axis on
the derivative function is $ per unit produced per time 
period.

/*Input. Execute (ctrl-enter) for results */
profit: gr2d(xaxis=true,title="Profit per year",
xlabel="x",ylabel="$ per year, thousands",
explicit(P,x,0,5*sqrt(6)+10) )$
dPdx: gr2d(xaxis=true,yrange=[-100,30],
title="Change in Profit per unit",
xlabel="x",ylabel="$ per unit, thousands",
explicit(diff(P,x),x,0,5*sqrt(6)+10) )$
wxdraw(profit, dPdx,dimensions =[480,480])$
/*
I added the yrange command in dPdx partly to force
three-digit values so that the vertical axes line
up properly for the two graphs.
The dimensions option is added so that the graphs are
easier to read.
The title options are optional.
*/
end;

Often an explicit representation of an expression is not
feasible, or is not the best way to proceed. Consider the
equation of a circle, x^2 + y^2 = a. One could solve this
as follows: y = (+/-)sqrt(a - x^2). That is, two explicit
functions represent the implicit function.

Graph the following two explict functions:
y = sqrt(5-x^2) and y = -sqrt(5-x^2). In both cases,
the range of x is -sqrt(5) to sqrt(5).

/*Input. Execute (ctrl-enter) for results */
wxdraw2d(
explicit(sqrt(5-x^2),x,-sqrt(5),sqrt(5)),
explicit(-sqrt(5-x^2),x,-sqrt(5),sqrt(5))
)$

Copy the cell above and paste it below. Add this option:
user_preamble="set size ratio 1",. This option forces
draw to treat the units on the two axes as equal. In 
most economics applications, the two axes will not be in
the same unit, so this adjustment is seldom used.

/*Input. Execute (ctrl-enter) for results */
wxdraw2d(user_preamble="set size ratio 1",
explicit(sqrt(5-x^2),x,-sqrt(5),sqrt(5)),
color = red, /*not required*/
explicit(-sqrt(5-x^2),x,-sqrt(5),sqrt(5))
)$

Copy the cell above and paste it below. Use the implicit
option to generate a circle with a radius of 3. This is 
the entry:,implicit(x^2+y^2=9,x,-3,3,y,-3,3) or
,implicit(x^2+y^2-9,x,-3,3,y,-3,3). If you like, you 
can change the color or line width to distinguish this 
circle.

/*Input. Execute (ctrl-enter) for results */
wxdraw2d(user_preamble="set size ratio 1",
explicit(sqrt(5-x^2),x,-sqrt(5),sqrt(5)),
color = red,/*This color change is not required*/
explicit(-sqrt(5-x^2),x,-sqrt(5),sqrt(5)),
line_width=2,
implicit(x^2+y^2=9,x,-3,3,y,-3,3)
)$

In some of our material, two variables' values are individually 
determined by a third variable. Stay with the case of a circle.
Use the command wxdraw2d(parametric(sin(x),cos(x),x,-%pi,%pi))$
to draw a circle with a radius of 1.

Optional: Add the user_preamble to force a circular appearance.
Optional: Add a second command with 2*sin and 2*cos.

/*Input. Execute (ctrl-enter) for results */
wxdraw2d(user_preamble="set size ratio 1",
xaxis=true,yaxis=true, key="radius = 1",/*not required*/
parametric(sin(x),cos(x),x,-%pi,%pi),
color=red, key="radius = 2", /*not required*/
parametric(2*sin(x),2*cos(x),x,-%pi,%pi)   )$