INF-MAT3360 - Mandatory Exercise 1 Exercise 1 Vegard Kjelseth University of Oslo

advertisement
INF-MAT3360 - Mandatory Exercise 1
Vegard Kjelseth
University of Oslo
vokjelse@ifi.uio.no
March 8, 2013
Exercise 1
a)
The solution of the ODE u0 (t) = au (t), t > 0, u (0) = 1 is u (t) = Ce at . The
value of C is found using the initial conditions. u (0) = C = 1. The full solution
is u (t) = e at .
b)
Given the following permutation of the initial conditions, ũ (0) = 1 + e, the
solution is given by:
ũ (t) = (1 + e) e at
The error is then given by:
e (t) = ũ (t) − u (t) = ee at
The error increases exponentially with t for a > 0, while for a < 0 the error
decreases exponentially. It follows that the problem is stable for a < 0 and
unstable for a > 0. The code following below plots the difference between the
solution and the perturbed solution. As expected, the difference decreases exponentially for a < 0, while for a > 0 it increases exponentially. This confirms
the analysis above.
1
2
% Initialize t
t = linspace ( 0 , 1 , 1 0 0 0 ) ;
3
4
5
% Initialize a
a = 100;
6
7
8
% Initialize u
u = exp ( a * t ) ;
9
10
11
12
% I n i t i a l i z e u_perturbed
epsilon = 0 . 0 1 ;
u_perturbed = (1+ e p s i l o n ) * exp ( a * t ) ;
13
14
15
16
% Plot d i f f e r e n c e between s o l u t i o n s
p l o t ( t , abs ( u−u_perturbed ) , ’ b ’ ) ;
legend ( ’ D i f f e r e n c e ’ ) ;
1
c)
The code for the forward Euler method follows below. The elements are calculated using the formula:
vm+1 = vm + havm = vm (1 + ha)
Solving this yields the following general formula for the different elements:
vm = (1 + ha)n
This shows that there is a clear danger with this approach in cases where
ha < −1 since this will result in an oscillating solution and an unstable scheme.
Running the script with the given values confirm this since for k = −100 and
k = −1000 we get increasingly higher oscillations that don’t correspond to the
actual solution. Since h = 1/30 this is explained by the fact that ha < −1 for
these values of k.
1
% S c r i p t f o r s o l v i n g u ’ ( t )= a * u ( t ) u s i n g f o r w a r d E u l e r
2
3
4
5
% I n i t i a l i z e time step
n = 3 0 ; % Number o f s t e p s
h = 1/n ;
6
7
8
9
% I n i t i a l i z e u vector
u = zeros ( 1 , n + 1 ) ;
u ( 1 ) = 1 ; % I n i t a l i z e u ( t =0)
10
11
12
% Initialize a
a = − 1;
13
14
15
16
17
18
% Perform forward e u l e r
for i =1:n
der = a * u ( i ) ;
u ( i +1) = u ( i )+h * der ;
end
19
20
21
22
23
24
25
26
% Plot solution
t = linspace (0 ,1 , n +1);
plot ( t , u , ’b ’ ) ;
hold on ;
p l o t ( t , exp ( a * t ) , ’ g ’ )
legend ( s t r c a t ( ’ Forward E u l e r s o l u t i o n t o u ’ , char ( 3 9 ) , ’ ( t )= a * u ( t ) ’ ) , . . .
’ E x a c t S o l u t i o n : exp ( a * t ) ’ ) ;
The code for the backwards Euler method follows below. The backwards
Euler solution is calculated using vm+1 − h f (vm+1 ) = vm which yields:
vm+1 − h a vm+1 = vm+1 (1 − ha) = vm ⇒ vm+1 = vm / (1 − ha)
Solving this yields the following general formula:
vm =
1
(1 − ha)n
The problem with this approach is that for ha = 1 it will divide by zero, in other
words for a = 30. For 30 < a < 60 we will get an oscillating behaviour with
2
increasingly volatile oscillations, while for a > 60 we will get an oscillating
behaviour converging towards zero. The solution is therefore not stable for
any values of a resulting in ha ≤ 1, something that is confirmed by running the
script for these values.
1
% S c r i p t f o r s o l v i n g u ’ ( t )= a * u ( t ) u s i n g b a c k w a r d E u l e r
2
3
4
5
% I n i t i a l i z e time step
n = 3 0 ; % Number o f s t e p s
h = 1/n ;
6
7
8
9
% I n i t i a l i z e u vector
u = zeros ( 1 , n + 1 ) ;
u ( 1 ) = 1 ; % I n i t a l i z e u ( t =0)
10
11
12
% Initialize a
a = 1;
13
14
15
16
17
% Perform forward e u l e r
for i =1:n
u ( i +1) = u ( i )/(1 − h * a ) ;
end
18
19
20
21
22
23
24
25
% Plot solution
t = linspace (0 ,1 , n +1);
plot ( t , u , ’b ’ ) ;
hold on ;
p l o t ( t , exp ( a * t ) , ’ g ’ )
legend ( s t r c a t ( ’ Backward E u l e r s o l u t i o n t o u ’ , char ( 3 9 ) , ’ ( t )= a * u ( t ) ’ ) , . . .
’ E x a c t S o l u t i o n : exp ( a * t ) ’ ) ;
Exercise 2
a
The code for solving the equations follows below. The equations are solved
using the forward Euler method. The plot for ζ = 0 can be found in figure 1
on page 5, while the plot for ζ = 0.001 is in figure 2 on page 6. The results are
similar in both cases, but clearly show that if the model is accurate there won’t
be a zombie apocalypse even if there is an outbreak.
Both cases show that the number of infected and the number of zombies never
rise significantly above 0. This can be explained by the fact that α > β which
means that the zombies are killed faster than the susceptible are being infected.
As for the difference between ζ = 0 and ζ = 0.001, the plot for ζ = 0.001
shows a slightly higher amount of deceased people than for ζ = 0. Using a
value of ζ 6= 0 means that the number of zombies increases not just because of
people being infected, but that dead susceptible people can become zombies as
well. Given the model we’re using this is not realistic since we are assuming
that people are infected through contact with zombies, and not through black
magic.
Running the simulation for longer than t = 100 shows the number of suscep3
tible continuing to decrease and the the dead increasing, but with the number
of zombies and infected staying close to 0. One could therefore argue that
even though the zombies don’t take over the planet, an outbreak would still
cause the death of all of humandkind (assuming all people are susceptible).
The reason this won’t happen is that the continued decrease in the number of
susceptible in the simulation is due to the fact that the number of zombies is
> 0, but it is still << 1. Since it’s not possible to have a fraction of a zombie,
the reality is that given the parameters used the zombie outbreak would be put
down quickly and we would all be able to continue with our lives.
1
2
3
% I n i t i a l i z e time step
h = 0.001;
n = 100/h ;
4
5
6
7
8
9
%
S
I
Z
R
Create vectors
= zeros ( 1 , n + 1 ) ;
= zeros ( 1 , n + 1 ) ;
= zeros ( 1 , n + 1 ) ;
= zeros ( 1 , n + 1 ) ;
%
%
%
%
Susceptible
Infected
Zombies
Removed / Dead
10
11
12
13
14
15
% Initialize vectors
S (1) = 1000;
I (1) = 0;
Z(1) = 2;
R(1) = 0;
16
17
18
19
20
21
22
23
24
% Set constants
Sigma
= 0;
alpha
= 0.03;
beta_var = 0 . 0 6 ;
delta
= 0.001;
sigma
= 0.001;
rho
= 0.1;
zeta
= 0 ; % Constant t o vary − c o n v e r s i o n from dead t o zombie
25
26
27
28
29
30
% Perform forward e u l e r
for i =1:n
% Susceptible
der = Sigma − b e t a _ v a r * S ( i ) * Z ( i )− d e l t a * S ( i ) ;
S ( i +1) = S ( i )+h * der ;
31
32
33
34
% Infected
der = b e t a _ v a r * S ( i ) * Z ( i )− rho * I ( i )− d e l t a * I ( i ) ;
I ( i +1) = I ( i )+h * der ;
35
36
37
38
% Zomb ies
der = rho * I ( i )− alpha * S ( i ) * Z ( i )+ z e t a * R( i )− sigma * Z ( i ) ;
Z ( i +1) = Z ( i )+h * der ;
39
40
41
42
43
% Removed / Dead
der = d e l t a * S ( i )+ d e l t a * I ( i )− z e t a * R ( i )+ alpha * S ( i ) * Z ( i ) ;
R ( i +1) = R ( i )+h * der ;
end
44
45
46
47
48
49
50
51
% Plot solution
t = linspace (0 ,100 ,n +1);
plot ( t , S , ’b ’ ) ;
hold on ;
plot ( t , I , ’ r ’ ) ;
hold on ;
plot ( t , Z, ’k ’ ) ;
4
Figure 1: Plot for ζ = 0
52
53
54
hold on ;
plot ( t , R, ’g ’ ) ;
legend ( ’ S u s c e p t i b l e ’ , ’ I n f e c t e d ’ , ’ Zombies ’ , ’ Removed / Dead ’ ) ;
b
In this exercise I have used the same code as listed above, but switching the
values of β and α so β = 0.06 and α = 0.03. The plot showing this scenario can
be found in figure 3 on the following page. As noted briefly above using these
values means that people are infected faster than zombies are being put down,
and the result is a zombie apocalypse which can be seen in the figure.
Exercise 3
Positive Definiteness
For the operator L to be positive definite it has to satisfy h Lu, ui ≥ 0 with
equality only if u = 0.
h Lu, ui =
Z1
(u ( x ))2 − u00 ( x ) u ( x ) dx
0
Using integration by parts on the second expression yields:
Z1
1
1 Z
u00 ( x ) u ( x ) dx = u0 ( x ) u ( x )0 −
0
0
5
u0 ( x )
2
dx
Figure 2: Plot for ζ = 0.001
Figure 3: Plot for β = 0.06, α = 0.03
6
1
u0 ( x ) u ( x )|0 = 0 since u (0) = u (1) = 0. Plugging this into the initial
equation yields:
h Lu, ui =
Z1
(u ( x ))2 + u0 ( x )
2
dx
0
This is clearly non-negative, and for the expression to be equal to zero both
u and u0 has to be equal to zero. This proves that the operator L is positive
definite.
Symmetry
For the operator L to be symmetric it has to satisfy the following equality:
h Lu, vi = hu, Lvi
Where u, v are solutions to the system described by L.
h Lu, vi =
hu, Lvi =
R1
0
R1
u ( x ) v ( x ) − u00 ( x ) v ( x ) dx
u ( x ) v ( x ) − u ( x ) v00 ( x ) dx
0
From this it is clear to see that for the equality to hold
R1
u00 ( x ) v ( x ) dx must
0
be equal to
R1
u ( x ) v00 ( x ) dx. Using integration by parts on the first expression
0
yields:
Z1
1
1
1 Z
u00 ( x ) v ( x ) dx = u0 ( x ) v ( x )0 − u ( x ) v0 ( x )0 + u ( x ) v00 ( x ) dx
0
0
Since u and v both are solutions to L this means that u (0) = v (0) = u (1) =
1
1
v (1) = 0. It follows that u0 ( x ) v ( x )|0 = u ( x ) v0 ( x )|0 = 0, which yields :
Z1
00
u ( x ) v ( x ) dx =
Z1
u ( x ) v00 ( x ) dx
0
0
So h Lu, vi = hu, Lvi and L is symmetric.
Uniqueness
Assume that u1 and u2 both are solutions to L, so Lu1 = f and Lu2 = f . I let
e = u1 − u2 . This yields:
Le = Lu1 − Lu2 = f − f = 0
From this it follows that h Le, ei = 0 due to the fact that Le = 0. Since L is
positive definite it also follows from Lemma 2.4 that e = 0 and u1 = u2 . It is
therefore clear that problem has a unique solution.
7
What is the solution?
Since f ( x ) = sin (kπx ) an initial guess would be that the solution is of the
form u ( x ) = A sin (kπx ). This covers the boundary conditions since u (0) =
u (1) = 0. I plug this into the differential equation:
u ( x ) − u00 ( x )
2
= A sin
(kπx ) +A (kπ ) sin (kπx )
= A 1 + (kπ )2 sin (kπx )
= f (x)
= sin (kπx )
Clearly the solution
u ( x ) = A sin (kπx ) solves the differential equation as
2
long as A 1 + (kπ ) = 1. This gives us the following value for A:
A=
1
1 + (kπ )2
The final solution is then:
u (x) =
sin (kπx )
1 + (kπ )2
Implement a numerical scheme to solve the PDE
The differential equation can be written as:
u ( x ) = u00 ( x ) + f ( x )
I will use the result of a Taylor series expansion given by:
u ( x + h) − 2u ( x ) + u ( x − h)
= u00 ( x ) + Eh ( x ) ≈ u00 ( x )
h2
2
uh
Where the error term Eh satisfies | Eh ( x ) | ≤ M12
, and where the constant Mu
is given by :
Mu = sup |u(4) ( x ) |
x
This yields:
u ( x + h) − 2u ( x ) + u ( x − h)
+ f (x)
h2
Letting v j denote the discrete solution for u x j and rearranging the expression
gives us:
− v j +1 + 2 + h 2 v j − v j −1
= f x j ⇒ − v j +1 + 2 + h 2 v j − v j −1 = h 2 f x j
2
h
u (x) =
I let b denote the vector where bi = h2 f ( xi ) and v denote the vector where
vi = vi for i = 1, . . . , n. v0 and vn+1 are not included since v0 = vn+1 = 0. The
8
solution can be found by solving the equation Av = b where A is given by:

