Application Center - Maplesoft

App Preview:

Maple Essentials: Section 5: Functions: Defining, Evaluating and Graphing

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

section5.mws

Section 5: Functions: Defining, Evaluating and Graphing

In this section you will learn how to define a function f(x)  in Maple. The remainder of the section covers evaluating functions, solving equations with functions, and graphing functions.

>    restart;

>   

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 f(x) = cos(Pi*x)+3  is defined by:

>    f := x -> cos(Pi*x)+3;   

f := proc (x) options operator, arrow; cos(Pi*x)+3 end proc

>   

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 f(x) .  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,

>    y := (x+2)/(x^3+5*x+2);

y := (x+2)/(x^3+5*x+2)

>   

and this is the function.

>    f := x -> (x+2)/(x^3+5*x+2);

f := proc (x) options operator, arrow; (x+2)/(x^3+5*x+2) end proc

>   

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 h(x) = x^3*sin(2*x+1) .

Student Workspace 5.1

>   

>   

>   

Answer 5.1

>    h := x -> x^3*sin(2*x+1);

h := proc (x) options operator, arrow; x^3*sin(2*x+1) end proc

>   

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 f(x)  above with ln(cos(5*x)) , type:

>    f := x -> ln(cos(5*x));

f := proc (x) options operator, arrow; ln(cos(5*x)) end proc

>   

We can confirm the current value for the function f(x) :

>    f(x);

ln(cos(5*x))

>   

If you want to clear the function f(x)  without redefining it, type:

>    unassign('f');

>   

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

f(3) = 3^2  = 9

Revise this sequence of three commands so the function f(x) = x^2  is correctly defined and evaluated at x = 3 .

>   

Student Workspace 5.2

>   

>   

>   

Answer 5.2

The Maple commands

restart;

f(x) := x^2;

do not create a function.  In fact, it is generally not wise to assign to the symbol f(x) .

The correct way to enter this function and evaluate it at x = 3  would be

>    restart;
f := x -> x^2;
f(3);

f := proc (x) options operator, arrow; x^2 end proc

9

>   

Exercise 5.3

If the following three commands

restart;

f := x^2;

f(3);

are entered in the workspace below, the output will not be the expected

f(3) = 3^2  = 9

Revise this sequence of three commands so the function f(x) = x^2  is correctly defined and evaluated at x = 3 .

>   

Student Workspace 5.3

>   

>   

>   

Answer 5.3

The Maple commands

restart;

f := x^2;

create an expression , not a function .  To create the function and evaluate it at x = 3 , use the syntax

>    restart;
f := x -> x^2;
f(3);

f := proc (x) options operator, arrow; x^2 end proc

9

>   

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.

>    unassign('f');

>   

Now, assign a new function to f .

>    f := x -> 3*x+x^2;

f := proc (x) options operator, arrow; 3*x+x^2 end proc

>   

Typing f(a);   will give you the value of f  at the point x = a . For example:

>    f(-1);

-2

>   

Maple will leave expressions such as sqrt  symbolic, as shown below.
 

>    f(2+sqrt(5));

6+3*5^(1/2)+(2+5^(1/2))^2

>   

To obtain a floating-point (numerical) value, use the evalf  command.

>    evalf(f(2+sqrt(5)));

30.65247584

>   

The value of x  does not have to be numerically computable. For example:

>    f(x+4);

3*x+12+(x+4)^2

>   

To simplify this expression, use the simplify  command.

>    simplify(f(x+4));

11*x+28+x^2

>   

Furthermore, you can manipulate the results of f(x);  directly. For example, the Newton (or difference) quotient for this function would be

>    NQ := (f(x+h)-f(x))/h;

NQ := (3*h+(x+h)^2-x^2)/h

>   

which can be simplified, via the simplify  command, to

>    simplify(NQ);

3+2*x+h

>   

Given the second function

>    g := x -> cos(x)+1;

g := proc (x) options operator, arrow; cos(x)+1 end proc

>   

the composition f(g(x))  is obtained with the natural notation

>    f(g(x));

