Application Center - Maplesoft

App Preview:

Maple Programming: 1.4: Expressions vs. functions

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

Learn about Maple
Download Application


 

1.04.mws

Programming in Maple

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

roger@calumet.purdue.edu

1.4. Expressions vs. functions: Some puzzles

The following examples are meant to show that there are still a lot of subtle things to learn about variables and functions and how Maple handles them. Do not expect to fully understand these examples now. You should return to these examples after you have gone through the chapter on Maple's evaluation rules and the chapter on procedures in Maple.

>   

Puzzle 1

Here is a subtle example of a difference between an expression and a function. First we define a couple of expressions. The first is an expression in x  and the second is an expression in y .

>    f1 := x^2+1;

>    f2 := y^2+1;

f1 := x^2+1

f2 := y^2+1

Now we add these two expressions and get an expression in the two variables x  and y .

>    f3 := f1 + f2;

f3 := x^2+2+y^2

 Now let us define a couple of Maple functions equivalent to the above expressions. Each of the next two functions is a function of one variable.

>    g1 := x -> x^2+1;

>    g2 := y -> y^2+1;

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

g2 := proc (y) options operator, arrow; y^2+1 end proc

Now add these two Maple functions. What do we get?

>    g3 := g1 + g2;   # What kind of function is g3?

g3 := g1+g2

Is   g3  a function of two variables like f3   is an expression in two variables? Or is it a function of one variable? The following command shows the formula for g3 .

>    g3(x);

2*x^2+2

So in fact, g3  is not a function of two variables like f3  is an expression in two unknowns; g3  is a function of one variable. This shows that Maple functions and Maple expressions handle unassigned  variables in different ways.  Let us look at g3  again. Here is another way to see the formula for g3 .

>    g3(y);

2*y^2+2

If we try to treat g3  as a function of two variables, g3  just ignores the second variable.

>    g3(w,z);

2*w^2+2

Why is it that the sum of f1  and f2  has two variables in it but the sum of g1  and g2  does not?

>   

Puzzle 2

There are two ways that a Maple expression can be converted into a Maple function but these two ways are not equivalent. Here is an example.  First, make sure that x  is unassigned and give a , b , and c  values.

>    x:='x': a:=1: b:=2: c:=3:

Here is an expression.

>    a*x^2+b*x+c;

x^2+2*x+3

Here is one way to convert this expression into a Maple function.

>    f := unapply( a*x^2+b*x+c, x );

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

Here is another way. Just use the expression on the right hand side of the arrow operator.

>    g := x -> a*x^2+b*x+c;

g := proc (x) options operator, arrow; a*x^2+b*x+c end proc

Notice that f  does not have the constants a , b , and c  in its definition, but g  does! Let us try evaluating both functions.

>    f(x); g(x);

x^2+2*x+3

x^2+2*x+3

Now they both have the values for a , b , and c  in them. Let us try to differentiate each of these functions.

>    D(f); D(g);

proc (x) options operator, arrow; 2*x+2 end proc

proc (x) options operator, arrow; 2*a*x+b end proc

Notice that the derivative of g  has the unevaluated constants in it but the derivative of f  has the constants evaluated.

Let us try changing one of the "constants". Change the value of c .

>    c := 15;

c := 15

Now evaluate the functions again.

>    f(x); g(x);

x^2+2*x+3

x^2+2*x+15

 Notice that the definition of f  did not change, but the definition of g  did, when we changed c .

>   

Puzzle 3

First, let us give the variable x  a value.

>    x := 5;

x := 5

Now try to plot the expression x^2 .

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

Error, (in plot) invalid arguments

Maple returned an error (why?). Now try to plot the function x->x^2 .

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

[Maple Plot]

There is no problem with this, even though the variable x  still has a value.

>    x;

5

This shows once again that there are subtle differences in the way that Maple treats variables used in expressions and variables used in functions.

>   

Puzzle 4

>    restart;

Let f  be the name for an expression.

>    f := x^2;

f := x^2

Now use f  to redefine f .

>    f := x*f;

f := x^3

>    f;

x^3

Let us try to do something similar with functions. Let g  be the name for a function equivalent to f .

>    g := x -> x^2;

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

Now use g  to redefine g .

>    g := (x->x)*g;

Error, recursive assignment

>    g(x);

x^2

Let us try a slightly different way.

>    g := 'g';

>    g := x -> x^2;

g := 'g'

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

Now use g  to redefine g .

>    g := x -> x*g(x);

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

>    g(x);

Error, (in g) too many levels of recursion

There was no problem when we used f  to redefine f , but we cannot use g  to redefine g . This shows that there are subtle differences in the way that Maple treats the names of expressions and the names of functions.

>   

Puzzle 5

Here is an anonymous expression.

>    3-x^2;

3-x^2

Now let us give it a name.

>    f := %;

f := 3-x^2

Now graph the expression by referring to its name.

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

[Maple Plot]

Now let us try to do something similar with a Maple function. Here is the anonymous expression again.

>    3-x^2;

3-x^2

Use the anonymous expression to define a Maple function.

>    g := x -> %;

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

But now the following graph is empty.

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

[Maple Plot]

In this sequence of commands we used the last result variable, % , twice. But the last result variable in the definition of f  has a different meaning from the last result variable in the definition of g , which is why the definition of g  does not work the way we might (reasonably) expect it to. Once again this shows that we need to understand the details of exactly how Maple interprets different kinds of variables in different kinds of situations.

>   

>   

>