2 + h2
−1
0
−1
2 + h2
..
.
−1
..
.




A=



0
..
.
0
..
...
..
.
..
.
−1 2 + h2
0
−1
.
...
0
..
.
0
−1
2 + h2









We know that a solution exists if the matrix A is diagonally dominant. Here
α = 2 + h2 , γ = β = −1 and the matrix is diagonally dominant if |α| > |γ|
and |α| ≥ | β| + |γ| (I dropped subscripts since they’re the same regardless of
index). This is clearly true for A, so it is diagonally dominant and has a unique
solution. The MATLAB code for solving this follows below:
1
2
3
% I n i t i a l i z e h and n
n = 1 0 0 ; % Number o f p o i n t s b e t w e e n 0 and 1
h = 1/(n + 1 ) ;
4
5
6
% Initialize v
v = zeros ( 1 , n + 2 ) ;
7
8
9
% I n i t i a l i z e x values
x = linspace (0 ,1 , n +2);
10
11
12
% Initialize k
k = 1;
13
14
15
% I n i t i a l i z e function f
f = @( x , k ) ( s i n ( k * pi * x ) ) ;
16
17
18
% Initialize b
b = h^2 * f ( x ( 2 : n + 1 ) , k ) ’ ;
19
20
21
% Initialize A
A = g a l l e r y ( ’ t r i d i a g ’ , − ones ( 1 , n − 1) ,(2+ h ^ 2 ) * ones ( 1 , n) , − ones ( 1 , n − 1 ) ) ;
22
23
24
% Calculate v
v ( 2 : n +1) = A\b ;
25
26
27
28
29
30
31
% Plot solution
plot ( x , v , ’b ’ ) ;
hold on ;
e x a c t = s i n ( k * pi * x ) / ( 1 + ( k * pi ) ^ 2 ) ;
plot ( x , exact , ’ r ’ ) ;
legend ( ’ Numerical S o l u t i o n ’ , ’ E x a c t S o l u t i o n ’ ) ;
32
33
34
35
% Largest error
e r r o r = max ( abs ( v−e x a c t ) ) ;
disp ( s t r c a t ( ’ The l a r g e s t e r r o r i s : ’ , num2str ( e r r o r ) ) ) ;
Analysis
Using h = 1/10000 was not a problem on my computer.
9
k=1
For h = 1/100 to 1/10000 the maximum errors are 6.7346e − 006, 6.857e − 008
and 7.6757e − 010.
k=10
For h = 1/100 to 1/10000 the maximum errors are 8.1911e − 006, 8.3003e − 008
and 8.3151e − 010.
k=100
For h = 1/100 to 1/10000 the maximum errors are 1.4379e − 005, 8.3576e − 008
and 8.3319e − 010.
CPU time and complexity
The script I used for timing this exercise and the next follows below. The timing was done with the plot part of the exercise commented out. For a given n,
h is defined as h = 1/ (n + 1).
Running this exercise with n = 100 led to an average time of t ≈ 0.00095s.
Using n = 1000 led to an average time of t ≈ 0.00135s, while using n = 10000
had an average time of t = 0.007s.
Going from n = 100 to n = 1000 leads to an increase in time by a factor of
around 1.4, while increasing from n = 1000 to n = 10000 only increases the
time by a factor of approximately 5. A reasonable explanation for this is that
at lower n the initialization cost of variables accounts for such a large part of
the total operations that the increase in the time doesn’t properly reflect the
complexity of the core computations in the script. I therefore increased n and
performed the timing for n = 20000 and n = 30000 as well, yielding the times
t = 0.014s and t = 0.021s respectively. This amounts to an increase in time of
2 and 3 for the same increase in n, indicating that the complexity is linear. I
therefore conclude that the complexity is O (n).
This complexity seemed strange to me initially since we’re dealing with an
n-by-n matrix A. On the other hand this matrix is initialized using the gallery
function which does not save elements that are equal to 0, which results in the
number of elements being n + 2 ∗ (n − 1) = 3n − 2. Additionally the script
computes A\b, which is the same as inv ( A) ∗ b to find the values of the elements of v. Finding the inverse of a matrix generally has a complexity larger
than O n2 , so this requires an explanation. An alternative to this would be to
use Algorithm 2.1 from the textbook to solve this problem. We could have used
this since the matrix A is diagonally dominant, and this algorithm is linear in
time. So it is clearly possible to solve this in linear time. My explanation for the
fact that the operation A\b only is linear in time is that MATLAB exploits the
fact that A is tridiagonal and diagonally dominant and that this is the reason it
is able to find the solution in linear time.
1
% S c r i p t f o r t i m i n g e x e r c i s e 3 and 4
10
2
3
iter = 5000.0;
t = cputime ;
4
5
6
7
for i =1: i t e r
run ( ’ e x e r c i s e 4 ’ ) ;
end
8
9
10
% Get a v e r a g e t i m e
avg = ( cputime− t )/ i t e r ;
11
12
13
% Print average time
disp ( s t r c a t ( ’ Average time : ’ , num2str ( avg ) ) ) ;
Exercise 4
Positive Definiteness
For the operator L to be positive definite it has to satisfy h Lu, ui ≥ 0 with
equality only if u = 0.
h Lu, ui =
Z1
−u00 ( x ) u ( x ) − cu0 ( x ) u ( x ) dx
0
I use integration by parts on the first expression which yields:
Z1
00
−u ( x ) u ( x ) dx = −u
0
( x ) u ( x ) |10
+
Z1
0
u (x)
2
dx =
0
0
Z1
u0 ( x )
2
dx
0
Where I used the fact that u (0) = u (1) = 0 to cancel out −u0 ( x ) u ( x ) |10 . I
proceed by using integration by parts on the second expression.
Z1
0
−u ( x ) u ( x ) dx =
−u ( x ) u ( x ) |10
+
Z1
0
u (x) u (x) =
0
0
Z1
u ( x ) u0 ( x )
0
Where I used the fact that u (0) = u (1) = 0 to cancel out −u ( x ) u ( x ) |10 . Moving the left hand-side over yields:
2
Z1
0
u (x) u (x) = 0 ⇒
Z1
u0 ( x ) u ( x ) = 0
0
0
Plugging these expressions into the initial equation yields:
h Lu, ui =
Z1
u0 ( x )
2
dx
0
This expression is clearly non-negative. For the expression to equal zero we
must have u0 ( x ) = 0. This means that u ( x ) must be a constant. Since we
know that u (0) = u (1) = 0, we must have u ( x ) = 0, thus h Lu, ui is always
positive except when u = 0, in which case h Lu, ui = 0. It follows that L is
positive definite.
11
Symmetry
For the operator L to be symmetric it has to satisfy the following equality:
h Lu, vi = hu, Lvi
Where u, v are solutions to the system described by L.
h Lu, vi =
hu, Lvi =
R1
0
R1
−u00 ( x ) v ( x ) − cu0 ( x ) v ( x ) dx
−u ( x ) v00 ( x ) − cu ( x ) v0 ( x ) dx
0
I showed that
R1
u00 ( x ) v ( x ) dx =
0
R1
u ( x ) v00 ( x ) dx in the previous exercise. To
0
prove equality I need to show that
R1
u0 ( x ) v ( x ) dx =
R1
0
u ( x ) v0 ( x ) dx. I do this
0
by using integration by parts:
Z1
0
u ( x ) v ( x ) dx =
u ( x ) v ( x ) |10
−
Z1
0
u ( x ) v ( x ) dx = −
u ( x ) v0 ( x ) dx
0
0
0
Z1
Where I used the fact that u (0) = u (1) = v (0) = v (1) = 0 to cancel out
R1
R1
R1
u ( x ) v ( x ) |10 . Since u0 ( x ) v ( x ) dx = − u ( x ) v0 ( x ) dx it is clear that u0 ( x ) v ( x ) dx 6=
0
R1
0
0
u ( x ) v0 ( x ) dx, and it follows that h Lu, vi 6= hu, Lvi, so L is not symmetric.
0
Uniqueness
The proof for uniqueness in the following exercise was based on the fact that
L was positive definite. The same proof can be applied here, since L is positive
definite in this case as well.
Assume that u1 and u2 both are solutions to L, so Lu1 = f and Lu2 = f . I
let e = u1 − u2 . This yields:
Le = Lu1 − Lu2 = f − f = 0
From this it follows that h Le, ei = 0 due to the fact that Le = 0. Since L is
positive definite it also follows from Lemma 2.4 that e = 0 and u1 = u2 . It is
therefore clear that problem has a unique solution.
What is f?
Since u ( x ) = sin (kπx ) I simply plug this into the differential equation to find
u.
f ( x ) = −u00 ( x ) − cu0 ( x ) = (kπ )2 sin (kπx ) − cπk cos (kπx )
12
Implement a numerical scheme to solve the PDE
The differential equation can be written as:
−u00 ( x ) − cu0 ( x ) = f ( x )
To find a numerical solution I will use the following results of Taylor series
expansions:
u( x +h)−u( x −h)
= u 0 ( x ) + O h2 ≈ u 0 ( x )
2h
u( x +h)−2u( x )+u( x −h)
= u00 ( x ) + Eh ( x ) ≈ u00
h2
(x)
The second expression is the
same as the one used in the previous exercise,
while the expression O h2 satisfies :
|O h2 | ≤ h2 sup |u(3) ( x ) |
x
Plugging this into the differential equation gives us:
−
u ( x + h) − u ( x − h)
u ( x + h) − 2u ( x ) + u ( x − h)
−c
= f (x)
2
2h
h
Multiplying through by 2h2 , rearranging and letting v j denote the discrete so
lution for u x j yields:
v j+1 (−2 − ch) + 4v j + v j−1 (−2 + ch) = 2h2 f ( x )
I let b denote the vector where bi = 2h2 f ( xi ) and v denote the vector where
vi = vi for i = 1, . . . , n. v0 and vn+1 are not included since v0 = vn+1 = 0. The
solution can be found by solving the equation Av = b where A is given by:

