Title: Ber
1Numeriska beräkningar i Naturvetenskap och Teknik
- Repetition of loops and conditions etc
2. Solving equations with computer
3. Differential equations part 1
2Numeriska beräkningar i Naturvetenskap och Teknik
- Conditions and loops etc
3Numeriska beräkningar i Naturvetenskap och Teknik
The for loop repeats a fixed number of
times for i00.110 startstepstop statement
s end
--------------------------------------------------
---------- Example The size of generation of an
animal population limited by resources can be
modelled with the logistics equation X(t1)
k X(t) ( 1 - X(t)) where t is the
generations number, X(t) the size (0 to 1) of
generation t, k gives the growth rate without
competition and the available resources are
proportional to (1-X(t))
4Numeriska beräkningar i Naturvetenskap och Teknik
The while loop runs until condition not
met. while ilt1000 difflt0.4 update counter
i calculate diff statements end
Example Same as the for loop model. Now we run
until the difference between two generations is
larger than 40 but we run for at most 1000
generations i.e we have a combined condition.
5Numeriska beräkningar i Naturvetenskap och Teknik
Matlab notation for vectors and matrices For
two vectors a and b a b is the scalar
product a . is the elementwise product a b
dot(a,b) ----------------------------------------
-------------------
Demo x0.50.60.7 sin(x)
sin(0.5)sin(0.6)sin(0.7) y x sin(x)
?, works ? Y x . sin(x) ?
6Numeriska beräkningar i Naturvetenskap och Teknik
Plotting and saving
Example Plot x sin (x) for x from 0 to 100 in
steps of 0.1 Label axis Add name to plot Save
to jpg
7Numeriska beräkningar i Naturvetenskap och Teknik
2D plots and meshes
Example Plot sin (x2 y2) for the grid x-2
to 2 and y-2 to 2 with steps of 0.05 in each
direction. X,Y meshgrid(-20.052,-20.052)
How do X and Y look like? Z sin(X.2
Y.2) Why do we use . ? mesh(X,Y,Z) Plots the
surface, other commands surf etc.
8Numeriska beräkningar i Naturvetenskap och Teknik
- Solving equations
9Numeriska beräkningar i Naturvetenskap och Teknik
Discretization
10Numeriska beräkningar i Naturvetenskap och Teknik
An example using graphs
11Numeriska beräkningar i Naturvetenskap och Teknik
A numerical exemple gtgt bisection method
Sign change
Half of the interval...
again...
and again...
12Numeriska beräkningar i Naturvetenskap och Teknik
Bisection
13Numeriska beräkningar i Naturvetenskap och Teknik
Bisection method A clear limitation of the
method is that the new approximation does not
take into account the value of the function for
the latest x-value it was calculated
at. Compare which gives So, we have
stepped two times but are hardly any closer to
the solution than we were two steps earlier(the
sign has changed though) How can we use the
knowledge we have of the value of the function in
order to guess a new better value?
14Numeriska beräkningar i Naturvetenskap och Teknik
Secant method
Equation of secant
Root
x2, approximation
Iteration formula!
15Numeriska beräkningar i Naturvetenskap och Teknik
The secant method
Code examples
16Numeriska beräkningar i Naturvetenskap och Teknik
Newton-Raphsons method
Let the step between xn and xn-1 tend to zero...
or the eq. of the tangent
17Numeriska beräkningar i Naturvetenskap och Teknik
Iteration principle
1-point method
2-point method
18Numeriska beräkningar i Naturvetenskap och Teknik
19Numeriska beräkningar i Naturvetenskap och Teknik
Our exemple once more
Solve for the root
i.e.
is a possibility. Are there others?
yes, infinitely many!
20Numeriska beräkningar i Naturvetenskap och Teknik
21Numeriska beräkningar i Naturvetenskap och Teknik
The mean value theorem
but
i.e.
or
22Numeriska beräkningar i Naturvetenskap och Teknik
The relative error between two iterations
If G(?) is less than 1 the iteration will
converge. ----------------------------------------
--------------------- If xn och xn1 are close to
a then ? is also an approximation for a, i.e. if
G(?)lt1 around the root a, the iteration will
Converge. The convergence is quicker the smaler
G is in the surrounding of a. ------------------
-------------------------------------------
How can this be used in order to optimize how we
write the iteration formula?
23Numeriska beräkningar i Naturvetenskap och Teknik
Let us rewrite f(x)0
that is
Assume that the start value is a good
approximation for the root
which leads to
24Numeriska beräkningar i Naturvetenskap och Teknik
Newton Raphsons modified method
Our exemples
Applying Newton Raphsons modified method
25Numeriska beräkningar i Naturvetenskap och Teknik
Error propagation
26Numeriska beräkningar i Naturvetenskap och Teknik
Error propagation
27Numeriska beräkningar i Naturvetenskap och Teknik
Discretization and error propagation
28Numeriska beräkningar i Naturvetenskap och Teknik
Demo Newton-Raphsons modifierade metod etc.