0% found this document useful (0 votes)
41 views

151lecture2 12 14

This document contains lecture notes from a chemical engineering class discussing ways to reduce round-off error in calculations, algorithms, stability of algorithms, rates of convergence, and root-finding methods like bisection. It provides pseudocode for the bisection method to find a root of a function by iteratively bisecting an interval and checking the sign of the function at each midpoint.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

151lecture2 12 14

This document contains lecture notes from a chemical engineering class discussing ways to reduce round-off error in calculations, algorithms, stability of algorithms, rates of convergence, and root-finding methods like bisection. It provides pseudocode for the bisection method to find a root of a function by iteratively bisecting an interval and checking the sign of the function at each midpoint.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

The Cooper Union for the Advancement of Science and Art

ChE 151 Lecture


2/12/14
Prof. Davis

Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Ways to Reduce Round-off Error


Nesting / minimizing calculations (also reduces
computer time, but can make code look messy)
Using relative values to avoid underflow
Generally avoid zero (Why?)
Test for nearness, not integer/binary equality
Dont multiply or subtract: big from big or small
from small
Dont divide or add: big to small or small to big
Increase precision, if possible (FORTRAN)
Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Algorithm = Recipe for Math


Inputs

Main routine
Constants

Global
variables

Subroutine 1

Operations
Subroutine 2
Subroutine 3

Outputs
Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Algorithms and Stability


An algorithm is a recipe for math a
procedure which describes, in an unambiguous
manner, a finite sequence of steps to be
performed in a specified manner
We would say an algorithm is stable if small
changes to the input give small changes to the
output. Otherwise it is unstable. Some are only
stable for certain inputs conditionally stable.
See Figure 1.10 on p. 26 for stable v. unstable
Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Rates of Convergence
If we have a sequence of numbers 1, 2, . . .
that converges to some number , we say it
converges with O(1/n) if:

K
1
n n1 is O n n large
n
n
We can say a similar thing for a function that
converges to L as h goes to zero:

f h is O h

f h L Kh

Chemical Engineering Department

as h 0

The Cooper Union for the Advancement of Science and Art

Activity: Rate of Convergence


Time: 5 min to do, 5 min discuss
Show that the Taylor polynomial we found for
sin(x):
3

x
x
x
sin x x sin x
3! 5! 6!
Converges with O(h) in the limit as h
approaches zero. Give EXPLICIT values for L
and K, not a range of values. Work in groups
of two and raise your hand if youre stuck.
Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Answer: Rate of Convergence


let

f h sin h , 1
3

h h h
h h h
sin h h sin h h
3! 5! 6!
3! 5! 6!
3
5
6
h h h
lim sin h lim h h
h 0
h 0
3! 5! 6!
h3 h5 h 6
lim sin h L lim h 0 h
h 0
h 0
3! 5! 6!

sin h is O h

...

K 1,

Chemical Engineering Department

L 0

The Cooper Union for the Advancement of Science and Art

Programming Languages
What are some programming languages you
have used?
What are examples of what they are used for?
What is the difference between languages?
What is a subroutine?
What is a special-purpose algorithm?
What is a general-purpose algorithm?
For more info: Sources and Development of
Mathematical Software by Cowell (see text)
Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Programming for This Class


I expect you to write your own special-purpose
algorithms, with minimal reliance on generalpurpose subroutines
You code need not be the most efficient, but it
should be straightforward, easy to read, and
easy to understand because thats how youll
learn the best (it should be a little efficient)
There is no benefit to writing the best/fastest
code for this class if I cant read or understand
what youre doing
Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Ch. 2: Soln. of Eqns. in 1 Variable


One of the oldest problems in mathematics
back to 1700 B.C. (irrational numbers)
Very common in ChE we usually have VERY
non-linear equations
Often SNLE can be reduced
to root-finding problems
We will learn two methods:
1. Bisection
2. Newton-Raphson in 1D
Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Motivation: Fluid Mechanics


Stuart Churchill and Stephan Zajic
(2002) Theoretical result for friction
factor in a pipe:
2

f
f
2
1
0.5Re

3.2 227
2500

ln
0.5Re 0.436 2
f
0.5Re

How would you find the friction factor if you


knew the Reynolds number?
Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Finding the Friction Factor from Re


First we should rearrange the equation and
define f(x) formally:
2

f
f
1
0.5 Re

f x 3.2 227
2500

ln

0.5 Re 0.436 2
0.5 Re

