Results 1 to 1 of 1

Math Help - Maple Programming Tutorial

  1. #1
    Rhymes with Orange Chris L T521's Avatar
    Joined
    May 2008
    From
    Chicago, IL
    Posts
    2,844
    Thanks
    3

    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)
       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:

    \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
    Last edited by Chris L T521; November 5th 2008 at 10:48 PM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

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

Search Tags


/mathhelpforum @mathhelpforum