Professional Documents
Culture Documents
Aman W
Department of Applied Physics
University of Gondar
contents
Introduction
Root finding of an equation
Guess and check
Iterative algorithms
Bisection method
Newton-Raphson
Secant method
Ordinary differential equations(ODE)
Numerical differentiation
Forward & backward difference
Central difference
Introduction
x = 25
We need a good way to generate
epsilon = 0.01
guess step = epsilon**2
If we could guess possible values Numguess = 0.0
ans = 0.0
for square root, g,
We should use a method(check) while abs(ans**2-x) >= epsilon and ans <= x:
where our guess is true/false
ans = ans+step
Since we can not guarantee an
exact answer, but look for Numguess = Numguess+ 1
time to find square root while abs(ans**2-x) >= epsilon and ans <= x:
Too large, it may
ans = ans+step
skip/jump over answer
Numguess = Numguess+ 1
without getting close
enough print('Numguess = '+str(Numguess))
x3 = (x1 + x2)
x3 is the midpoint of the interval. If f (x2) f (x3) < 0, then the root must
be in (x2, x3) and we record this by replacing the original bound x1 by
x3.
Otherwise, the root lies in (x1, x3), in which case x2 is replaced by x3.
In either case, the new interval (x1, x2) is half the size of the original
interval.
The bisection is repeated until the interval has been reduced to a small
value , so that
Example
Finding the root of a polynomial
First, two numbers a and b have to be found such that f(a) and f(b) have
opposite signs.
For the above function, a = 1 and b = 2 satisfy this criterion
F(1)=-2 and f(2)=4
Because the function is continuous, there must be a root within the interval [1,
2].
so the midpoint is
C1=(a+b)/2=1.5
x = 0.0 x=g x
We should select half of x, g= (0+x)/2
If by chance we got the answer, we succeed(most probably
not)
If g not close enough, that is it may be
Too big
Too small
If g**2 > x, we know g is too big, so we search again
If this new g is for instance too small, g**2 <x, we know that
g is too small, we search again
f ( x1 )
Newton-Raphson Method
x i x i1
f (x )
x i1 x i ' i
f (x i )
f (x i1 ) f (x i )
f (x i )
'
x i1 x i
Secant Methods (cont)
f (x i )x i1 x i
x i1 x i
f (x i1 ) f (x i )
df ( x) f ( x h) f ( x)
lim
dx h 0 h
In our first equation,
Once you can do that, you can solve the ODE for all t values
by just continuing stepping to larger times one small h at a
time
Simply we use integration to solve differential equations
Eulers rule (Figure ) is a simple algorithm for integrating the
differential equation () by one step and is just the forward-
difference algorithm for the derivative:
Velocity from position
We can turn this into an approximation rule for y (t) =f (x)
by replacing the limit as h approaches 0 with a small but finite
h: Let us take the trajectory is that of a projectile
dy y (t h) y (t )
y ' (t )
dt h
Forward-difference
approximation (slanted
dashed line) for the
numerical first derivative at
time t.
Euler Method
Explicit Euler Method
Consider Forward Difference
y (t t ) y (t )
y ' (t )
t
y (t t ) y (t ) tf (t , y (t ))
Which implies
36
Euler Method
t0 0
ti i t
t t
n
The Explicit Euler Method Formula
37
Take a function which is related to that parabola
Exact differentiation
y(t) y(t t)
y'(t)
t
Which implies
43
Euler Method
44
Example
Consider: y(t)=-15y(t), t0, y(0)=1
Exact solution: y(t)=e-15t, so y(t)0 as t0
If we examine the forward Euler method, strong
oscillatory behaviour forces us to take very small
steps even though the function looks quite
smooth
Forwards vs Backwards difference
f ' c ( x)
1
f ' f ( x) f 'b ( x)
2
f ( x h) f ( x ) f ( x ) f ( x h) h h
f ' ' ( x) f ' ' ( x)
2h 2 2
h2
f ' ' ' ( x) ....
6
f ( x h) f ( x h ) Note that all odd ordered
f ' c ( x) O(h )
2
terms cancel too. Further the
2h
central difference is symmetric
about x, forward & backward
differences are clearly not.
Central difference
Best h values
Consider again the A simple y = 1.0 # initial y
differential equation is the
equation that gives rise to the
y0 = 1.0 # initial y0
exponential decay law, dt = 0.1 # time step
Implicit Euler Method(backward lamb = 1.0 # decay
difference, central difference)
constant
Note: This implicit solution is
stable for large step sizes. Nmax=101 # no. of steps
Compare for y=y0
dt = 0.01 for i in range(Nmax):
dt = 0.05
dt = 0.15
print i*dt, y
y = (1.0 - dt*lamb)*y