Ryerson Crest Ryerson Header

MTH 207 Lab Lesson 3

Functions


Up to Main Lab Page Next Lesson - Plotting Functions Previous Lesson - Expressions

Topics for this Lab: Standard Maple Functions Composition of Functions Piecewise Functions Special Points Unapply


Expressions are a useful tool, however they can become confusing as the variables all are global. Changing x changes the value of any expression involving x. This is not usually how we want things to behave.

We really want to be able to define a mathematical function and use the f(x) notation. To do this we use the -> operator.
This tells Maple to define the function f(x) = x^2+3*x+4, the global value of x is irrelevent. Try
> f := x -> x^2+3*x+4;
> x:=7;
> f(2);
> f(n);

  1. Define f(x) to be the polynomial x^3-2x^2-3x+2 and g as the function n^2.
    1. Find f(2), f(56), f(-28) and f(304).
    2. Find f as a function of y, i.e. f(y).
    3. Find f(g(x)).
    4. Find g(f(x)).

Standard Maple Functions

Maple knows a number of standard mathematical functions:

Calculus Functions:
exp the exponential function: exp(x) = sum(x^i/i!,i=0..infinity) = x -> E^x
ln natural logarithm (logarithm with base E = 2.71828...)
log logarithm to arbitrary base
log10 log to the base 10
max, min maximum/minimum of a list of real values
sqrt square root

The trigonometric functions: sin, cos, tan, sec, csc, cot.
The hyperbolic functions: sinh, cosh, tanh, sech, csch, coth
The inverse trigonometric functions:arcsin, arccos, arctan, arcsec, arccsc, arccot, arctan.
The inverse hyperbolic functions:arcsinh, arccosh, arctanh, arcsech, arccsch, arccoth.

Complex Functions:
argument argument of a complex number or expression
conjugate conjugate of a complex number or expression
abs absolute value of real or complex argument

Integer functions:
binomial binomial coefficients: binomial(n,r) = n!/(r!*(n-r)!)
ceil ceil(x) = smallest integer greater than or equal to x
factorial the factorial function factorial(n) = n!
floor floor(x) = greatest integer less than or equal to x
round round(x) = nearest integer to x (round(.5) = 1)
signum sign function for real and complex expressions
trunc trunc(x) = nearest integer from x in the direction of 0

You can use Maple's help to find the exact syntax of these functions (though most of them are obvious). To use Maple's help type in the word and press control F1, or use the keyword search or browser from the help menu.

  1. Define f(x) to be the function sin(x)/x
    1. Find f(y).
    2. Find f(0.005).
    3. Find f(0.000000000001).
    4. Set Digits to 100 and find f(0.000000000001).
    5. Try to find f(0) - what happens and why?

  2. Define f(x) to be the function ln(x), g(x) to be sqrt(x) and h(x) to be f(x)/g(x).
    1. Find f(-1).
    2. Find g(-1)
    3. Find h(-1).
    4. Why is the answer for h incorrect if we are working over the reals?

The last question shows that we must be careful about function domains in Maple.

Composition of Functions

Given two functions f and g, we may define f composed with g by using the @ symbol.

> f:=x->sqrt(x);
> g:=x->sin(x);
> (f@g)(x);
Note the brackets around f@g.

  1. Define f(x) to be the function x2, g(x) to be sin(x) and h(x) to be cos(x).
    1. Find f composed with g.
    2. Find f composed with h.
    3. Find the sum of f composed with g and f composed with h.

Piecewise Functions

Many mathematical functions are defined piecewise, that is they have different definitions on different intervals. Thus for example
f(x) = { x if x < 0
x2 + 1 if x > 0
Maple uses the piecewise function to allow you to define piecewise functions.
The syntax is piecewise(range 1, value on range 1, range 2, value on range 2, ..., range n, value on range n, value otherwise).
Thus the function above could be defined by
> f:=x->piecewise(x < 0, x, x > 0, x^2 + 1);

NOTE: piecewise only works in version 4 or above of Maple. In version 3 you must use the Heaviside function to define piecewise functions.

In order to define ranges you may use any of the following symbols:

Symbol SyntaxExplantion
> x > a Values of x which are greater than a
< x < a Values of x which are less than a
>= x >= a Values of x which are greater than or equal to a
<= x <= a Values of x which are less than or equal to a
<> x <> a Values of x which are not equal to a
and Intersection of two ranges
or Union of two ranges
So for example the function:
f(x) = { x if x < 0
x2 + 1 if 0 <= x < 1
x2 Otherwise
Would be given by
> f:=x->piecewise(x < 0, x, x >= 0 and x < 1, x^2 + 1, x^2);

  1. Define the following functions:
    1. f(x) = { ex if x < 0
      sin2(x) if 0 <= x < PI
      sqrt(cos(x)) Otherwise
    2. The Federal tax rate is 0% for income under $6000, 17% for income from $6000 to $25,000, and 26% for income over $30,000. Write a function which calculates the tax payable given the income.
    3. Provincial tax is 17% of Federal tax. Write a function to compute total tax payable given the federal tax payable.
    4. Use composition to create a function which computes the total tax payable (Federal and Provincial), given the income.

Special points

Certain functions have special values defined at particular points. For example
f(x) = { 1 if x = 0
x/x otherwise
To do this in Maple we redefine these points after defining the function. Thus the above function would be
>f(x):=x->x/x;
>f(0) := 1;

We can define as many points as we like in this way.

Changing Between Functions and Expressions

Sometimes it is convenient to change a function into an expression, or visa versa.
We may turn an expression into a function by using unapply.

unapply turns an expression into a function.
The syntax is unapply(a, x), where a is an expression and x is the variable in the definition of a which is to be made into a free variable.
> x:='x';
> a:=x^2;
> f:=unapply(a,x);
> a;
> x:=2;
> a;
> f(y);
> f(4);

Note that x must be undefined when unapply is executed. The following will generate an error.
> a:=x^2;
> x:=2;
> f:=unapply(a,x);
So will
> a:=x^2;
> x:=c+1
> f:=unapply(a,x);

However the following is OK.
> a:=x^2;
> x:=c+1
> f:=unapply(a,c);

  1. Turn the following Expressions into functions f, g, h etc, the variables on the right are always assumed to be free.
    1. a:=x^2+1;
    2. y:=sin(a);
    3. x:=ln(y);

It is always possible to turn a function into an expression by assigning it. That is, if f is a function then f(x) is an expression.
> f:=x->x^2;
> a:=f(z);
Now try
> a;
> z:=2;
> a;
> f(5);


Up to Main Lab Page Next Lesson - Plotting Previous Lesson - Expressions Top of this Lesson


Maintained by: P. Danziger, January 1998