You are on page 1of 13

Høgskolen i Telemark

Avdeling for teknologiske fag

FM1115 Scientific Computing

Final exam

SUGGESTED SOLUTION

Lecturer: Svein Linge

Class: Date: Time:

1 Master 11 Dec., 2015 09.00 – 12.00

Number of pages: Number of problems: Number of attachments:


5 3 0

Accepted tools:
Pocket calculator.

It is the responsibility of each candidate to check that the number of pages is


complete and in accordance with the above mentioned specifications.

1
1.
a)

Let f ( x) = sin( x) and g ( x) = x 2 . The graphs of these two functions will cross for
x = x r , where x r is close to 1. The graphs will also cross for x = 0, but we do not
consider this solution now.

Use x = 1 as your starting value and do two iterations with Newton’s method to
demonstrate how this method can be used to find an approximate value for x r .
Show your calculations.

Suggested solution

Solving sin( x) = x 2 is equivalent to solving f ( x) = sin( x) − x 2 = 0 , which means finding


x where the left hand side is zero. Graphically, it means finding where the graph crosses
the x-axis (only the solution close to 1 is asked for). A plot looks like:

Choosing x = 1 as our starting value, two iterations with Newton’s scheme give us

x0 = 1
f ( x0 ) sin(1) − 12
x1 = x0 − = 1− ≈ 0.89139
f ' ( x0 ) cos(1) − 2 ⋅ 1
f ( x1 ) sin(0.89139) − (0.89139) 2
x 2 = x1 − = 0.89139 − ≈ 0.87
f ' ( x1 ) cos(0.89139) − 2 ⋅ (0.89139)

2
b)
Assume you are to test a function you have just implemented (of some numerical
method). Suggest three sensible ways of doing this.

Suggested solution

1. Compare output from the code with hand computations.

2. Let the code solve a problem that will be computed exactly (to machine
precision), - if the code works correctly. For example, if the integrand is a straight
line, the Trapezoidal method should get the exact answer (to machine precision).

3. Demonstrate correct convergence rates. In cases where the exact error can be
computed, we can investigate how quickly the error goes to zero as the
discretization is made finer and finer and compare this to theory. For example,
letting the number of subintervals (n) increase in the Trapezoidal method
(numerical integration) should cause the error to drop as 1/n2.

c)
The Python program below handles some measurement data that get stored in an array M.

Explain briefly what the program does and write down the content of M after each
iteration of the outer for-loop (i.e. the loop running over “i”).

3
Suggested solution

The program sorts the measurements in descending order, i.e. so that the largest number
appears in M[0], then the second largest in M[1], and so on.

The first iteration of the outer for loop finds the largest number of all 4 measurements and
places that number on in M[0]. The second iteration finds the largest number of the
remaining 3 numbers and puts that number in M[1]. The third iteration finds the largest
number of the remaining 2 numbers and puts that number in M[2]. The last place, i.e.
M[3] will then necessarily contain the smallest number of all the measurements, so no
more checking is required.

i = 0, after the first iteration: [ 11. 1. 2. 7.]

i = 1, after the second iteration: [ 11. 7. 2. 1.]

i = 2, after the third iteration: [ 11. 7. 2. 1.]

d)
A friend from your math class invites you for a trip that involves walking in flat terrain
(as always, you have brought your laptops with Python installed…). According to the
map, there is a path ahead and you would like to reach that path. You know your present
location and the map tells you the shape and location of the path ahead, as indicated in
Figure 1, where an xy-coordinate system has also been included.

Figure 1 The path (blue curve) and your location (black circle with center at coordinates
8, 1) in an xy-coordinate system.
4
To challenge you, your friend gives you the following problem:
Let us introduce an xy-coordinate system on the map (as shown in Figure 1), so that the
3
path corresponds to the graph of y = 1 + x 2 , x ∈ [0,4] , and our position is (8, 1).

Write a program (Python/Matlab) that finds approximately the least distance we have to
walk to reach the path.

Note: the problem may be solved analytically, but that is not the issue here.

Suggested solution

Running the program gives 6.63, i.e. km, as printout.

