Application Center - Maplesoft

# Maple Programming: 1.5: Working with expressions and Maple functions

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

1.05.mws

Programming in Maple

Roger Kraft
Department of Mathematics, Computer Science, and Statistics
Purdue University Calumet

roger@calumet.purdue.edu

1.5. Working with expressions and Maple functions (review)

There are many operations that you might want to perform on a function, for example, graph it, evaluate it, differentiate or integrate it, compose it with another function, etc. How you do these operations in Maple depends on how you choose to represent the function in Maple, as either a Maple expression or as a Maple function. In this section we review these ideas by looking at a number of examples.

First, an example of graphing a mathematical function defined as an Maple expression.

 > plot( x^2, x=-5..5 );

Now graph the same mathematical function defined as a Maple  function.

 > plot( x->x^2, -5..5 );

Notice the subtle difference in the syntax of these two commands. For example, both of the following Maple commands are incorrect.

 > plot( x^2, -5..5 );

`Plotting error, empty plot`

 > plot( x->x^2, x=-5..5 );

```Error, (in plot) invalid plotting of procedures, perhaps you mean plot(proc (x) options operator, arrow; x^2 end proc,-5 .. 5)

```

 >

Let us do an example where we give the functions names. Here is an expression named f  and a Maple function named g  that both represent the same mathematical function.

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

 > g := x -> x^2-3*x-10;

Now plot them.

 > plot( f, x=-3..6 );

 > plot( g, -3..6 );

Notice that both of the following commands are incorrect. You need to be very careful to distinguish between Maple expressions and Maple functions.

 > plot( f, -3..6 );

`Plotting error, empty plot`

 > plot( g, x=-3..6 );

```Error, (in plot) invalid plotting of procedures, perhaps you mean plot(g,-3 .. 6)

```

 >

Let us see why it makes sense that Maple would have a different syntax for plotting Maple expressions and Maple functions. Suppose we give Maple the following command.

 > plot( a*x^2, -5..5 );

`Plotting error, empty plot`

We have asked Maple to plot an expression with two variables but we have given Maple only one range. Which of the variables should Maple assign the range to? We have to tell Maple which of the variables the range is to be associated with (and the other variable has to be given a value so that it acts like a constant). So when we plot an expression, it makes sense to specify which variable the range is associated with (even if there might only be one variable). Now suppose we give Maple the following command.

 > plot( x->a*x^2, -5..5 );

`Plotting error, empty plot`

The function being graphed does not have two variables. The arrow notation specifies that the function has only one variable (and one "parameter"). Since the function only has one variable and there is one range given, it is clear that the range is supposed to be associated with the single variable. The command is not ambiguous (as long as we make sure that the "parameter" a  has a value). Now consider the following command.

 > plot3d( (x,a)->a*x^2, -5..5, -10..10 );

The arrow notation specifies that the function being graphed is a function of two variables and there are two ranges given in the command. The ranges are associated with the variables in the order they are given, so the -5..5  range is associated with the variable x  and the -10..10  range is associated with a . Again, there is no ambiguity about the variables, so there is no need when plotting a function to explicitly associate the range with a variable name as when you graph an expression.

Exercise : How would you graph the expression a*x^2  as a function of two variables?

 > plot3d(   );

```Error, (in plot3d) at least three arguments are required

```

 >

Here are some other differences in how Maple treats an expression and a Maple function. The following command tells us the definition of the expression f .

 > f;

But the following command does not tell us much about the function g .

 > g;

Here is a way to get the definition of g .

 > print( g );

Here are two more ways to get the definition of g .

 > eval( g );

 > op( g );

 >

Now suppose we wanted to evaluate our mathematical function at a point, say at 1. We use different syntax for the Maple expression and the Maple function. For the expression, we subs titute 1  for x  in f .

 > subs( x=1, f );

For the Maple function we can use traditional functional notation.

 > g(1);

Notice that the following two commands do not work.

 > f(1);

 > subs( x=1, g );

 >

We should mention that there is another way to evaluate an expression at a point. We can use a form of the eval  command (which is, of course, an abbreviation of eval uate).

 > eval( f, x=1 );

Notice the difference in the syntax between eval  and subs .

 > subs( x=1, f );

Think of reading eval(f,x=1)  as " eval uate f  at x=1 " and think of reading subs(x=1,f)  as " subs titute x=1  into f ". As you should expect, the following command does not work with the Maple function g .

 > eval( g, x=1 );

 >

The following command will factor the expression f .

 > factor( f );

But the following command does not factor the Maple function g .

 > factor( g );

Here is how we can factor it.

 > factor( g(x) );

 >

Here are commands for finding the derivative of an expression and a Maple function.

 > diff( f, x );

 > D( g );

(Explain why the diff  command needed a reference to x  in it but the D  command did not. ) As you might expect by now, the following two commands do not do what we want them to do.

 > D( f );

 > diff( g, x );

Why is the following command's output like that of D(f) ?

 > D( g(x) );

Notice how Maple did not complain about any of these last three commands. They were not syntactically incorrect. As far as Maple is concerned, we asked it to do something valid, and it did it. What we asked it to do is not clear at this point, but what ever it was, Maple did it. But what Maple did was not what we were expecting. There are two lessons to be learned from this. First, be careful to keep track of when you are working with expressions and when you are working with Maple functions. Second, you need to always look carefully at your Maple outputs. Just because Maple computed something does not mean that it computed something that made sense or that it computed what you wanted it to compute.

 >

Let us do an example of combining two mathematical functions f and g by composing them to make a new function .  Here is how we would compose two mathematical functions if they are represented by expressions.

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

 > g := x + 1;

 > h := subs( x=g, f );

We substitute the inner function g  into the outer function f  and we get the expression h  that represents the composition . Now here is how we would do this if f and g are represented by Maple functions.

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

 > g := x -> x + 1;

 > h := f@g;

