Section 5: Functions: Defining, Evaluating and Graphing

© 2000 Seattle Central Community College. Reproduced with permission

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

To distinguish a function from an expression, Maple requires special notation when defining a function. For example, the function f(x) = cos(Pi*x)+3 is defined as:

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

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

Take note of the syntax here. It is absolutely necessary to type the "arrow" - > made by typing a "minus sign" and a "greater than" symbol. Maple will not define a function if you type f(x):=cos(Pi*x)+3 ;

Below is a comparison of an expression and a function. Note the difference in syntax and how Maple returns the output for each.

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

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

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

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

Functions always require an arrow when typing in; Maple 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

Enter the function h(x) = x^3*sin(2*x+1) in the workspace below.

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+...

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 5x), type:

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

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

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:

> f:='f';

f := '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.

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.

> f:='f';

f := 'f'

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

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

> f(-1);

-2

> f(2+sqrt(5));

6+3*sqrt(5)+(2+sqrt(5))^2

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

30.65247584

> f(x+4);

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

> simplify(%);

11*x+28+x^2

> (f(x+h)-f(x))/h;

(3*h+(x+h)^2-x^2)/h

> simplify(%);

3+2*x+h

If more than one function is involved, composing functions is easy to do.

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

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

> f(g(Pi/3));

27/4

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

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

> j(x);

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

Exercise 5.2

Exercise 2 : Define the function s(t) = (3+t^2)/sqrt(3*t+1) then have Maple calculate

s(2),and s(t-3) and s(t) - s(3) and simplify your results. Don't forget the arrow notation!

Student Workspace 5.2

>

>

>

>

>

>

>

>

Answer 5.2

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

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

> s(2);

sqrt(7)

> s(t - 3);

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

> simplify (%);

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

> s(t) - s(3);

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

> simplify(%);

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

Notice that if you define a function, there is no need to evaluate the function using the "subs" command like you do with expressions.

Solving Equations involving Functions

Once your function is defined, you can solve equations with functions either exactly or approximately:

> g:='g';

g := 'g'

> g:=t->t^3-6*t^2+6*t+8;

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

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

4, 1+sqrt(3), 1-sqrt(3)

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

-.7320508076, 2.732050808, 4.

Graphing a Function

The plot function works the same for functions:

> h:='h'; y:='y'; x:='x';

h := 'h'

y := 'y'

x := 'x'

> h:=x->x*exp(-x);

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

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

[Maple Plot]

Several functions can be graphed simultaneously just at we did for expressions.

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

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

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

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

[Maple Plot]

Exercise 5.3

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.3

>

>

>

>

>

>

>

>

>

Answer 5.3

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

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

> f(6.5);

-24.25

> simplify(f(z-4));

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

> plot(f(x),x);

[Maple Plot]

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

1.449489743

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

3.449489743

Exercise 5.4

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.4

>

>

>

>

>

>

>

>

>

>

Answer 5.4

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

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

> h:=x->x+1;

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

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

[Maple Plot]

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

[Maple Plot]

> 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);

2.437186898

> h(xval);

2.437186898

Exercise 5.5

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.5

>

>

>

>

>

>

>

>

Answer 5.5

a)

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

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

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

[Maple Plot]

b)

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

[Maple Plot]

There appears to be three intersection points at x=3.25 ,4.825 and 5.95 .

>

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);

3.265300793

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

4.857290511

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

5.933090071

 

Table of Contents

Section 6: More on Graphing

Maple Quick Reference Card

Notes on the Maple Worksheet Interface