(…the import of matplotlib.pyplot is there because the path of y was also plotted, - but
these codelines are not shown here, since it was not asked for)

2.
A scalar quantity y depends on time t according to

5
t ∈ [0,4] .
1
y '+ y = t y 3 , y (0) = ,
2

This differential equation can be solved analytically, but that is not the issue here.

a)
i) Derive a computational scheme based on the forward Euler method that
computes an approximation to the solution y (t ) over the given time interval.

ii) Let ∆t = 0.1 and do two iterations with your scheme to illustrate how it works,
i.e. compute an approximate value for y (0.2) .

Suggested solution

i)

y i +1 − y i
= − y i + t i y i3 , i = 0, 1, 2, …
∆t

(
y i +1 = y i + ∆t − y i + t i y i3 )
y i +1 = (1 − ∆t ) y i + ∆t t i y i3

ii)

1
∆t = 0.1 , y0 =
2

y1 = (1 − ∆t ) y 0 + ∆t t 0 y 03 = (1 − 0.1) 12 + 0 = 0.45

(
y 2 = (1 − ∆t ) y1 + ∆t t1 y13 = (1 − 0.1) 0.45 + 0.1 ⋅ 0.1 ⋅ 0.45 3 ≈ 0.41)

b)
i) Derive a computational scheme based on the backward Euler method that
computes an approximation to the solution y (t ) over the given time interval.
Explain briefly any choices you may make.

ii) Implement your scheme in a Python/Matlab program.

6
Suggested solution

i)

y i +1 − y i
= − y i +1 + t i +1 y i3+1 , i = 0, 1, 2, …
∆t

(
y i +1 = y i + ∆t − y i +1 + t i +1 y i3+1 ) ,

(− ∆t t i +1 ) yi3+1 + (1 + ∆t ) yi +1 + (− yi ) = 0 ,
which is a nonlinear algebraic equation on the form

ay i3+1 + by i +1 + c = 0 ,

where
a = −∆t t i +1 , b = 1 + ∆t , c = − yi .

To advance the solution from one point in time to the next, this algebraic equation must
be solved. The choice made here, is to do that by Newton’s method. Newton’s method is
often a good choice when two successive y values are quite similar, since then y i is a
good initial guess (in Newton’s method) for y i +1 .

ii)
Even if not asked for, the code is here implemented to also plot the true solution (which is
known in this particular case), so that the quality of the numerical solution may be
judged.

7
Running the code produces the plot

8
The known exact solution is seen in red, while the numerical solution is in dashed blue.

c)
Derive a computational scheme based on the Crank-Nicolson method that computes an
approximation to the solution y (t ) over the given time interval.
Explain briefly any choices you may make.

Suggested solution

We use a finite difference approximation for the derivative at the midpoint of the
timestep. However, this derivative is then expressed as the average of the derivatives at
each end of the timestep.

y i +1 − y i
∆t
= 1
2
[(− y i ) (
+ t i y i3 + − y i +1 + t i +1 y i3+1 , )] i = 0, 1, 2, …

y i +1 = y i + ∆t
2 (− y i )
+ t i y i3 + ∆t
2 (− y i +1 )
+ t i +1 y i3+1 ,

(− ∆2t t i +1 )yi3+1 + (1 + ∆2t )yi +1 + [( ∆2t − 1)yi − ∆2t t i yi3 ] = 0 ,


which is a nonlinear algebraic equation on the form

ay i3+1 + by i +1 + c = 0 ,
where
∆t
a = − ∆2t t i +1 , b = 1+ , c = ( ∆2t − 1)y i − ∆2t t i y i3 .
2

As with BE we choose to use Newton’s method to solve this nonlinear algebraic


equation.

9
d)
i) Derive a computational scheme based on Heun’s method that computes an
approximation to the solution y (t ) over the given time interval.

ii) Let ∆t = 0.1 and do two iterations with your scheme to illustrate how it works,
i.e. compute an approximate value for y (0.2) .

Suggested solution

i)
We start out as for the CN method, i.e. use a finite difference approximation for the
derivative at the midpoint of the timestep.

