Title: 7th lecture
1Elementary Programming using Mathcad (INF-20303)
2- Finding roots Rule of False position (Regula
Falsi) - Consider interval
- Given and
- so 1 or more roots on
interval
b
a
c
- Note its not always the same side of the
interval - In some cases it will select only the left part,
sometimes only the right part of the interval,
sometimes alternating (see next slide).
3In interval point of inflection
b
1
a
2
Consider this when preparing the function
RegulaFalsi!
4Algorithm
Rule of False position converges always,
sometimes slow.
5- Finding roots Newton Raphson
- Select a point on the graph of f(x)
- Draw the tangent in that point
- Determine intersection of the tangent with
x-axis - Repeat procedure in the intersection until root
is accurate
6p1
p2
p0
Recursive formula
Problem?
7Newton Raphson does not converge always, for
example
Start x0 gives
8Fixed point iteration
Suppose you want a root of
So
Or
and
(equivalent form)
Fixed point iteration works as follows
x0 4 x1 3.31662 x2 3.10375 x3 3.01144 .
. . xn 3.0
Start value is
Compute successively
etc. Hopefully it will converge.
Stop when
9In general, other equivalent forms can be
derived, e.g.
10- Iterations
- Sometimes iterations do not converge
- An iteration is of form
- (F does not need to be a mathematical
function!) - Convergence requires for all
- This should also hold for
- If iteration does not converge try another
- starting point.
11- Advantages of iterations seen from Computer
Science perspective - Iterations are simple to program
- If iteration converges then arithmetic errors
will be resolved - in the next iteration
- Suppose you need to know the roots of
- Which method is better?
- quadratic-formula
- iterative method like Rule of False position
12Stop criteria
E.g. in the Newton iteration (sqroot) to
compute , we used criterion tol.
- Suppose stop criterion is
- absolute
- relative
- mixed
e0.01
When a 1 then tolerance 1. When a 10 then
tolerance 0.1 For high values rather stringent
Stringent for small values, better suited for
high values
For small values of a ad is small absolute
criterion For high values of a e is negligible
relative criterion
13Answer practical 6, section 15.1 assignment 2
- Function will not work when b 0! (we have to
adapt function again) - If explicit or implicit prerequisites about
arguments may be not - fulfilled
TEST and generate ERROR message!
14Answer practical 6, section 15.1, assignment 3
prepare a function that finds all divisors of a
given number, the number itself excluded.
What is the test in this function? Assume N is
the number. We can test whether N can be divided
by 2 by
(mod is applied here instead of function
remainder)
More generally is i a divisor of N?
To find all divisors on N, repeat this
(Why not a while?)
15 When a divisor is found, it should be stored in
a vector, e.g. v (here)
No need to test N itself
j indicates position in vector
Make room for the next number
vóór de for-lus
16Do we need all N-1?
is sufficient
Result vector v NOT
Assignment indicated that Ngt0.
17(Assignment was Give divisors lt N, so this is
actuallynot correct)
18Suppose we take advantage of the knowledge that 1
is always a divisor and insert 1 into the vector
without further testing
19(No Transcript)
20mod(1,2) ? 0
21N 1 not correct We need yet another
error message!
Conclusion sometimes an optimization leads to a
complicated function.
22- Divide and conquer
- Suppose one needs to prepare a function to find
the first 100 - prime numbers
- Prepare two functions
- one function that tests whether a number is
prime - another function that finds the first 100 prime
numbers - In this second function you call the first
function. -
- Separate responsibilities, resulting a function
that is clear and can be maintained more easily
23Answer section 15.1, assignment 4 Series to
compute sine
Make
Repeat as long as needed
24Test your function
25Correct
Not correct
26Do not
The magnitude of t related to the value of r is
of importance!