Whats x?
Whats f? Whats the other f?
Why did I rearrange it into this form?
Chemical Engineering Department

2
f

The Cooper Union for the Advancement of Science and Art

Does a Root Exist?


We want to find the number p which satisfies
f(p) = 0.
We shouldnt even try to find p if it doesnt exist.
Which theorem proves the existence of a root?

Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Intermediate Value Theorem


Existence of an intermediate value:

f C a, b , K : f a K f b
c a, b : f c K

For K = 0 and c = p:

if :
then :

f C a, b , f a 0, and f b 0,
p a, b exists such that f p 0

Note that we can flip a and b; IVT is still valid


Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Proving Existence for Our Eqn.


Is f(x) continuous for the Churchill-Zajic eqn.?
What are possible choices for a and b?
Should f(a) or f(b) ever be equal to zero?

Is f(a) always less than f(b)?

Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Activity: Existence of a Root


Time: 10 min to do, 10 min discuss
For the Churchill-Zajic equation below:
1. Rewrite the function in the form f(x) = 0 with x
clearly defined. Assume Re = 20000.
2. Prove that f is continuous by picking a and b
3. Prove that a root exists
2

f
f
1
0.5 Re

f x 3.2 227
2500

ln

0.5 Re 0.436 2
0.5 Re

Chemical Engineering Department

2
f

The Cooper Union for the Advancement of Science and Art

Answer: Existence of a Root


let

x 2 f :

227

f x 3.2
ln 0.5 Re
1 x
0.436

0.5 Re
2500 2 1
2

ln
x

x
3 ln x 0

0
1
2

2
0.25 Re
0.436

Let a = 33 and b = 1. f is the sum of continuous


functions on [1,33] and is therefore continuous.
Since f is a continuous function, f(a) < 0, and
f(b) > 0, a root p must exist between 33 and 1.
Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Bisection Method for Root Finding


Have you heard of this before?
The bisection method will find a p* (our best
guess at the real root p) by starting with an
initial interval (a,b), cutting it in half (bisecting
it), then picking the half with the root in it
successively until you tell it to stop. See Figure
2.1 on p. 34 of the book.
How do you pick the half with the root?
Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Steps in the Bisection Method


1. Identify f(x) and x clearly
2. Pick a and b such that f(a)*f(b) < 0
3. Show that f is continuous on [a,b]
4. Find your first guess middle of the interval
5. If the function value there is positive, use the
new guess and a as the new interval
6. If the function value is negative, use the new
guess and b as the new interval
7. Repeat until done (When are you done?)
Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Bisection Algorithm in Words


Input: function f, scalar a, and scalar b
1.
2.
3.
4.
5.
6.

Check that f(a)*f(b) < 0. If not, STOP.


ab
Set p
2
If f(a)*f(p) < 0, set b = p.
Else, set a = p.
If f(p) or (a-b) small enough, STOP.
Go to Step 2.
Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Bisection in Pseudocode
Set TOL
Set a
Set b
While (a - b) > TOL
Find f(a)
Find f(b)
If (f(a)*f(b) < 0 )
p = (a + b)/2;
Find f(p)

If ( f(a)*f(p) < 0)
Set b =p
Else
Set a = p
End while

Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Activity: Writing Comments


Time: 5 min to do, 10 min discuss
In your notes, write the bisection algorithm I
outlined on the previous slide in pseudocode
with a while loop. Include a comment at the
end of every line describing the computers
action on that line. Work by yourself, then
compare with a partner once you are done.
I will ask you to write pseudocode from time to
time, so please use this as practice.
Chemical Engineering Department

The Cooper Union for the Advancement of Science and Art

Answer: Writing Comments


Set TOL % Set the tolerance for exiting the loop
Set a
% Interval for bisection
Set b
% f(a) < 0 < f(b)
While (a - b) > TOL % Continue loop until TOL is reached
Find f(a) % First value of f(a)
Find f(b) % First value of f(b)
If ( f(a)*f(b) < 0 ) % Check to see if the interval is valid
p = (a + b)/2; % Calculate p = midpoint of (a,b)
Find f(p)
% Find f(p), trying to get it to 0
If ( f(a)*f(p) < 0) % Check to see if the interval is valid
Set b =p % If f(a) and f(p) are opposite sign, make p the new b
Else a = p % If f(a) and f(p) are the same sign, use p and b
Chemical Engineering Department

You might also like