y i +1 − y i
∆t
= 1
2
[(− y i ) (
+ t i y i3 + − y i +1 + t i +1 y i3+1 , )] i = 0, 1, 2, …

However, at this point we choose to compute y i +1 that appears on the right hand side by a
FE scheme. The whole scheme then becomes

y * = y i + ∆t − y i + t i y i3 ( )
y i +1 = y i + ∆t
2
[(− y + t y ) + (− y
i i
3
i
*
+ t i +1 y *( ) )].
3

These two equations are solved in the order given (top-down) for each timestep.

ii)
1
∆t = 0.1 , y0 =
2

( )
y * = y 0 + ∆t − y 0 + t 0 y 03 = 12 + 0.1(− 12 + 0 ) = 0.450
y1 = y 0 + ∆t
2
[(− y 0 + t 0 y 03 ) + (− y *
( ) )]
+ t1 y *
3

[ (
y1 = 12 + 02.1 (− 12 + 0 ) + − (0.45) + 0.1(0.45)
3
)] = 0.453
( ) ( ( ))
y * = y1 + ∆t − y1 + t1 y13 = 0.453 + 0.1 − 0.453 + 0.1 0.4533 = 0.409
y2 = y + [(− y + t y ) + (− y + t ( y ) )]
1
∆t
2 1
3
1 1
*
2
* 3

= 0.453 + [(− 0.453 + 0.1 (0.453 )) + (− (0.409) + 0.1(0.409 ) )] = 0.411


0.1 3 3
y2 2

10
3.
a)
For a function of time u (t ) , the Taylor series around a point in time t n can be written as

Use the Taylor series to show that (if ∆t << 1 ):

Suggested solution

If we write t = t n + ∆t , we may express the Taylor series as

Next, we rearrange the equation by isolating on one side

Finally, dividing through by Δt, we arrive at

which means that

with the remaining terms identifying the error of the approximation. When ∆t << 1 , the
terms in the error get smaller and smaller as the exponent increases.

11
b)
Often, it is preferred to scale PDEs.

i) Why? Explain briefly.

ii) Consider the heat (or diffusion) equation given as


∂u ∂ 2u
=β 2 , x ∈ (0, L) , t ∈ (0, T ] ,
∂t ∂x
with some appropriate initial and boundary conditions.

Show that by introducing new variables


u − u* x t
u= , x= , t = ,
uc − u *
L tc
(where u c , u * and t c are chosen constants)

the equation may be written as

∂u ∂ 2 u  Tβ 
= , x ∈ (0,1) , t ∈  0, 2  .
∂t ∂x 2  L 

In particular, there is no need to run one simulation for each parameter value
Suggested solution combination, since solutions with many different parameter combinations may
be derived directly from a single simulation run. This is useful if simulations
i) are (too) time consuming.
Scaling reduces the number of physical parameters in the problem, which in turn
simplifies analyses.

ii)
u − u*
From u = ( )
we get that u = u u c − u * + u * , which means that
uc − u *

∂u ∂u ∂u ∂u ∂ 2u ∂ 2u
∂t
=
∂t
( )
uc − u * , =
∂x ∂x
(
uc − u * , ) =
∂x 2 ∂x 2
( )
uc − u * .

Furthermore, the chain rule implies that

∂u ∂u dt ∂u 1 ∂u ∂u dx ∂u 1 ∂ 2u ∂  ∂u 1  dx ∂ 2 u 1
= = , = = , =   = .
∂t ∂t dt ∂t t c ∂x ∂x dx ∂x L ∂x 2 ∂x  ∂x L  dx ∂x 2 L2

Thus,

12
∂u  uc − u *  ∂ 2u  u − u * 
  = β 2  c 2  .
∂t  tc  ∂x  L 

L2
If we now choose t c = , we get
β

∂u ∂ 2 u
= .
∂t ∂x 2

x
Since x ranges from 0 to L, x = implies that x ∈ (0,1) .
L
t tβ  Tβ 
Also, since t ranges from 0 to T, t = = 2 implies that t ∈  0, 2  .
tc L  L 

------------------------------------------------------------
END

13

You might also like