3*cos(x)+3+(cos(x)+1)^2

>   

To assign the composite function f(g(x))  the name h(x) , use

>    h := x -> f(g(x));

h := proc (x) options operator, arrow; f(g(x)) end proc

>   

The specific value f(g(Pi/3))  is then

>    f(g(Pi/3));

27/4

>   

or

>    h(Pi/3);

27/4

>   

Exercise 5.4

Define the function s(t) = (3+t^2)/sqrt(3*t+1)   then have Maple calculate s(2), s(t-3) , and s(t)-s(3)  and simplify your results.

Don't forget the arrow notation!

Student Workspace 5.4

>   

>   

>   

>   

>   

>   

>   

>   

Answer 5.4

>    s := t -> (3 + t^2)/(sqrt(3*t+1));

s := proc (t) options operator, arrow; (3+t^2)/sqrt(3*t+1) end proc

>   

For s(2) :

>    s(2);

7^(1/2)

>   

For s(t-3) :

>    s(t - 3);

(3+(t-3)^2)/(3*t-8)^(1/2)

>   

To simplify the result:

>    simplify(s(t-3));

(12+t^2-6*t)/(3*t-8)^(1/2)

>   

For s(t)-s(3) :

>    s(t) - s(3);

(3+t^2)/(3*t+1)^(1/2)-6/5*10^(1/2)

>   

To simplify the result:

>    simplify(s(t)-s(3));

1/5*(15+5*t^2-6*10^(1/2)*(3*t+1)^(1/2))/(3*t+1)^(1/2)

>   

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

>    unassign('g');
g := t -> t^3-6*t^2+6*t+8;

g := proc (t) options operator, arrow; t^3-6*t^2+6*t+8 end proc

>   

the exact solution of the equation g(t) = 0  is obtained with

>    solve(g(t)=0,t);

4, 1+3^(1/2), 1-3^(1/2)

>   

whereas an approximate (numerical) solution is obtained with

>    fsolve(g(t)=0,t);

-.7320508076, 2.732050808, 4.

>   

Graphing a Function

The plot  function works the same for functions, provided the function is evaluated at its argument.

For example, consider the function

>    unassign('h','y','x');   
h := x -> x*exp(-x);

h := proc (x) options operator, arrow; x*exp(-x) end proc

>   

whose graph is generated via

>    plot(h(x),x=-1..4,y=-2..1);

[Maple Plot]

>   

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    f(x) = 4/(x^2+1)  , entered into Maple as

>    f := x -> 2/(x^2+1);

f := proc (x) options operator, arrow; 2/(x^2+1) end proc

>   

Below we graph this function along with the horizontal shifts f(x+1)  , f(x-3) and f(x-6)  . Can you identify each?

>    plot([f(x),f(x+1),f(x-3),f(x-6)],x=-5..10,y=-1..3);

[Maple Plot]

>   

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

>    plot([f(x),f(x+1),f(x-3),f(x-6)],x=-5..10,y=-1..3, color=[black,red,green,blue]);

[Maple Plot]

>   

The order of the functions corresponds to the order of the colors!

>   

Exercise 5.5

Define the function   f(x) = 2*x-abs(x^2-5)   then answer the following questions.

a) Find the value of  f(6.5)

b) Simplify the expression f(z-4)  where z  is a variable

c) Plot a graph of f(x)  

d) Find all values of x such that f(x) = 0 .

Student Workspace 5.5

>   

>   

>   

>   

>   

>   

>   

>   

>   

Answer 5.5

First define the function f(x) .

>    f := x -> 2*x-abs(x^2-5);

f := proc (x) options operator, arrow; 2*x-abs(x^2-5) end proc

>   

a) To find the value of f at x  = 6.5:

>    f(6.5);

-24.25

>   

b) First evaluate f  at x = z-4  then simplify the expression using the simplify  command.

>    simplify(f(z-4));

2*z-8-abs(z^2-8*z+11)

>   

c) Use the plot  command.

>    plot(f(x),x);

[Maple Plot]

>   

