Functions
Functions can be defined in several useful ways in Maple.
As an expression: The assignment
> area := Pi*r^2;
defines the area of a circle as a function of it's radius. The area function defined as an expression is evaluated with subs. Since this function assigns real numbers to real numbers, its values can be plotted on a graph with the Maple word plot. So the statement
> plot(area,r=0..4);
will produce in a separate plot window, the graph of the area function over the interval from
to
.
> area := r -> Pi*r^2;
defines the area function also. Now to find the area of a circle of radius 3, we simply enter the statement
> area(3);
To plot this function over the domain r = 0..4 , say
> plot(area,0..4);
Note that the variable r is omitted here.
> pol := x^2 + 4*x -1;
then the assignment
> pol := unapply(pol,x);
turns pol into a function defined by an arrow operator.
As a procedure : The Maple word proc can be used to define functions. For example,
> area := proc(r) Pi*r^2 end;
> area := proc(r)
> if r <= 0 then ERROR("radius must be positive") else
> Pi*r^2 fi end;
> area(3);
> area(-3);
Error, (in area) radius must be positive
Functions of two variables can be defined and plotted just as easily in Maple as functions of one variable. For example, the volume V of a cylinder of height h and radius r is defined by
> V := (r,h) -> Pi*r^2*h;
To see what the graph of V looks like, use plot3d .
> plot3d(V,0..4,0..4,axes=boxed);
Which way of defining a function is the preferred way? That really depends on the situation. The expression method works well for functions which have only one rule of evaluation, but eventually you cannot avoid using an -> or proc definition. You will find yourself using arrow or proc definitions more and more as time goes by.
Piecewise defined functions:
> f :=unapply(piecewise(x <= -1,x^3+8, x <= 2,7+ 2*x, x <= 4, 11 - cos(x),3*x),x);
> f(2);
When plotting piecewise defined functions, sometimes style = point is better.
> plot(f, -3..6,style= point);
You can also plot discontinuous functions by setting discont = true.
> plot(f, -3..6,discont=true,color=blue);
>
>
Composition of functions
One of the important skills needed for calculus and other mathematics is the skill to see a function as built up from simpler functions by the 4 arithmetic operations plus composition. In Maple these operations are named by +, *, -, /, and @. So for example the function
can be seen as the quotient of g (x) =
by h(x) =
.
>
> g := x-> x^2 +1; h := x->sqrt(x^3-1);
> f := g/h;
> f(x);
This analysis of a function into simpler functions is not unique and can often be extended further. For example, the denominator function h is seen to be the composition of the square root function
with the cubic polynomial function
.
> k := x-> x^3 -1;
> f := g/ (sqrt @ k) ;
> f(x);
Problem:
Define three functions g,h, and k so that the function f defined by
is