CS 121 Engineering Computation Lab

Download Report

Transcript CS 121 Engineering Computation Lab

CS 121 Engineering Computation Lab
Lab 2
Bruce Char
Department of Computer Science
Drexel University
Summer 2009
©By the author. All rights reserved. Permission is given to CS121 Fall 2008
staff and students to use and reproduce these notes for their own use.
Your class instructor and TA
• Instructor for this section:
• Your TAs are:
Note that verification sheet requires you to put
down instructor’s name.
Week 1 retrospective
• Lab 1: first week
• Quiz 1: first week
• If you are a latecomer and are catching up?
Talk to instructor!
Lab 2
• Overview
• Part 1 Limited precision numbers(ch. 3
readings)
• Part 2 Data structures, functions (ch. 4
readings)
Limited precision arithmetic
• Limited precision arithmetic: use 10 digits
(or some other fixed number such as 20).
• Perform an operation using as many
decimal digits as necessary.
• Round the result to 10 decimal digits.
• Do the next limited precision arithmetic
operation.
• Repeat until all the operations are done.
Adding 111.11 and 7.7777 in 5 digit
limited precision arithmetic
• 111.11 + 7.7777:
111.11
+ 7.7777
________
118.887
118.89
(final result)
exact answer
rounded to five digits
Five digit limited precision in Maple
Ten digit limited precision arithmetic is
Maple’s default (no evalf necessary)
Exact arithmetic is not the same as
limited precision arithmetic
• Exact arithmetic always works things out
to exactly.
• Operations between integers stay as exact
integers.
• Operations with fractions stay as exact
fractions.
Two kinds of arithmetic
• Maple does exact arithmetic with exact numbers
• Maple does limited precision arithmetic with
limited precision numbers.
• How does Maple know what to do with a
number? By the way it looks.
– Numbers with decimal points, :”e’ exponents are
limited precision numbers
– Numbers that are integers or ratios of integers without
decimal points are exact numbers.
– Symbolic constants are also considered exact
numbers.
Floating Point Numbers
• Having a decimal point written with them
• Considered as limited-precision numbers
in Maple
• Answers are rounded to the number of
digits of accuracy
• e.g.
– 1111.1, meaning the limited-precision version
with 5 digit numbers
“e” notation
• Scientific notation using “e” to indicate the
exponent
• Considered as limited-precision numbers
in Maple
• Do not use “e” for logarithms from the
palette, but type the letter e on the
keyboard normally
• e.g.:
– 7e11, meaning 7*1011
– 2e-8, meaning 2*10-8
How do you convert an exact
number into an approximate
number?
Clickable Approximate->20
evalf( expr) or evalf(expr,digits)
Bonus (not mentioned in chapter
readings): how do you convert an
approximate number into an exact
number?
Maple as a math word processor
(sect 4.1, 4.4.5, 4.4.6)
• Ordinary entry is 2D Math mode
• Can enter text. Once in text mode, can do
boldface, italics, center-justification as with a
word processor.
• Control-T/Control-R (Command-T/Command-R)
to switch between the two.
• When in doubt click cursor on position, then do
Format -> Create Document Block. Switch to
proper mode and start typing.
Demo of Math text entry
• Demo of word processing.
• Demo of Greek letters (section 4.4.6),
math constants, subscripts in math mode.
Assignment, labels (sect 4.2)
• Hitting enter (return) gives a result a
numerical label.
• Control-L (command-L) to refer to a result
by its label.
• name := expression
– Is similar – turns that into a label for the result
of evaluating the expression.
• Both are useful when you want to refer to
results in later steps of a calculation.
Demo of assignments and labels
• Replace with demo of assignment and
labeling at work.
Functions (Sect 4.4)
• Function name ( input )
produces an output.
• Most math functions take one numerical input
and produce one numerical output.
• Some math functions take two numerical inputs
and produce one numerical output.
• Maple functions do this and more: sometimes
the inputs and outputs are not numbers.
Function demo
• Demo textual entry of:
– Sqrt, exp, abs, sin, cos, min, max
Maple has “non math” functions
(sect 4.4.7)
• function name( input1, input2, … )
with output.
• Inputs and outputs do not have to be
numbers.
Demo of non-Math functions
• Demo of solve, factor,eval, plot
Finishing up – save files
• Make sure your name/user id/section number/
date,time/instructor name are on the verification
sheet.
• Get the verification sheet signed and handed in.
• Save worksheet on desktop if you haven’t done
so already. You can call the file “MyLab2”. This
will create a file called MyLab1.mw. Opens on
any machine running Maple 12.
• Submit a copy to Blackboard site as evidence
that you did the lab. This is not required but can
help you later.
• Email a copy to yourself and/or your lab partners
as an attachment so you can look at what you
did for review purposes later.
Weeks 2
• Did you enter in week 2?
– Talk to the instructor during their office hours
about catching up with missed material.
• Take Quiz 2 Tuesday-Friday Week 2
• Look for Chapter 5,6 released at end of
Week 2