cylindervolume(r,h)

MAT 4830
Mathematical 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

You can set the Text mode as default
(ask Brittany)
> 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 exact
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…if
you do not use the text mode.
Maple


I will explain how things work in terms of
memory allocation and memory
contents.
Understand how computer works help
you a long way in programming.
Act Like a Lady, Think Like a
Man?
Act like a Man, Think Like a
Computer
Maple



I will explain how things work in terms of
memory allocation and memory
contents.
Understand how computer works help
you a long way in programming.
Do not type into Maple until I tell you to
do so. You will have plenty of time to
type in your program.
Example 0
Find the volume of a (circular) cylinder
volume=(base area)(height)
base area=(radius2)
Computational Task

1.
2.
3.
3 Components
Data
Computational Method
Results
Example 0
Data
r, h
Method base area s   r
volume v  sh
Result
volume v
2
Program

A program is an implementation of the
computational method for the task.
Example 0
cylindervolume:=proc(radius,height) # program definition
local basearea, volume;
# local variables
basearea:=Pi*radius^2;
# compute the base area
volume:=evalf(basearea*height); # compute the volume
print(`The volume is`);
print(volume);
end;
> r:=5.5: h:=8.2:
> cylindervolume(r,h);
The volume is
779.2720578
# output result
Program Declaration
cylindervolume:=proc(radius,height) # program definition
local basearea, volume;
# local variables
basearea:=Pi*radius^2;
# compute the base area
volume:=evalf(basearea*height); # compute the volume
print(`The volume is`);
print(volume);
end;
> r:=5.5: h:=8.2:
> cylindervolume(r,h);
The volume is
779.2720578
# output result
Memory Allocation and Values
cylindervolume:=proc(radius,height) # program definition
local basearea, volume;
# local variables
basearea:=Pi*radius^2;
# compute the base area
volume:=evalf(basearea*height); # compute the volume
print(`The volume is`);
print(volume);
end;
> r:=5.5: h:=8.2:
> cylindervolume(r,h);
The volume is
779.2720578
# output result
5.5
8.2
r
h
radius
height
Memory Allocation and Values
cylindervolume:=proc(radius,height) # program definition
local basearea, volume;
# local variables
basearea:=Pi*radius^2;
# compute the base area
volume:=evalf(basearea*height); # compute the volume
print(`The volume is`);
print(volume);
end;
> r:=5.5: h:=8.2:
> cylindervolume(r,h);
The volume is
779.2720578
# output result
5.5
8.2
5.5
8.2
r
h
radius
height
Memory Allocation and Values
cylindervolume:=proc(radius,height) # program definition
local basearea, volume;
# local variables
basearea:=Pi*radius^2;
# compute the base area
volume:=evalf(basearea*height); # compute the volume
print(`The volume is`);
print(volume);
end;
> r:=5.5: h:=8.2:
> cylindervolume(r,h);
The volume is
779.2720578
# output result
5.5
8.2
radius
height
-
-
basearea
volume
Memory Allocation and Values
cylindervolume:=proc(radius,height) # program definition
local basearea, volume;
# local variables
basearea:=Pi*radius^2;
# compute the base area
volume:=evalf(basearea*height); # compute the volume
print(`The volume is`);
print(volume);
end;
> r:=5.5: h:=8.2:
> cylindervolume(r,h);
The volume is
779.2720578
# output result
5.5
8.2
radius
height
30.25π
779.27…
basearea
volume
Output
cylindervolume:=proc(radius,height) # program definition
local basearea, volume;
# local variables
basearea:=Pi*radius^2;
# compute the base area
volume:=evalf(basearea*height); # compute the volume
print(`The volume is`);
print(volume);
end;
> r:=5.5: h:=8.2:
> cylindervolume(r,h);
The volume is
779.2720578
# output result
5.5
8.2
radius
height
30.25π
779.27…
basearea
volume
Termination
cylindervolume:=proc(radius,height) # program definition
local basearea, volume;
# local variables
basearea:=Pi*radius^2;
# compute the base area
volume:=evalf(basearea*height); # compute the volume
print(`The volume is`);
print(volume);
end;
With “echo”
> r:=5.5: h:=8.2:
> cylindervolume(r,h);
The volume is
779.2720578
# output result
5.5
8.2
radius
height
30.25π
779.27…
basearea
volume
HW

Do not turn in the “echo” from Maple.
Type It in!
cylindervolume:=proc(radius,height) # program definition
local basearea, volume;
# local variables
basearea:=Pi*radius^2;
# compute the base area
volume:=evalf(basearea*height); # compute the volume
print(`The volume is`);
print(volume);
end;
> r:=5.5: h:=8.2:
> cylindervolume(r,h);
The volume is
779.2720578
# output result
Alternative – Less Desirable
cylindervolume:=proc(radius,height) # program definition
local basearea, volume;
# local variables
basearea:=Pi*radius^2;
# compute the base area
volume:=evalf(basearea*height); # compute the volume
print(`The volume is`);
print(volume);
end;
> cylindervolume(5.5,8.2);
The volume is
779.2720578
# output result
5.5
8.2
radius
height
Basic Structure
name:=proc(variables) #Comments
local local variables;
………………………….
print(….);
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 $𝑅 to an
account that pays interest rate 𝑖 per
period, for 𝑛 periods, then the future value
$𝑆 of the account after 𝑛 periods will be
S


R 1  i   1
n
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 𝐴, requires
monthly deposit $𝑅, for 𝑦 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 𝐴, requires
monthly deposit $𝑅, for 𝑦 years.

𝑅 = 300, 𝐴 = 0.12, 𝑦 = 30, 𝑆 =?
n
(𝑖 = 𝐴/12, 𝑛 = 𝑦 ∙ 12)
R 1  i   1
S


i
Example 1
Example 1
Example 2
A program that compute the two solutions
of the general quadratic equation
ax  bx  c  0
2
with the quadratic formula. Use the
program to solve
3x 2  5 x  2  0 and x 2  3x  2  0
Example 2
A program that compute the two solutions
of the general quadratic equation
ax  bx  c  0
2
with the quadratic formula. Use the
program to solve
3x 2  5 x  2  0 and x 2  3x  2  0
b  b2  4ac
x
2a
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**)
For those who took 4725 last year, you
need to redo the HW. Do not turn in
your HW from last year.
Read Zeng Section 1.4