32
MAT 4830 Mathematical Modeling Section 1.2 Simple Programming http://myhome.spu.edu/lauw

MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Embed Size (px)

Citation preview

Page 1: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

MAT 4830Mathematical Modeling

Section 1.2

Simple Programming

http://myhome.spu.edu/lauw

Page 2: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Maple Standards

Always work in the worksheet mode.

Page 3: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Maple Standards

Always work in the worksheet mode.

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

Page 4: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

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

Page 5: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

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.

Page 6: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Questions

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

Page 7: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Questions

2. What is the function of “#” ?

Page 8: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Questions

3. Name the context of one example.

Page 9: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Today…

Zeng 1.2 It may be really easy and fast for some

of you. Be patient.

Page 10: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Zeng 1.2

Introduces the basic elements of Maple programming

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

Page 11: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Maple

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

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

Page 12: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

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.

Page 13: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Example 0

Find the volume of a (circular) cylinder

volume=(base area)(height)

base area=(radius2)

Page 14: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Computational Task

3 Components

1. Data

2. Computational Method

3. Results

Page 15: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Example 0

2

Data ,

Method base area

volume

Result volume

r h

s r

v sh

v

Page 16: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Program

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

Page 17: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

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;

Page 18: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

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;

Page 19: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

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;

Page 20: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Good Practices

Be sure to use• Indentations

• Empty lines

• A lot of meaningful comments

Page 21: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

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

Page 22: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

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.

Page 23: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

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

Page 24: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Example 1

Page 25: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Example 1

Page 26: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Example 1

Page 27: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

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

Page 28: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

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

Page 29: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Example 2

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

Page 30: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Example 2

Page 31: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

What is not working in Zeng…

Cannot use underscore as shown in Zeng e.g.

cylinder_volume:=proc(radius,height)

Page 32: MAT 4830 Mathematical Modeling Section 1.2 Simple Programming

Homework

Download from the web (individual HW**)

Read Zeng Section 1.3