Functions

A function is a rule f (possibly very complicated) for assigning to each argument x in a given set, a unique value f(x) in a set. In calculus the arguments and values of a function are always real numbers, but the notion of function is much more flexible than that.

Functions can be defined in several useful ways in Maple.

As an expression: The assignment

> area := Pi*r^2;

[Maple Math]

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);

[Maple Plot]

will produce in a separate plot window, the graph of the area function over the interval from [Maple Math] to [Maple Math] .

With the arrow operator the assignment: If you have a simple function, you can often use the arrow operator . For example,

> area := r -> Pi*r^2;

[Maple Math]

defines the area function also. Now to find the area of a circle of radius 3, we simply enter the statement

> area(3);

[Maple Math]

To plot this function over the domain r = 0..4 , say

> plot(area,0..4);

[Maple Plot]

Note that the variable r is omitted here.

Use unapply . The ugly little word unapply transforms expressions of one or more variables into fuctions defined by an arrow operator. For example, if we had a polynomial defined by the assignment

> pol := x^2 + 4*x -1;

[Maple Math]

then the assignment

> pol := unapply(pol,x);

[Maple Math]

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;

[Maple Math]

defines the area function too. It is evaluated and plotted as in the arrow operator definition. One advantage of this way of defining a function is that the domain can be specified. For example, the domain of the area function for a circle is all positive real numbers. This can be inserted into the procedure, with the Maple word ERROR . The message must be enclosed in backquotes ` or doublequotes ", which is on the key with the tilde .

> area := proc(r)

> if r <= 0 then ERROR("radius must be positive") else

> Pi*r^2 fi end;

[Maple Math]

> area(3);

[Maple Math]

> area(-3);

Error, (in area) radius must be positive

Note the if..then..fi control statement here. You can learn more about the word if by typing ?if in an input cell and entering it.

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;

[Maple Math]

To see what the graph of V looks like, use plot3d .

> plot3d(V,0..4,0..4,axes=boxed);

[Maple Plot]

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:

Many functions can only be described by stating various rules for various parts of the domain. The Maple word piecewise will help with defining such functions. Here is an example to show usage.

> f :=unapply(piecewise(x <= -1,x^3+8, x <= 2,7+ 2*x, x <= 4, 11 - cos(x),3*x),x);

[Maple Math]

> f(2);

[Maple Math]

When plotting piecewise defined functions, sometimes style = point is better.

> plot(f, -3..6,style= point);

[Maple Plot]

You can also plot discontinuous functions by setting discont = true.

> plot(f, -3..6,discont=true,color=blue);

[Maple Plot]

>

>

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 [Maple Math] can be seen as the quotient of g (x) = [Maple Math] by h(x) = [Maple Math] .

>

> g := x-> x^2 +1; h := x->sqrt(x^3-1);

[Maple Math]

[Maple Math]

> f := g/h;

[Maple Math]

> f(x);

[Maple Math]

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 [Maple Math] with the cubic polynomial function [Maple Math] .

> k := x-> x^3 -1;

[Maple Math]

> f := g/ (sqrt @ k) ;

[Maple Math]

> f(x);

[Maple Math]

Problem: Define three functions g,h, and k so that the function f defined by [Maple Math] is [Maple Math]