Section 5: Functions: Defining, Evaluating and Graphing
In this section you will learn how to define a function in Maple. The remainder of the section covers evaluating functions, solving equations with functions, and graphing functions.
Defining and Clearing a Function in Maple
Maple requires special notation when defining a function , as opposed to defining an expression .
For example, the function is defined by:
Take note of the syntax here. The "arrow" is necessary, and is made by typing (with no intervening space) a "minus sign" and a "greater than" symbol.
Warning: Maple will not define a function if you type either
f(x) := cos(Pi*x)+3;
or
f := cos(Pi*x)+3;
The first input fails to define the desired function because of the assignment to the symbol . In general, this kind of assignment should be avoided in Maple.
The second input creates an expression , not a function .
Below is a comparison of an expression and a function. Note the difference in syntax and how Maple returns the output for each.
This is the expression,
and this is the function.
Generally, functions require an arrow when typed in from the keyboard. When created this way, the Maple echo should also have an arrow in its output. Always check the output for the arrow to confirm that you have in fact defined a function.
Exercise 5.1
In the workspace below, enter the function .
Student Workspace 5.1
Answer 5.1
Once you have defined a function, Maple will remember that function during your entire working session. If you want to overwrite the function with a new definition, you simply retype the definition.
For example, if you want to replace the function above with , type:
We can confirm the current value for the function :
If you want to clear the function without redefining it, type:
It's always a good idea to clear your functions when you start a new problem. Alternatively, you can use restart; to clear everything from memory.
Exercise 5.2
If the following three commands
restart;
f(x) := x^2;
f(3);
are entered in the workspace below, the output will not be the expected
= 9
Revise this sequence of three commands so the function is correctly defined and evaluated at .
Student Workspace 5.2
Answer 5.2
The Maple commands
do not create a function. In fact, it is generally not wise to assign to the symbol .
The correct way to enter this function and evaluate it at would be
Exercise 5.3
f := x^2;
Student Workspace 5.3
Answer 5.3
create an expression , not a function . To create the function and evaluate it at , use the syntax
Evaluating a Function
Once a function has been defined, you can evaluate it at various values or literal expressions using function notation. It's always a good idea to clear the function name first before entering a new function.
Now, assign a new function to f .
Typing f(a); will give you the value of f at the point . For example:
Maple will leave expressions such as sqrt symbolic, as shown below.
To obtain a floating-point (numerical) value, use the evalf command.
The value of x does not have to be numerically computable. For example:
To simplify this expression, use the simplify command.
Furthermore, you can manipulate the results of f(x); directly. For example, the Newton (or difference) quotient for this function would be
which can be simplified, via the simplify command, to
Given the second function
the composition is obtained with the natural notation
To assign the composite function the name , use
The specific value is then
Exercise 5.4
Define the function then have Maple calculate , and and simplify your results.
Don't forget the arrow notation!
Student Workspace 5.4
Answer 5.4
For :
To simplify the result:
Notice that if you define a function, there is no need to use the eval command like you do with expressions.
Solving Equations Involving Functions
Once your function is defined, you can solve equations containing this function either exactly or approximately. If the function is
the exact solution of the equation is obtained with
whereas an approximate (numerical) solution is obtained with
Graphing a Function
The plot function works the same for functions, provided the function is evaluated at its argument.
For example, consider the function
whose graph is generated via
Several functions can be graphed simultaneously just at we did for expressions, again, provided that the functions are evaluated at their arguments.
Consider the function , entered into Maple as
Below we graph this function along with the horizontal shifts , and . Can you identify each?
By not assigning unique and recognizable colors to the functions plotted, we have created a real puzzle that could have been avoided with syntax such as
The order of the functions corresponds to the order of the colors!
Exercise 5.5
Define the function then answer the following questions.
a) Find the value of f(6.5)
b) Simplify the expression where z is a variable
c) Plot a graph of
d) Find all values of x such that .
Student Workspace 5.5
Answer 5.5
First define the function .
a) To find the value of f at x = 6.5:
b) First evaluate f at then simplify the expression using the simplify command.
c) Use the plot command.
d) We can use the fsolve command to find the solutions in the intervals [0, 2] and [3, 4].
Exercise 5.6
Define the functions and then do the following.
a) Plot a graph that shows both functions and . Experiment with different values for domain and range.
b) Estimate the coordinates of the point of intersection of these two graphs by using left mouse-button click.
c) Use fsolve to solve the equation . How does the solution of this equation relate to your answer to part (b).
Student Workspace 5.6
Answer 5.6
First declare the functions.
a) To plot a graph that shows both functions, simply put the functions in a list using square brackets [ ].
c) To solve the equation using fsolve , type:
The solution to is the x -coordinate of the point of intersection of and . To find the corresponding y -coordinate of the intersection point evaluate either or at this value.
Exercise 5.7
Define the function , then do the following:
a) Plot the graph of this function on the domain [-1, 8] .
b) Modify your plot from part (a) to include the horizontal line . Use this new plot to estimate the number and approximate values for x such that .
c) What single function could you graph that would give you the same information as in part (b)
d) Use Maple's fsolve command to approximate all solutions to the equation .
Student Workspace 5.7
Answer 5.7
a) First declare the function via
then plot using the plot command, obtaining
b) As it can be seen from the following, there appears to be three intersection points at x = 3.25 ,4.825 and 5.95 .
c) We could graph and look for x -intercepts. These will correspond to x -values found in part (b).
Here are the solutions using fsolve :
Converting Expressions to Functions
If an expression such as
already appears in Maple, it can be made into a function only by use of the unapply command, whose syntax is
The expression is now the function , so that evaluation at, for example, , is accomplished with
Exercise 5.8
If the following four commands
k := x^2 + 5;
f := x -> k;
f(2);
are entered in the workspace below, the output will not be
because the function will not have been correctly formulated in Maple.
Show the proper way to convert the expression to a function so the value of is correctly computed.
Student Workspace 5.8
Answer 5.8
Entering
creates an expression . Expressions that already exist in Maple cannot be "functionalized" with an arrow. Hence the following commands fail.
The unapply command must be used.
The correct syntax would be