MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Preview:

Citation preview

MAT 4830Mathematical Modeling

Section 1.2

Simple Programming

http://myhome.spu.edu/lauw

Maple Standards

Always work in the worksheet mode.

Maple Standards

Always work in the worksheet mode.

Always use the Text mode to type your program (press the Text button).

Maple Standards

Since your program is in text, it can be copy-and-paste to WORD as text. And your program is editable in WORD.

> cylinder_volume:=proc(radius,height) # program definition local base_area, volume; # local variables base_area:=Pi*radius^2; # implement method volume:=evalf(base_area*height);

Maple Standards

Make sure the font is not too small. Always print in color.

Use separate boxes for program and output.

Make sure you pay attention to the test cases and the exact format for the input data.

Questions

1. What are the 3 components of a computational task?

Questions

2. What is the function of “#” ?

Questions

3. Name the context of one example.

Today…

Zeng 1.2 It may be really easy and fast for some

of you. Be patient.

Zeng 1.2

Introduces the basic elements of Maple programming

Open your Maple now (because it takes so long…)

Maple

Use ONLY worksheet mode. In HW, you need to cut-and-paste your

program/results from Maple. Sometimes, strange things happen…

Maple

I will explain how things work. Understand how computer works help you a long way in programming.

Do not type into Maple until I tell you to do so.

Example 0

Find the volume of a (circular) cylinder

volume=(base area)(height)

base area=(radius2)

Computational Task

3 Components

1. Data

2. Computational Method

3. Results

Example 0

2

Data ,

Method base area

volume

Result volume

r h

s r

v sh

v

Program

A program is an implementation of the computational method for the task.

Example 0

cylindervolume:=proc(radius,height) # program definitionlocal basearea, volume; # local variables

basearea:=Pi*radius^2; # compute the base areavolume:=evalf(basearea*height); # compute the volume

print(`The volume is`); # output result print(volume);

end;

Example 0

> cylindervolume(5.5,8.2);The volume is779.2720578

cylindervolume:=proc(radius,height) # program definitionlocal basearea, volume; # local variables

basearea:=Pi*radius^2; # compute the base areavolume:=evalf(basearea*height); # compute the volume

print(`The volume is`); # output result print(volume);

end;

Example 0

> r:=5.5: h:=8.2:> cylindervolume(r,h);

The volume is779.2720578

cylindervolume:=proc(radius,height) # program definitionlocal basearea, volume; # local variables

basearea:=Pi*radius^2; # compute the base areavolume:=evalf(basearea*height); # compute the volume

print(`The volume is`); # output result print(volume);

end;

Good Practices

Be sure to use• Indentations

• Empty lines

• A lot of meaningful comments

Example 1

The future value of an annuity: Suppose you make a periodic deposit $R to an account that pays interest rate i per period, for n periods, then the future value $S of the account after n periods will be

1 1n

R iS

i

Example 1

Write a program to calculate the worth of a retirement plan at the end of the term if the plan pays an annual rate A, requires monthly deposit $R, for y years.

Then use the program to calculate the future value if the monthly deposit is $300, the annual interest rate is 12%, for 30 years.

Example 1

Write a program to calculate the worth of a retirement plan at the end of the term if the plan pays an annual rate A, requires monthly deposit $R, for y years.

R=300, A=0.12, y=30, S=? (i=A/12, n=y*12) 1 1

nR i

Si

Example 1

Example 1

Example 1

A program that compute the two solutions of the general quadratic equation

with the quadratic formula. Use the program to solve

Example 2

2 0ax bx c

2 23 5 2 0 and 3 2 0x x x x

A program that compute the two solutions of the general quadratic equation

with the quadratic formula. Use the program to solve

Example 2

2 0ax bx c

2 23 5 2 0 and 3 2 0x x x x 2 4

2

b b acx

a

Example 2

Note: Comments are left out to save time and space. In your HW, you need to put in a lot of comments.

Example 2

What is not working in Zeng…

Cannot use underscore as shown in Zeng e.g.

cylinder_volume:=proc(radius,height)

Homework

Download from the web (individual HW**)

Read Zeng Section 1.3

Recommended