Table of Contents

Back to Maple Tutorial Table of Contents

What Is a Function?

A function is a special type of variable that stores an equation. This equation can be evaluated at different values.

Top

How Are a Function and a Variable Different?

A function allows you to evaluate at a given number. A variable is a static constant. If you want Maple to remember a number for you, use a variable. If you want to store an equation, use a function.

Top

Defining a One-Variable Function

The procedure is relatively the same as defining a variable.

> f:=x->x^2;

[Maple Math]

Notice the "x->". This is typed by pressing x, the hyphen key, and the greater than sign. This tells Maple which variable is the input variable.

Another example:

> g:=y->y^3;

[Maple Math]

> h:=theta->sin(theta)^2+theta;

[Maple Math]

 

Top

Evaluating a Function at a Given Value

> f(2);

[Maple Math]

> g(f(2));

[Maple Math]

> h(Pi);

[Maple Math]

> h(Pi/4);

[Maple Math]

 

Top

Displaying a Function

Unlike a variable, you cannot use it's name to view it. You must evaluate it at an unknown variable. Most of the time, you will want to evaluate it at the variable you used when you defined it.

Example:

> z:=a->a^2+a;

[Maple Math]

> z(a);

[Maple Math]

Using this method, you can find the value of a function at some other variable.

> z(b);

[Maple Math]

You can also insert expressions:

> z(theta+2);

[Maple Math]

Note that other procedures with functions are identical to those of variables. Example:

> h(x);

[Maple Math]

> h:='h';

[Maple Math]

> h(x);

[Maple Math]

 

Top

Defining a Multi-Variable Function

> f:=(x,y)->x^2+y^3;

[Maple Math]

> f(2,3);

[Maple Math]

Note the (x,y). This tells Maple that the function will have two variables. As above, separate the two variables with a comma. You must include the parenthesis.

 

Top

Defining a "Multi-Valued" Function

This type of function takes more than one equation and does multiple computations. It's useful if you want to compute different computations with a number. (Example: sin(x), cos(x), and tan(x).)

> g:=x->x^2,x->x^3,x->x^4;

[Maple Math]

> g(2);

[Maple Math]

 

Top