Calculus B - Lab 3 

TA: Patrick C. Rowe 

> restart;
 

Procedures 

 

This lab will outline how to write a Maple procedure. Procedures are a way to write code in Maple so that repetitive commands can be called with one command.  

 

Although we will not have the opportunity to plumb the full depths of Maple, it is in fact more than just a nice calculator and grapher. Maple is a powerful programming language. Part of this power is its ability to be customized through the use of procedures and packages. Just as there are pre-programmed Maple packages, it is possible for the user to create their own packages to store commands in. These can also be loaded using the with command. 

 

We're only going to cover how to write a basic procedure here, but the above can be researched in the help menu. 

 

Simplest Procedure: 

> namehere:=proc(x) local y;
y:=x^2;
return y;
end proc;
 

> namehere(4);
 

namehere in the above is the name you give to the procedure. It is the command you will use to call the procedure. 

 

After namehere comes the assignment operator. Then proc() is typed. This indicates that a procedure is to be assigned to namehere. 

 

x in the parentheses is a parameter. More than one can be entered. These are the inputs the user passes to the procedure. They can be anything: numbers, expressions, functions, etc. 

 

local indicates a declaration section. Here you tell Maple the name of variables which will be used in your procedure. These variables are only used in the procedure and cannot be accessed from outside. 

 

The ; indicates the end of the line.

At the end of lines in a procedure, do not hit
enter. You must type shift+enter. This keeps the lines within the procedure. 

 

The next line carries out the operation.

The
return command sends the result back to the place where it was called from.

The final line is necessary in every procedure. It tells Maple that the procedure is over.
 

>
 

if statements 

 

These are used to make a comparison and then, based on that comparison, either execute or don't execute a command.
It can be used in conjunction with the
else command. 

 

> namehere:=proc(x) local y;
y:=x^2;
if y>1 then return "Too big!";
else return y;
end if;
end proc;
 

> namehere(2);
 

> namehere(.5);
 

while statements 

 

These are used to set up a loop. The loop continues as long as the specified condition is true. Notice the word do which specifies where the loop begins and must be used to end the loop. 

> namehere:=proc(x) local y;
if abs(x)>1 then y:=x^2;
else y:=1/(x^2);
end if;
while y<=100 do
y:=y^2;
end do;
return y;
end proc;
 

> namehere(3);
 

infinite loops 

 

What happens in the above procedure if we put x=1? By following the calculation through, we see that we will never meet the criteria to break end the loop. y will be set equal to 1 and 1 squared is always 1 again. In Maple, when this happens, you can click the red stop sign with a hand on it in the toolbar. 

 

Try entering 1. 

> namehere(1);
 

 

When constructing loops, you have to be careful and check to see if the loop may be infinite under some circumstances. If circumstances could allow for it, a good way to work around it is to put another stopping criteria in to the loop. Below, I've taken namehere and put a 1000 iteration stopping criteria into the loop. Notice that the if statement afterwards prints a message to the screen if the maximum iterations were reached. 

> namehere:=proc(x) local y, count;
if abs(x)>1 then y:=x^2;
else y:=1/(x^2);
end if;
count:=1; # count is initialized here
while y<=100 and count<1000 do
y:=y^2;
count:=count+1;
end do;
if (count>=1000) then printf("Maximum iterations exceeded.");
fi;
return y;
end proc;
 

> namehere(2);
 

> namehere(1);
 

 

Be careful though, if the stopping criteria is not set high enough however, you may stop the calculation before reaching the desired result. 

> namehere(1.00000000000000001);
 

>
 

>