Maple Introduction, Part 6:
Functions and decision-making
Integrated, First-Year Curriculum in Science, Engineering, and Mathematics
Based on a notebook by Claude Anderson
Extensively revised by Kurt Bryan and David Mutchler
Format of today's session
Questions and answers (15 minutes)
Strings in Maple (2 minutes)
Conditionals (if-then-else-fi) (15 minutes)
What is a conditional (overview)?
A simple example
Using conditionals in Maple functions
How to use conditionals in functions
What if the value of the test is neither true nor false (if, for example, there's not enough information to decide)? In the example below the variable "stuff" has not been assigned a value, so there's no way to decide whether stuff < 3:
> if stuff < 3 then 8 else 5 fi;
Error, cannot evaluate boolean
However, by using Maple functions, we can incorporate variables into conditionals, as we will now see.
You can define
Maple functions
that incorporate conditionals, but not
expressions.
For example, trying to assign a variable to a conditional gives an error:
> h := if stuff < 3 then 8 else 5 fi;
reserved word `if` unexpected
but the same thing works if inside a Maple function:
>
h := stuff -> if stuff < 3 then 8 else 5 fi;
>
h(1);
h(10);
>
An example of conditionals in Maple functions
Here is an example to show the power of these ideas.
We can use conditionals in the definition of the Maple function for the "two ships" problem from a previous homework assignment, as shown below:
Recall the problem:
Two ships sail from the same port. The first ship leaves at noon and travels eastward at 15 knots. The second ship leaves at 3:00 PM and travels northward at 40 knots. Find the distance (in nautical miles) between the ships as a function of time since noon. Plot the distance as a function of time after noon, i.e., represents the time 12:00 noon, represents 1:00 PM, etc.
New Solution using conditionals
:
>
ship1 := t -> 15 * t;
ship2 := t -> if t < 3 then 0 else 40 * (t - 3) fi;
distance := t -> sqrt( ship1(t)^2 + ship2(t)^2 );
We can plot the distance function:
> plot( distance, 0 .. 5 );
>
Again note the syntax: since distance is a function (not an expression), we must omit the x= in the range.
An exercise on conditionals in functions
Let's pretend for a moment that Maple has no built-in absolute-value function.
Write one. Call it
absolute.
Plot it to see if it looks right.
> absolute := x -> if x <0 then -x else x fi;
> absolute(12);
> absolute(-12);
>
>
Conditionals inside solve or fsolve (no, no, no!)
A more complicated example
It is rumored that in some courses at some colleges, the following grading scale is used:
percentage grade
------------- -------
90 - 100 A
80 - 89 B
70 - 79 C
60 - 69 D
0 - 59 F
Here is function that assigns a grade based on a student's average:
>
grade := average ->
if average >= 90 then "A"
else if average >= 80 then "B"
else if average >= 70 then "C"
else if average >= 60 then "D"
else "F"
fi
fi
fi
fi;
Note how I used line spacing and indentation to make the Maple function readable; you should do the same!
Now we test this Maple function. Notice that a Maple function can return something other than a number, in this case a letter.
> grade(90);
Here the Maple function is applied to several averages, arranged in a list.
> [ grade(92), grade(45), grade(77), grade(60), grade(82) ];
>
Using iteration to create functions with 2 parameters (8 minutes)
Let's look at some graphs (a good use for iteration). Here are some closely related Maple functions that we saw last week.
>
f2 := x -> Pi/2 - sin(x) - sin(2*x) / 2;
f3 := x -> Pi/2 - sin(x) - sin(2*x) / 2 - sin(3*x) / 3;
f6 := x -> Pi/2 - sin(x)
- sin(2*x) / 2
- sin(3*x) / 3
- sin(4*x) / 4
- sin(5*x) / 5
- sin(6*x) / 6;
Note that the above are Maple functions (they use the arrow notation).
Let's plot all three Maple functions in a single graph (colored red, blue and green, respectively):
> plot( [f2(x), f3(x), f6(x)], x = 0 .. 2*Pi, color = [red, blue, green] );
Can you see the trend? What do you think will happen as we add more and more terms? One thing is certain, we do not want to keep adding in extra terms by typing each one in individually! What if we wanted to plot
f30
? Or
f76
?
Exercise
(do it now): Use the
add
command to define a Maple function for
f45
:
> f45 := x -> Pi/2 - add(sin(n*x)/n,n=1..45);
>
Warning, `n` in call to `add` is not local
Don't worry about the warning message: we'll learn more about that soon enough.
Recall that we can evaluate
f45
at any particular value:
>
f45( 0.5 );
f45( 1.2 );
f45( Pi );
f45( x );
and we can plot
f45
:
> plot( f45(x), x = 0 .. 2*Pi );
But what if we want to look at the trend as we add more and more terms to the summation?
That is, what if we want to look at
f10
, then
f15
, then
f20
, and so forth?
It seems like we want to focus on two parameters: the number
of terms as well as the number
.
So let's define a Maple function of
two
parameters:
> f := (n, x) -> Pi/2 - add( sin(k*x) / k, k = 1 .. n );
Warning, `k` in call to `add` is not local
Don't worry about the warning message: we'll learn more about that soon enough.
Note the notation for the two parameters. Look what happens when we evaluate
f
at specific values:
>
f( 10, 0.5 );
f( 12, 0.7 );
f( 8, x );
The last of the above expressions shows how we can plot our two-parameter function for a fixed value of n :
> plot( f(8, x), x = 0 .. 2*Pi );
Note how easy it is for us to plot
for any
of interest:
> plot( f(30, x), x = 0 .. 2*Pi );
Whoa, this is cool! Isn't it interesting what happens to the sum of the sine functions?!
>
Review exercises (20 minutes)
Homework
Homework instructions and due dates
Who to work with on the homework
When is the homework due?
Reminder: print out only your homework solutions (collapse the preceding sections)
Homework problems
Complete the above worksheet, including the review exercises, before you begin the homework!
Don't forget to begin thinking about these problems well before they are due.
Problem 1
First Class postage rates are 32 cents for the first ounce (or part of an ounce), and 23 cents for each additional ounce (or part of an ounce). Here are the prices for some sample weights
weight (ounces) price (cents)
--------------- -------------
0.5 32
1.0 32
1.2 55
2.1 78
4.0 101
Define a Maple function
postage(weight)
that takes a weight (in ounces) as its input argument and returns the postage for that weight as its value. (
Hint:
check out the
ceil
and/or
floor
functions.) Then:
--- evaluate postage at 2.5 to find the price for a 2.5 ounce letter.
--- use a single
seq
command to evaluate postage at all intger weights from 1 to 16 ounces.
>
postage := w -> if w <= 1 then 32
else if w > 1 then 32+23*ceil(w-1)
fi
fi;
> postage(.5);
> postage(1);
> postage(1.2);
> postage(2.1);
> postage(4.0);
> seq(postage(w), w=1..16);
>
Problem 2
This is taken from the IRS 1993 1040 instructions, page 49.
Schedule X - Use if your filing status is Single:
If the amount on Form 1040, line 37 is
But not Enter on Form 1040,
Over -- over-- line 38 of the amount over
--------------------------------------------------------------------------------------
$0 $22,100 ............ 15% $0
22,100 53,500 $3,315.00 + 28% 22,100
53,500 115,000 12,107.00 + 31% 53,500
115,000 250,000 31,172.00 + 36% 115,000
250,000 ....... 79,772.00 + 39.6% 250,000
Write a Maple function,
taxAmount(taxableIncome)
, that takes the taxable income (from line 37) as its argument, and returns the amount of tax owed.
Once you have defined this function:
--- evaluate
taxAmount
at 53,600 (you better get 12,138 as your answer, if not, maybe you are misinterpreting the problem)
--- evaluate
taxAmount
at 200,000
--- use a single
seq
command to evaluate
taxAmount
for incomes from 0 to 300,000 in increments of 50,000
--- plot
taxAmount
from 0 to 60000.
> restart;
>
taxAmount := taxableIncome -> if taxableIncome <= 22100 then (taxableIncome-0)*.15
else if taxableIncome > 250000 then 79772+(taxableIncome-250000)*.396
else if taxableIncome > 115000 then 31172+(taxableIncome-115000)*.36
else if taxableIncome > 53500 then 12107+(taxableIncome-53500)*.31
else if taxableIncome > 22100 then 3315+(taxableIncome-22100)*.28
fi
fi
fi
fi
fi:
> taxAmount(53600);
>
> taxAmount(290105);
> seq(taxAmount(n*50000),n=0..6);
> seq1 := seq( taxAmount(a), a=0..60000):
> plot( seq1,x-0..10 );
Error, (in plot) invalid arguments
>
Problem 3
Consider the following collection of functions:
and so forth.
(a) Write a Maple function of three variables , and , that corresponds to the above collection of functions.
(b) Plot your function (over a range of your own choosing) when the subscript is 40 and the arguments and are equal.
> restart;
> fn := n -> add( (x^i*y^(i-1))/(2*i) , i=1..n);
Warning, `i` in call to `add` is not local
> fn(1);
> fn(3);
> fn40 := fn(40):
> x := y;
> plot( fn40,x=0..1.5,y=-2..10);
>
Problem 4 (counts as two problems)
Consider the following collection of functions:
and so forth.
(a) Define the Maple function of two variables n and x . Use your function to plot for several specific values of . Can you figure out from the graphs what function approximates as gets large? (Hint: you might need to try several different intervals of x -values in order to see this.)
(b) I'll refer to the function that approximates as . For some large value of , plot to support your conclusion that is the correct function. You may need to plot it over several different intervals of x -values in order to support your conclusion.
(c) Experiment with plots for different values of
to find
the smallest value of
that will guarantee
that the absolute value of
is less than 0.001 for all
in the range 0 <
< 5.
(Read that carefully!)
> restart;
> gn := n -> add( (-1)^i*x^(1+2*i)/(1+2*i)! , i=0..n);
Warning, `i` in call to `add` is not local
> gn5 := gn(5);
> [1!,3!,5!,7!,9!,11!];
> plot( {gn(5),gn(10),gn(20),gn(40),sin(x)}, x=-40..40, y=-8..8);
> sx := sin(x):
> plot( (sx-gn(100)),x=-40..40, y=-1..1);
> fsolve(sx-gn(n)=0.001,n,x=0..5);
Error, (in gn) unable to execute add
> gn(n);
Error, (in gn) unable to execute add
>