# Math Help - Maple Programming Tutorial

1. ## Maple Programming Tutorial

Hello! I decided to start on a new tutorial (my other one [which is still in the process of being completed] may be found here.)

This tutorial will be more of a programming one.

1. Basic Programming

Programming in Maple is a bit different than other computer languages (like C and the like). This is a programming language suited for mathematicians; as a result, there are some great commands that aren't available in other programming languages.

When constructing a program, there are several things that must be done:

1. You need to declare the name of the program, and its inputs. For example, $\texttt{FourierSeries := proc(f,x,d,n)}$ declares the name of the program $\texttt{FourierSeries}$ with inputs $\texttt{f}$ (function), $\texttt{x}$ (variable/value at which series is approximated), $\texttt{d}$ defines interval $(-d,d)$ and $\texttt{n}$ (number of terms in series).
2. You need to declare any local variables. (I will explain as we create a program).
3. Now you can write the program.
4. Return Values
5. Always end program with $\texttt{end proc;}$

When you program, also keep in mind that each command line ends with a semicolon [;]. However, you don't end the declaration line with a semicolon, or the declaration of an $\texttt{if}$-block or a $\texttt{for-do}$/ $\texttt{while-do}$.

Let's start off with an example problem and see how we would program this:

Example 1: Write a program that outputs the radius, volume, and surface area of a sphere when the diameter is given.

When writing programs, I would strongly recommend writing them in notepad.

I will call this program $\texttt{SphereInfo}$. The only input is the diameter.

Thus, the declaration line is $\texttt{SphereInfo := proc(diam)}$

Now, we need to define local variables. These will be variables that are used within the program. Since we are to compute the radius, volume, and surface area, it would be best to declare them as local:

$\texttt{local rad, surfarea, vol;}$

Now that we have our local variables, let us use [define] them in constructing our program.

We know that radius is half the diameter, so $\texttt{rad := diam/2;}$

We know that surface area is $4\pi r^2$. Thus, $\texttt{surfarea}\texttt{ := 4 }$* $\texttt{ Pi }$* $\texttt{rad}$^ $\texttt{ 2 ;}$

We know that volume is $\tfrac{4}{3}\pi r^3$. Thus, $\texttt{vol := 4/3 }$* $\texttt{ Pi}$* $\texttt{rad }$^ $\texttt{ 3 ;}$

Now, return the values of radius, surface area, and volume:

$\texttt{return rad, surfarea, vol;}$

Now end the program:

$\texttt{end proc;}$

In notepad, this is what the program should look like:

Code:
SphereInfo := proc(diam)

end proc;
Wasn't that easy? [I hope it was ] Save this to a flash drive if you have one [or a temporary folder]

Now that you have created it, the last step is to run it in maple. I would suggest running the Classic Worksheet.

At the command prompt, call up the text file containing the program:

$\texttt{> read("e:/SphereInfo.txt"):}$

Once it passes reading, run the program:

$\texttt{> SphereInfo(5);}$
$\texttt{>}\hspace{15pc}\color{blue}\frac{5}{2},~25 \pi,~\frac{125\pi}{6}$

Note how we got symbolic answers. What if we want decimal approximations? apply $\texttt{evalf()}$ [which is a floating point evaluation]:

$\texttt{> evalf(SphereInfo(5));}$
$\texttt{>}\hspace{10pc}\color{blue}2.500000000,~78 .53981635,~65.44984695$

Note that we now have an approximation of the solution.

-------------------------------------------------------------------------

Now that you are familiar with the basics of programming, try these on your own:

Problem 1
When in parallel, the total resistance of a circuit is defined as $\frac{1}{R}=\frac{1}{R_1}+\frac{1}{R_2}+\frac{1}{R _3}$.
Write a program that computes $R$ given the input of $R_1$, $R_2$ and $R_3$

-------------------------------------------------------------------------

This will conclude the first bit for now. I need to get some sleep.

I will continue on with more examples and techniques tomorrow.

--Chris