4

 −2 − ch


A=
0


..

.
0
−2 + ch
0
4
..
.
−2 + ch
..
.
..
.
...
...
..
.
..
.
−2 − ch
4
0
−2 − ch
0
..
.






0


−2 + ch 
4
We know that a solution exists if the matrix A is diagonally dominant. Here
α = 4, γ = −2 + ch and β = −2 − ch. (Like the previous exercise I’ve dropped
the subscripts since they’re the same regardless of index.)
The first requirement is that |α| > |γ|. |γ| = 2 − ch for ch ≤ 2 which is clearly
less than |α| = 4. For ch > 2 we have |γ| = ch − 2 which is less than |α| = 4
for ch < 6.
The second requirement is that |α| ≥ | β| + |γ|. For ch ≤ 2 we get | β| + |γ| =
2 + ch + 2 − ch = 4, which satisifies the requirement since it’s equal to |α|. For
ch > 2 we get | β| + |γ| = 2 + ch − 2 + ch = 2ch > 4, which does not satisfy
this requirement.
13
In conclusion, the matrix A is diagonally dominant as long as ch is ≤ 2. Since
we’re only operating with values where c/h < 2 ⇒ c < 2h we get ch < 2h2 < 2
since h will always be far less than 1. The matrix A is therefore diagonally dominant for all values we operate with.
The MATLAB code for solving this follows below:
1
2
3
4
%
n
h
c
Initialize h, n, c
= 3 0 0 0 0 ; % Number o f p o i n t s b e t w e e n 0 and 1
= 1/(n + 1 ) ;
= h;
5
6
7
% Initialize v
v = zeros ( 1 , n + 2 ) ;
8
9
10
% I n i t i a l i z e x values
x = linspace (0 ,1 , n +2);
11
12
13
% Initialize k
k = 1;
14
15
16
% I n i t i a l i z e function f
f = @( x , k ) ( ( s i n ( k * pi * x ) * ( k * pi )^2) − c * k * pi * cos ( k * pi * x ) ) ;
17
18
19
% Initialize b
b = 2 * h^2 * f ( x ( 2 : n + 1 ) , k ) ’ ;
20
21
22
% Initialize A
A = g a l l e r y ( ’ t r i d i a g ’ ,( − 2 − c * h ) * ones ( 1 , n − 1) ,4 * ones ( 1 , n) ,( − 2+ c * h ) * ones ( 1 , n − 1 ) ) ;
23
24
25
% Calculate v
v ( 2 : n +1) = A\b ;
26
27
28
29
30
31
32
% Plot solution
%p l o t ( x , v , ’ b ’ ) ;
%h o l d on ;
%e x a c t = s i n ( k * p i * x ) ;
%p l o t ( x , e x a c t , ’ r ’ ) ;
%l e g e n d ( ’ N u m e r i c a l S o l u t i o n ’ , ’ E x a c t S o l u t i o n ’ ) ;
33
34
35
36
% Largest error
%e r r o r = max ( a b s ( v− e x a c t ) ) ;
%d i s p ( s t r c a t ( ’ The l a r g e s t e r r o r i s :
’ , num2str ( e r r o r ) ) ) ;
Analysis
Using h = 1/10000 was not a problem on my computer. The following errors
are calculated using c = h.
k=1
For h = 1/100 to 1/10000 the maximum errors are : 0.0013762, 0.00013439 and
1.3405e − 005.
14
k=10
For h = 1/100 to 1/10000 the maximum errors are 0.0087552, 0.00016744 and
1.2784e − 005.
k=100
For h = 1/100 to 1/10000 the maximum errors are 1.4193, 0.0082552 and
8.2873e − 005.
CPU time and complexity
I used the same script for timing the exercise as I did for exercise 3.
Running this exercise with n = 100 led to an average time of t ≈ 0.001s. Using n = 1000 led to an average time of t ≈ 0.0014s, while using n = 10000
had an average time of t = 0.0071s. This amounts to an increase by a factor
of approximately 1.4 and 5 respectively and paints the same picture as what I
encountered in exercise 3. Assuming the cost of the initialization of variables
takes a larger share of the computational cost for lower values of n I performed
this using n = 20000 and n = 30000 like I did in the previous exercise. The
results were average times of t = 0.01317s and t = 0.020873s respectively, corresponding to approximately an increase in time of a factor 2 for an increase in
n by 2, and and increase in time by 3 for an increase in n by 3. This indicates
that it runs in linear time like the script in the previous exercise. The complexity is O (n).
The script for this exercise is basically the same as in exercise 3, but tailored
for this specific problem, so for an explanation of this I refer to the explanation
in the last exercise.
15
Download