1. Ciaran Cox (1115773)
MA5605: Financial Computing 2 Assignment
0.1 Task 1: Monte-Carlo for PDE’s
The partial differential equation in question is:
find u : R×(0,T] → R such that,
∂u
∂t
=
∂2u
∂x2
for x ∈ R,0 < t ≤ T,
subject to the initial condition,
u(x,0) = u0(x).
The solution of this equation is given by:
u(x,t) =
1
2
√
πt
∞
−∞
uo(y)exp(
−(y−x)2
4t
)dy,
u0(y) =
1
3
sin(3x)+
1
2
sin(2x)+sin(x).
This looks similar to the expectation of the normal distribution integral:
E(V(X)) =
1
σ
√
2π
∞
−∞
V(y)exp(
−(y− µ)2
2σ2
)dy.
Taking σ2 = 2t,µ = x and V(y) = u0(y) we obtain u(x,t) = E(u0(X)). Practically, to solve this
expectation we simulate M normally distributed random numbers with µ = x and σ2 = 2t, then
place each of these random numbers into the function u0(y) and then take the average. Denote this
approximation by w(x,t), as the number of random numbers generated tends to infinity, w(x,t) →
u(x,t).
w(x,t) :=
1
M ∑
m
u0(Ym) with Ym ∼ N(x,2t)
The exact solution can be computed using u(x,t) = ω−1e−ω2
sin(ωx) for u0(x) = sin(ωx)/ω.
Therefore, for our problem, the exact solution is:
u(x,t) =
1
3
e−32t
sin(3x)+
1
2
e−22t
sin(2x)+e−t
sin(x).
Table 1 below shows the approximation of the estimation as M is increased for w(π
2 ,2), the error
u(π
2 ,2)−w(π
2 ,2), an estimated 95% confidence interval, the width of the interval and the computing
1
2. Table 1: Approximation of the Expectation
M Approximation Error Confidence Interval Width Time taken
600000 0.136383 0.001048 (0.134627,0.138139) 0.00351275 0.801095
100 0.197175 -0.061839 (0.0631943,0.331155) 0.26791 0.000152813
1000 0.155804 -0.020468 (0.113136,0.198471) 0.0853345 0.000657904
10000 0.136054 -0.000719 (0.122511,0.149597) 0.0270862 0.0119135
100000 0.133355 0.001980 (0.129053,0.137658) 0.00860457 0.146268
1000000 0.136031 -0.000695 (0.134671,0.137391) 0.00272008 1.33134
10000000 0.135735 -0.000400 (0.135305,0.136165) 0.000860387 12.5592
100000000 0.135301 0.000034 (0.135165,0.135437) 0.000272102 116.279
time. Along with the results for a user input of 600000. C file implemented appendix (.1).
As M is increased the approximation of the estimation converges to the exact solution of the partial
differential equation, for x = π
2 and t = 2. The width of the confidence interval tends to zero
centering around the exact solution. The change in computation time from M = 10,000,000 and
M = 100,000,000 is a big jump different from previous changes. Showing the calculation no longer
fitted into the computer cache, but had to go to the main memory, hence communication time was
increased and therefore total computing time.
2
3. 0.2 Explicit Time Stepping
The partial differential equation in question:
find u : (a,b)×(0,T] → R such that,
∂u
∂t
=
∂2u
∂x2
+ f(x,t) for a < x < b,0 < t ≤ T,
subject to boundary conditions:
u(a,t) = uL(t) = (1−sin(πt))e−2t
,
u(b,t) = uR(t) = (1−sin(πt))e−2t
,
along with the initial condition of:
u(x,0) = u0(x) = cos(2πx).
and
f(x,t) = e−2t
cos(2πx)(−πcos(πt)+(3π2
−2)(1−sin(πt))),
with T,a and b given. A computational grid is set up with step sizes along x being h = (b−a)/N.
Step sizes along time being k = T/M, here N and M being the dissection of x and t respectively.
Points along the grid are defined by, xn = a+nh and tm = mk. The forward Euler approximation of
u(xn,tm) is given by:
um+1
n = αum
n−1 +(1−2α)um
n +αum
n+1 +k f(xn,tm), n = 1,2,...,N −1;m = 0,1,...,M −1
here α = kh−2, with u0
n = u0(xn),um
0 = uL(tm) and um
N = uR(tm) holding. Appendix (.2) is the
C code implementing this approximation for u(x,T) that requests M and N from the user with
T = 2,a = 0,b = 1.
The exact solution to the problem is u(x,t) = (1 − sin(πt))e−2tcos(2πx), tabulated below is the
error for a user input of M = 400000 and N = 300 at T given by:
ε :=
N
∑
n=0
(u(xn,T)−uM
n )2
3
4. Table 2: Error for forward Euler approximation
M N error time
400000 300 0.161787 60.5788
131072 128 0.105629 8.28702
524288 256 0.149447 65.9431
2097152 512 0.211372 564.699
8388608 1024 0.298934 4228.74
Computing time is drastically increased as the iterations increase, due to the capacity of the
cache being breached and a lot of main memory in use. The error is diverging instead of converging.
The individual error’s at each point along the final time vector is plotted below;
Figure 1: Error plot
From the plot, the approximation is exact at the boundary conditions but diverges away in between
the boundaries then back down to the exact solution at the boundaries. The peak of the error is the
same for both cases shown and is distributed equally across the interval as M is increased.
4
5. .1 Monte-Carlo for PDE
/*Ciaran Cox (1115773) 1115773@my.brunel.ac.uk*/
/*relvant libruarys*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<omp.h>
#define PI 3.14159265358979
/*Function Prototype*/
double NormalRandom(double, double);
double UniformRandom(void);
double Function(double);
/*main function*/
int main()
{
/*creating variables and parameters*/
double x,t,s,w,conl,conr,*av,t1,t2,exact;
long int i,m,j;
/*Input from user*/
printf("Enter M: n");
scanf("%ld",&m);
printf("Mtapproximationterrorttconfidencettwidthtttimen");
x=PI/2; t=2;
/*exact solution*/
exact=(1/3)*exp(-pow(3,2)*t)*sin(3*x)
+(1/2)*exp(-pow(2,2)*t)*sin(2*x)+exp(-t)*sin(x);
/*timer start*/
t1=omp_get_wtime();
/*Allocating memory for simulated random numbers*/
if((av=(double*)malloc(m*sizeof(double)))==NULL) exit(1);
x=PI/2; t=2;
/*Approximation of expectation*/
for(i=0;i<m;i++)
{
5