d) We can use the fsolve  command to find the solutions in the intervals [0, 2] and [3, 4].

>    fsolve(f(x)=0,x=0..2);
fsolve(f(x)=0,x=3..4);

1.449489743

3.449489743

>   

Exercise 5.6

Define the functions   g(x) = 5*exp(-.5*x)   and   h(x) = x+1   then do the following.

a) Plot a graph that shows both functions g(x)  and h(x) . 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 g(x) = h(x) . 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.

>    g := x -> 5*exp(-0.5*x);
h := x -> x+1;

g := proc (x) options operator, arrow; 5*exp(-.5*x) end proc

h := proc (x) options operator, arrow; x+1 end proc

>   

a) To plot a graph that shows both functions, simply put the functions in a list using square brackets [ ].

>    plot([g(x),h(x)],x=-5..5,g=-20..20);

[Maple Plot]

>   

>    plot([g(x),h(x)],x=1..2,g=1..4);

[Maple Plot]

>   

c) To solve the equation using fsolve , type:

>    xval := fsolve(g(x)=h(x),x);

xval := 1.437186898

>   

The solution to g(x) = h(x)  is the x -coordinate of the point of intersection of g(x)  and h(x) . To find the corresponding y -coordinate of the intersection point evaluate either g(x)  or h(x)  at this value.

>    g(xval);
h(xval);

2.437186898

2.437186898

>   

Exercise 5.7

Define the function k(x) = x+3*sin(2*x)  , 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 y = 4 .  Use this new plot to estimate the number and approximate values for x such that k(x) = 4 .

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 k(x) = 4 .

Student Workspace 5.7

>   

>   

>   

>   

>   

>   

>   

>   

Answer 5.7

 a)  First declare the function via

>    k := x -> x+3*sin(2*x);

k := proc (x) options operator, arrow; x+3*sin(2*x) end proc

>   

then plot using the plot  command, obtaining

>    plot(k(x),x=-1..8);

[Maple Plot]

>   

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 .

>    plot([k(x),4],x=-1..8);

[Maple Plot]

>   

c) We could graph k(x)-4  and look for x -intercepts. These will correspond to x -values found in part (b).

>    plot(k(x)-4,x=-1..8);

[Maple Plot]

>   

Here are the solutions using fsolve :

>    fsolve(k(x)=4,x=2 .. 3.5);
fsolve(k(x)=4,x=3.5 .. 5);
fsolve(k(x)=4,x=5 .. 7);

3.265300793

4.857290511

5.933090071

>   

Converting Expressions to Functions

If an expression such as

>    Q := x*sin(x)-exp(2*x);

Q := x*sin(x)-exp(2*x)

>   

already appears in Maple, it can be made into a function only  by use of the unapply  command, whose syntax is

>    f := unapply(Q,x);

f := proc (x) options operator, arrow; x*sin(x)-exp(2*x) end proc

>   

The expression is now the function f(x) = x*sin(x)-exp(2*x) , so that evaluation at, for example, x = 1 , is accomplished with

>    f(1);

sin(1)-exp(2)

>   

Exercise 5.8

If the following four commands

restart;

k := x^2 + 5;

f := x -> k;

f(2);

are entered in the workspace below, the output will not be

f(2) = 2^2+5  = 9

because the function f(x) = x^2+5  will not have been correctly formulated in Maple.

Show the proper way to convert the expression k  to a function f(x)  so the value of f(2)  is correctly computed.

>   

Student Workspace 5.8

>   

>   

>   

>   

Answer 5.8

Entering  

>    restart;
k := x^2+5;

k := x^2+5

>   

creates an expression .  Expressions that already   exist  in Maple cannot be "functionalized" with an arrow.   Hence the following commands fail.

>    f := x -> k;
f(2);

f := proc (x) options operator, arrow; k end proc

x^2+5

>   

The unapply  command must be used.

The correct syntax would be

>    restart;
k := x^2+5;
f := unapply(k,x);
f(2);

k := x^2+5

f := proc (x) options operator, arrow; x^2+5 end proc

9

>   

>