STUDENT LAB USE OF MAPLE (PART 1)
> restart:#PRESS ENTER TO START
Mathematics 100 Lab 2
FUNCTIONS
FUNCTION NOTATION
A function in Maple is a command name which tells Maple which operations to apply to an input. We have already used some of the built in functions like sin and sqrt . Note that sin is the name of the function whereas sin(x) is the function applied to the input x .
If we want to build our own functions, we have to define our rule for Maple. For example, to define the squaring function, we would define the rule as x->x^2 . This rule tells Maple to take the input and square it. (The -> is supposed to look like an arrow.) We read this as x goes to x^2 or x is mapped to x^2 . To apply our rule, we can use the rule itself in parenthesis as a command name:
Type (x->x^2)(4);
>
Type (x->x^2)(t);
As you can see, using the rule as a command name is awkward. We can make our defined rules look like more familiar functions by assigning them names. The following line assigns the name f to our squaring rule.
Type f:=x->x^2;
Recall that := is the assignment operator in Maple. Once a rule has been assigned a name, the name is now a Maple command and can be used to apply the rule:
Type f(4);
Type f(t);
Type f(a+1);
Type expand(f(a+1));
Type f(f(x)+1);
One must be clear about the difference between f and f(x) . f is the name assigned to the rule whereas f(x) is the expression which results from applying the rule to x .
BUILT IN FUNCTIONS
Maple has a large number of predefined functions. To use them, we need only refer to them by name. For example, to define the function , we type
Type f:=x->x*cos(x);
Note that if we define a "new" function by assigning a name to a built in function, Maple simply assigns our name to the name of the built in function.
Type f:=x->cos(x);
PIECEWISE FUNCTIONS
Maple has a special command for defining piecewise functions. The syntax is
piecewise( condition 1 , expression 1 , condition 2 , expression 2 );
Lets define p to be the function which is x if x<2 or -x if x>2
> p:=x->piecewise(x<2,x,x>2,-x);
To check our definition, evaluate at x :
> p(x);
Lets test it:
> p(1);
> p(3);
FUNCTION ALGEBRA
Once functions have been defined, we can create new functions from them: Define f to be the squaring function,
> f:=x->x^2;
define g to be the function,
> g:=x->sin(2*x);
and define h to be the function
> h:=x->(1-x)/(2-x);
Then, we can add, multiply, and compose these functions:
> (f+g)(x);
> (f*g)(x);
> f(g(x));
(Note how the square applied to the function parenthesis means the whole function squared in Maple.)
Type h(f(g(x)));
Type simplify(%);
BASIC PLOTTING
Once a function f is defined, we can use Maple to plot y=f(x) . The syntax to plot y=f(x) over the interval is
plot(f(x),x=a..b);
Define a function:
Type f:=x->x*sin(2*x);
Type plot(f(x),x=0..2*Pi);
We can also plot a list of functions on the same graph with the syntax
plot([f(x),g(x)],x=a..b);
Type plot([cos(x),x*cos(x)],x=0..2*Pi);
One can also control the y range of the plot by including the option y=c..d :
Type plot(x^2,x=0..2,y=0..3);
There are many more plotting options which we will see later in Lab 5.
You are now finished the tutorial part of the lab. You may continue and open the optional examples by simply pressing enter or, you may skip the optional examples and continue to type your own commands by scrolling past the optional examples and clicking on the last command prompt.
> #press enter here to open optional examples
OPTIONAL EXAMPLES
COMMON ERRORS IN DEFINING FUNCTIONS
Note that the following examples DO NOT DEFINE functions.
1. The "naming an expression instead of a function" mistake:
> f(x):=x^3;
> f(x);
Looks okay but
> f(t);
The function x->x^3 was not defined and named f . Instead, the expression x^3 was named f(x) . The name looks like a function but is only a four symbol name for an expression!
2. The "refering to the last output" mistake:
> x^3+5*x+6;
> f:=x->%;
You get nothing! Maple does not evaluate the right hand side of the function definition when the function is defined so it never found out what % refered to. See the next example for the correct way to create a function from an output expression.
FUNCTIONS FROM EXPRESSIONS
A function can be created from an expression that Maple has output using the unapply(,) command.
> f:=unapply(%,x);
Use the following command prompt to input your own commands.