Here is how we can verify that the Maple function h  represents the composition of f  and g .

 > h(x);

We do not need to use a separate name like h  for the composition of the Maple functions f and g. The symbol f@g  can be used as a name for the composition, but we need to put parentheses around this name when we use it to evaluate the composition.

 > (f@g)(x);

The at sign ( @ ) is used in Maple to mean composition of two Maple functions. The at sign is used because it is the closest character on the standard computer keyboard to the little raised circle used in mathematics books to denote composition of functions. (If you do not remember the symbol, look up composition in almost any calculus book.)

Exercise : In what way are the following four expressions similar to (f@g)(x) ?

 > (f+g)(x);

 > (f-g)(x);

 > (f*g)(x);

 > (f/g)(x);

 >

Exercise : Explain the results of the following two commands. (Try executing these two commands with f  and g  as unassigned variables.)

 > ((f+g)@g-f)(x);

 > ((f+g)@(g-f))(x);

 >

Let us do an example of representing a mathematical function of two variables. Here is an expression in two variables.

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

And here is the equivalent Maple function of two variables.

 > g := (x,y) -> (x^2+y^2)/(x+x*y);

Here is how we evaluate the expression and the Maple function at a point.

 > subs( x=1, y=2, f );

 > eval( f, {x=1, y=2} );

 > g(1,2);

Neither of the next two commands works.

 > f(1,2);

 > subs( x=1, y=2, g );

Here is how we get the definitions of f  and g .

 > f;

 > print( g );

Notice that Maple can simplify the expression a bit.

 > f;  simplify( f );

But the simplify  command does nothing to g .

 > simplify( g );

Here is how we can do the simplification using g .

 > simplify( g(x,y) );

Here is how we compute partial derivatives of the expression.

 > diff( f, x );

 > simplify( % );

 > diff( f, y );

 > simplify( % );

Here is how we compute the partial derivatives of the Maple function.

 > D[1](g);

 > simplify( %(x,y) );

(What did the %  refer to in the last command?)

 > D[2](g);

 > simplify( %(x,y) );

Notice how the notation for partial derivatives of expressions uses the name of the independent variable but with Maple functions the partial derivative notation does not  use the name of the independent variable. The D  operator uses a number to indicate the first, second, third, etc, independent variable. This is an indication of the fact that, for example, the expression 3*x^2+5*y^2  is not the same expression as   3*u^2+5*v^2 , but the Maple function (x,y)->3*x^2+5*y^2  is exactly  the same function as (u,v)->3*u^2+5*v^2 . (Consider (s,t)->3*t^2+5*s^2 . Is it the same function?) So the D  operator differentiates with respect to the position of the independent variable in the input list on the left hand side of the arrow operator, not with respect to the name given to the independent variable. For Maple functions, it is the position of the independent variable in this list that matters, not the name of the independent variable.

Suppose we want to compose our mathematical function with the function  where h will be the outer function in the composition. Let h1  represent h as an expression and let h2  represent h as a Maple function. Let k1  be the name of the composition as an expression and let k2  be the name of the composition as a Maple function.

 > h1 := sqrt(z);

 > h2 := z -> sqrt(z);

Here is the composition using expressions.

 > k1 := subs( z=f, h1 );

Here is the composition using Maple functions.

 > k2 := h2@g;

 > k2(x,y);

Explain why k1  is the same thing as k2(x,y) .

 >

In a previous section we defined a function h  as follows.

 > h := 2*sin - 9/exp;

h  is a function, not an expression. The next two commands show that h  really is a function, not an expression, since h  must be plotted using the syntax for a function, not an expression.

 > plot( h, 0..10 );      # The function syntax works.

 > plot( h, x = 0..10 );  # The expression syntax does not work.

`Plotting error, empty plot`

Here is another way to define the same function h .

 > h := x -> 2*sin(x) - 9/exp(x);

 >

Exercise : Let  and let . We have already seen how to compute the composition h(f( )) using either expressions or Maple functions. Explain why the composition f(h( )) does not make sense mathematically. Compute the compositions f(h( ), ), f( ,h( )), and f(h( ),h( )). First do this exercise using expressions. Can you do these compositions using Maple functions and the @  operator?

 >

Exercise : Let f  be the following function of two variables.

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

If we hold one of the inputs to f  fixed, then we get a function of one variable that we will call a "slice of f ". Let fx3  be the slice of f  defined by letting y  be fixed at 3. Find a Maple command that uses f  to define fx3  as a Maple function.

 >

Note: The following Maple command represents the function we want as an expression, so it is not the correct answer to this exercise.

 > fx3 := f(x,3);

Let f3y  denote the slice of f  with x  fixed at 3. Use f  to define f3y  as a Maple function.

 >

Explain the mathematical meaning of diff(fx3(x),x)  and diff(f3y(y),y) . (The following two commands do not mean anything until you have properly defined fx3  and f3y .)

 > diff( fx3(x), x );

 > diff( f3y(y), y );

Find another way to compute diff(fx3(x),x)  and diff(f3y(y),y)  that does not use the slice functions fx3  and fy3 .

 >

Almost anything you would like to do with an expression you can do with a Maple function, and visa versa. Unfortunately, as we have seen above, the syntax for doing the same thing with an expression and a Maple function can be quite different. Is one of the two methods "better" than the other? Most people do most of their Maple work using expressions to represent mathematical functions. Overall, expressions seem to be a bit easier to work with. But Maple functions are indispensable at times, so you must get used to working with both concepts. In addition, working with Maple functions is very much like working with Maple procedures (since, as we will see later, Maple functions are  procedures) so getting used to Maple functions is a good prerequisite for Maple programming.

 >

 >

 >