Results 1 to 1 of 1

Thread: Maple Programming Tutorial

  1. #1
    Rhymes with Orange Chris L T521's Avatar
    May 2008
    Chicago, IL

    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, $\displaystyle \texttt{FourierSeries := proc(f,x,d,n)}$ declares the name of the program $\displaystyle \texttt{FourierSeries}$ with inputs $\displaystyle \texttt{f}$ (function), $\displaystyle \texttt{x}$ (variable/value at which series is approximated), $\displaystyle \texttt{d}$ defines interval $\displaystyle (-d,d)$ and $\displaystyle \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 $\displaystyle \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 $\displaystyle \texttt{if}$-block or a $\displaystyle \texttt{for-do}$/$\displaystyle \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 $\displaystyle \texttt{SphereInfo}$. The only input is the diameter.

    Thus, the declaration line is $\displaystyle \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:

    $\displaystyle \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 $\displaystyle \texttt{rad := diam/2;}$

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

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

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

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

    Now end the program:

    $\displaystyle \texttt{end proc;}$

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

    SphereInfo := proc(diam)
       local rad, surfarea, vol;
       rad := diam/2;
       surfarea := 4*Pi*rad^2;
       vol := 4/3*Pi*rad^3;
       return rad, surfarea, vol;
    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:

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

    Once it passes reading, run the program:

    $\displaystyle \texttt{> SphereInfo(5);}$
    $\displaystyle \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 $\displaystyle \texttt{evalf()}$ [which is a floating point evaluation]:

    $\displaystyle \texttt{> evalf(SphereInfo(5));}$
    $\displaystyle \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 $\displaystyle \frac{1}{R}=\frac{1}{R_1}+\frac{1}{R_2}+\frac{1}{R _3}$.
    Write a program that computes $\displaystyle R$ given the input of $\displaystyle R_1$, $\displaystyle R_2$ and $\displaystyle 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.

    Last edited by Chris L T521; Nov 5th 2008 at 10:48 PM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 0
    Last Post: Nov 30th 2011, 02:25 PM
  2. Maple Programming Problem
    Posted in the Math Software Forum
    Replies: 1
    Last Post: Sep 16th 2011, 04:30 PM
  3. Maple programming advice
    Posted in the Math Software Forum
    Replies: 0
    Last Post: Nov 2nd 2009, 12:39 AM
  4. Replies: 1
    Last Post: Nov 17th 2008, 03:18 AM
  5. [SOLVED] Maple Programming (1)
    Posted in the Math Software Forum
    Replies: 3
    Last Post: Aug 26th 2008, 12:27 PM

Search Tags

/mathhelpforum @mathhelpforum