SlideShare a Scribd company logo
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
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
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
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
.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
av[i]=Function(NormalRandom(x,2*t));
w+=av[i];
}
w=w/m;
/*standard deviation*/
for(i=0;i<m;i++)
{
s+=pow((av[i]-w),2);
}
s=sqrt(s/m);
/*confidence interval*/
conl=w-(1.96*s)/(sqrt(m));
conr=w+(1.96*s)/(sqrt(m));
/*timer stop*/
t2=omp_get_wtime();
/*printing table of results*/
printf("%ldt%lft%lft(%lg,%lg)t%lgt%lgn"
,m,w,exact-w,conl,conr,fabs(conl-conr),t2-t1);
printf("nn");
/*freeing memory*/
free(av);
/*computing table*/
for(j=100;j<=100000000;j=j*10)
{
t1=omp_get_wtime();
if((av=(double*)malloc(j*sizeof(double)))==NULL) exit(1);
/*approximation of expectation*/
for(i=0;i<j;i++)
{
av[i]=Function(NormalRandom(x,2*t));
w+=av[i];
}
w=w/j;
/*standard deviation*/
6
for(i=0;i<j;i++)
{
s+=pow((av[i]-w),2);
}
s=sqrt(s/j);
/*confidence interal*/
conl=w-(1.96*s)/(sqrt(j));
conr=w+(1.96*s)/(sqrt(j));
t2=omp_get_wtime();
printf("%ld t%lft%lft(%lg,%lg)t%lgt%lgn"
,j,w,exact-w,conl,conr,fabs(conl-conr),t2-t1);
/*freeing memory*/
free(av);
}
}
/*Uniformally distriubuted random variable*/
double UniformRandom(void)
{
return (double)rand()/RAND_MAX;
}
/*Converting Unform random variable into
* normal random variable with required
* mean and variance*/
double NormalRandom(double MEAN2, double SD2)
{
double U, X=UniformRandom(), Y=UniformRandom(),out;
while(X==0) X=UniformRandom();
U=sqrt(-2*log(X))*sin(2*PI*Y);
out=sqrt(SD2)*U+MEAN2;
return out;
}
/*Function for the required expectation*/
double Function(double IN)
{
7
double out;
out=(1/3)*sin(3*IN)+(1/2)*sin(2*IN)+sin(IN);
return out;
}
.2 Explicit Time Stepping
/*Ciaran Cox (1115773) 1115773@my.brunel.ac.uk*/
/*relevant libruarys*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<omp.h>
#define PI 3.14159265358979323846264338327950288
/*Function prototypes*/
double Function(double, double);
double leftbound(double);
double rightbound(double);
double initial(double);
double check(double, double);
/*main function*/
int main()
{
/*Defining variables and parameters*/
long int M,N,i,j;
double a,b,h,T,*xn,*tm,k,alpha,*Unew,*Uold,error,sum,t1,t2;
/*Input from user for N and M*/
printf("Enter N:n"); scanf("%ld",&N);
printf("Enter M:n"); scanf("%ld",&M);
printf("MtNterrortttimen");
t1=omp_get_wtime();
/*Dynamically allocating memory*/
if((xn=(double*)malloc((N+1)*sizeof(double)))==NULL) exit(1);
if((tm=(double*)malloc((M+1)*sizeof(double)))==NULL) exit(1);
8
if((Unew=(double*)malloc((N+1)*sizeof(double)))==NULL) exit(1);
if((Uold=(double*)malloc((N+1)*sizeof(double)))==NULL) exit(1);
a=0; b=1; T=2;
h=(b-a)/N; k=T/M;
alpha=k*(1/pow(h,2));
xn[0]=a;xn[N]=b;
tm[0]=0;
/*initial boundary condition using function calls*/
Uold[0]=leftbound(tm[0]);
Uold[N]=rightbound(tm[0]);
/*intial condition for middle of vector*/
for(i=1;i<N;i++)
{
/*x computational grid*/
xn[i]=a+i*h;
Uold[i]=initial(xn[i]);
}
/*Iterating up through time*/
for(j=0;j<M;j++)
{
/*next time value*/
tm[j+1]=(j+1)*k;
/*boundary conditions*/
Unew[0]=leftbound(tm[j+1]);
Unew[N]=rightbound(tm[j+1]);
/*Forward Euler approximation*/
for(i=1;i<N;i++)
{
Unew[i]=alpha*Uold[i-1]+(1-2*alpha)*Uold[i]
+alpha*Uold[i+1]+k*Function(xn[i],tm[j]);
}
/*replacing old vector with new vector*/
for(i=0;i<=N;i++)
{
9
Uold[i]=Unew[i];
}
}
sum=0;
/*Computing error*/
for(i=0;i<=N;i++)
sum+=pow(check(xn[i],T)-Uold[i],2);
error=sqrt(sum);
/*freeing memory*/
free(xn); free(tm); free(Unew); free(Uold);
t2=omp_get_wtime();
/*prints results for required input from user*/
printf("%dt%dt%1gt%lgnnn",M,N,error,t2-t1);
/*repeats above procedure for tabulated results*/
for(M=131072,N=128;M<=8388608,N<=1024;M=M*4,N=N*2)
{
t1=omp_get_wtime();
if((xn=(double*)malloc((N+1)*sizeof(double)))==NULL) exit(1);
if((tm=(double*)malloc((M+1)*sizeof(double)))==NULL) exit(1);
if((Unew=(double*)malloc((N+1)*sizeof(double)))==NULL) exit(1);
if((Uold=(double*)malloc((N+1)*sizeof(double)))==NULL) exit(1);
a=0; b=1; T=2;
h=(b-a)/N; k=T/M;
alpha=k*(1/pow(h,2));
xn[0]=a;xn[N]=b;
tm[0]=0;
Uold[0]=leftbound(tm[0]);
Uold[N]=rightbound(tm[0]);
for(i=1;i<N;i++)
{
xn[i]=a+i*h;
Uold[i]=initial(xn[i]);
}
for(j=0;j<M;j++)
10
{
tm[j+1]=(j+1)*k;
Unew[0]=leftbound(tm[j+1]);
Unew[N]=rightbound(tm[j+1]);
for(i=1;i<N;i++)
{
Unew[i]=alpha*Uold[i-1]+(1-2*alpha)*Uold[i]
+alpha*Uold[i+1]+k*Function(xn[i],tm[j]);
}
for(i=0;i<=N;i++)
{
Uold[i]=Unew[i];
}
}
sum=0;
for(i=0;i<=N;i++)
{
sum+=pow(check(xn[i],T)-Uold[i],2);
}
error=sqrt(sum);
free(xn); free(tm); free(Unew); free(Uold);
t2=omp_get_wtime();
printf("%dt%dt%1gt%lgn",M,N,error,t2-t1);
}
}
/*Function for the forcing term*/
double Function(double x, double t)
{
double out;
out=exp(-2*t)*cos(2*PI*x)*(-PI*cos(PI*t)
+(3*pow(PI,2)-2)*(1-sin(PI*t)));
return out;
}
/*Function for the left boundary condition*/
11
double leftbound(double t)
{
double out;
out=(1-sin(PI*t))*exp(-2*t);
return out;
}
/*Function for the right boundary condition*/
double rightbound(double t)
{
double out;
out=(1-sin(PI*t))*exp(-2*t);
return out;
}
/*Function for the initial condition*/
double initial(double x)
{
double out;
out=cos(2*PI*x);
return out;
}
/*Function for the exact solution*/
double check(double x, double t)
{
double out;
out=(1-sin(PI*t))*exp(-2*t)*cos(2*PI*x);
return out;
}
12

More Related Content

PPTX
Newton's forward & backward interpolation
PPTX
Newton’s Divided Difference Formula
PPTX
Newton's forward difference
PDF
Two algorithms to accelerate training of back-propagation neural networks
PPTX
numericai matmatic matlab uygulamalar ali abdullah
PDF
Maxima & Minima of Functions - Differential Calculus by Arun Umrao
DOCX
Internal assessment
Newton's forward & backward interpolation
Newton’s Divided Difference Formula
Newton's forward difference
Two algorithms to accelerate training of back-propagation neural networks
numericai matmatic matlab uygulamalar ali abdullah
Maxima & Minima of Functions - Differential Calculus by Arun Umrao
Internal assessment

What's hot (20)

PDF
Newton's Forward/Backward Difference Interpolation
PDF
Applied Digital Signal Processing 1st Edition Manolakis Solutions Manual
PPTX
Newton’s Forward & backward interpolation
PDF
adv-2015-16-solution-09
PPTX
Newton Forward Difference Interpolation Method
PPT
Newton divided difference interpolation
PPTX
Newton backward interpolation
PDF
Bounded var
PPTX
Interpolation In Numerical Methods.
PDF
Applied numerical methods lec9
PPT
Newton-Raphson Method
PDF
Computational electromagnetics
DOCX
Metodo de kutta
PDF
Composed short m sequences
PPTX
Numerical method (curve fitting)
PDF
Gradient Descent
PDF
MUMS Undergraduate Workshop - Quantifying Uncertainty in Hazard Forecasting -...
PDF
Open GL T0074 56 sm4
PPT
Lab lecture 1 line_algo
PDF
Solution of non-linear equations
Newton's Forward/Backward Difference Interpolation
Applied Digital Signal Processing 1st Edition Manolakis Solutions Manual
Newton’s Forward & backward interpolation
adv-2015-16-solution-09
Newton Forward Difference Interpolation Method
Newton divided difference interpolation
Newton backward interpolation
Bounded var
Interpolation In Numerical Methods.
Applied numerical methods lec9
Newton-Raphson Method
Computational electromagnetics
Metodo de kutta
Composed short m sequences
Numerical method (curve fitting)
Gradient Descent
MUMS Undergraduate Workshop - Quantifying Uncertainty in Hazard Forecasting -...
Open GL T0074 56 sm4
Lab lecture 1 line_algo
Solution of non-linear equations
Ad

Viewers also liked (20)

PPT
241 Early management of brachial plexus inries
PPTX
Mobile Future 2012 : Living with Mobile Technology, New Yorker Conference 2007
PDF
Pharmacology of GABA
PPTX
WordPress per una scuola (più) digitale
PPTX
PowerShell Best Practices and Resources
PPT
Redes neuronales
PDF
카드 2016년 3분기 en
PDF
Module 6 Coping With Failure
PPTX
Fielding: Tom Jones - Power Point Presentation
PDF
Design harvests
PDF
Geraes' Creative Communities Sustainable development through social design
PPT
Tue projects-humus
PPTX
PDF
Ponencias mesa redonda - el empoderamiento de la mujer profesional - celia hil
DOC
Tarefa 7 1.ª Parte
PPT
Physical and chemical properties and changes
PPT
Chick shampoo case study
PDF
Packaging Design Trends Prediction - A Mashup of Function and Aesthetics
PPTX
MICROSURGICAL ANATOMY OF CRANIAL NERVES
PDF
Presentación freelance 360 marketing
241 Early management of brachial plexus inries
Mobile Future 2012 : Living with Mobile Technology, New Yorker Conference 2007
Pharmacology of GABA
WordPress per una scuola (più) digitale
PowerShell Best Practices and Resources
Redes neuronales
카드 2016년 3분기 en
Module 6 Coping With Failure
Fielding: Tom Jones - Power Point Presentation
Design harvests
Geraes' Creative Communities Sustainable development through social design
Tue projects-humus
Ponencias mesa redonda - el empoderamiento de la mujer profesional - celia hil
Tarefa 7 1.ª Parte
Physical and chemical properties and changes
Chick shampoo case study
Packaging Design Trends Prediction - A Mashup of Function and Aesthetics
MICROSURGICAL ANATOMY OF CRANIAL NERVES
Presentación freelance 360 marketing
Ad

Similar to assignment_2 (20)

PDF
Errors in the Discretized Solution of a Differential Equation
PDF
Applications Of MATLAB Ordinary Differential Equations (ODE
PPT
Ch08 1
PDF
Modeling and quantification of uncertainties in numerical aerodynamics
DOCX
MATLAB sessions Laboratory 3MAT 275 Laboratory 3Numeric.docx
PPT
Chap8
PDF
NPDE-TCA
PDF
Numerical_PDE_Paper
PDF
QMC: Transition Workshop - Probabilistic Integrators for Deterministic Differ...
PDF
Silent error detection in numerical time stepping schemes (SIAM PP 2014)
PDF
Diff eq
PDF
FITTED OPERATOR FINITE DIFFERENCE METHOD FOR SINGULARLY PERTURBED PARABOLIC C...
PDF
FITTED OPERATOR FINITE DIFFERENCE METHOD FOR SINGULARLY PERTURBED PARABOLIC C...
PDF
Fitted Operator Finite Difference Method for Singularly Perturbed Parabolic C...
PPT
Unit One - Solved problems on error analysis .ppt
PDF
Heat problems
PDF
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
PDF
partial defferential_slides_numerical.pdf
PPT
Top school in India
PDF
Scilab is not naive
Errors in the Discretized Solution of a Differential Equation
Applications Of MATLAB Ordinary Differential Equations (ODE
Ch08 1
Modeling and quantification of uncertainties in numerical aerodynamics
MATLAB sessions Laboratory 3MAT 275 Laboratory 3Numeric.docx
Chap8
NPDE-TCA
Numerical_PDE_Paper
QMC: Transition Workshop - Probabilistic Integrators for Deterministic Differ...
Silent error detection in numerical time stepping schemes (SIAM PP 2014)
Diff eq
FITTED OPERATOR FINITE DIFFERENCE METHOD FOR SINGULARLY PERTURBED PARABOLIC C...
FITTED OPERATOR FINITE DIFFERENCE METHOD FOR SINGULARLY PERTURBED PARABOLIC C...
Fitted Operator Finite Difference Method for Singularly Perturbed Parabolic C...
Unit One - Solved problems on error analysis .ppt
Heat problems
Program on Quasi-Monte Carlo and High-Dimensional Sampling Methods for Applie...
partial defferential_slides_numerical.pdf
Top school in India
Scilab is not naive

assignment_2

  • 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
  • 6. av[i]=Function(NormalRandom(x,2*t)); w+=av[i]; } w=w/m; /*standard deviation*/ for(i=0;i<m;i++) { s+=pow((av[i]-w),2); } s=sqrt(s/m); /*confidence interval*/ conl=w-(1.96*s)/(sqrt(m)); conr=w+(1.96*s)/(sqrt(m)); /*timer stop*/ t2=omp_get_wtime(); /*printing table of results*/ printf("%ldt%lft%lft(%lg,%lg)t%lgt%lgn" ,m,w,exact-w,conl,conr,fabs(conl-conr),t2-t1); printf("nn"); /*freeing memory*/ free(av); /*computing table*/ for(j=100;j<=100000000;j=j*10) { t1=omp_get_wtime(); if((av=(double*)malloc(j*sizeof(double)))==NULL) exit(1); /*approximation of expectation*/ for(i=0;i<j;i++) { av[i]=Function(NormalRandom(x,2*t)); w+=av[i]; } w=w/j; /*standard deviation*/ 6
  • 7. for(i=0;i<j;i++) { s+=pow((av[i]-w),2); } s=sqrt(s/j); /*confidence interal*/ conl=w-(1.96*s)/(sqrt(j)); conr=w+(1.96*s)/(sqrt(j)); t2=omp_get_wtime(); printf("%ld t%lft%lft(%lg,%lg)t%lgt%lgn" ,j,w,exact-w,conl,conr,fabs(conl-conr),t2-t1); /*freeing memory*/ free(av); } } /*Uniformally distriubuted random variable*/ double UniformRandom(void) { return (double)rand()/RAND_MAX; } /*Converting Unform random variable into * normal random variable with required * mean and variance*/ double NormalRandom(double MEAN2, double SD2) { double U, X=UniformRandom(), Y=UniformRandom(),out; while(X==0) X=UniformRandom(); U=sqrt(-2*log(X))*sin(2*PI*Y); out=sqrt(SD2)*U+MEAN2; return out; } /*Function for the required expectation*/ double Function(double IN) { 7
  • 8. double out; out=(1/3)*sin(3*IN)+(1/2)*sin(2*IN)+sin(IN); return out; } .2 Explicit Time Stepping /*Ciaran Cox (1115773) 1115773@my.brunel.ac.uk*/ /*relevant libruarys*/ #include<stdio.h> #include<stdlib.h> #include<math.h> #include<omp.h> #define PI 3.14159265358979323846264338327950288 /*Function prototypes*/ double Function(double, double); double leftbound(double); double rightbound(double); double initial(double); double check(double, double); /*main function*/ int main() { /*Defining variables and parameters*/ long int M,N,i,j; double a,b,h,T,*xn,*tm,k,alpha,*Unew,*Uold,error,sum,t1,t2; /*Input from user for N and M*/ printf("Enter N:n"); scanf("%ld",&N); printf("Enter M:n"); scanf("%ld",&M); printf("MtNterrortttimen"); t1=omp_get_wtime(); /*Dynamically allocating memory*/ if((xn=(double*)malloc((N+1)*sizeof(double)))==NULL) exit(1); if((tm=(double*)malloc((M+1)*sizeof(double)))==NULL) exit(1); 8
  • 9. if((Unew=(double*)malloc((N+1)*sizeof(double)))==NULL) exit(1); if((Uold=(double*)malloc((N+1)*sizeof(double)))==NULL) exit(1); a=0; b=1; T=2; h=(b-a)/N; k=T/M; alpha=k*(1/pow(h,2)); xn[0]=a;xn[N]=b; tm[0]=0; /*initial boundary condition using function calls*/ Uold[0]=leftbound(tm[0]); Uold[N]=rightbound(tm[0]); /*intial condition for middle of vector*/ for(i=1;i<N;i++) { /*x computational grid*/ xn[i]=a+i*h; Uold[i]=initial(xn[i]); } /*Iterating up through time*/ for(j=0;j<M;j++) { /*next time value*/ tm[j+1]=(j+1)*k; /*boundary conditions*/ Unew[0]=leftbound(tm[j+1]); Unew[N]=rightbound(tm[j+1]); /*Forward Euler approximation*/ for(i=1;i<N;i++) { Unew[i]=alpha*Uold[i-1]+(1-2*alpha)*Uold[i] +alpha*Uold[i+1]+k*Function(xn[i],tm[j]); } /*replacing old vector with new vector*/ for(i=0;i<=N;i++) { 9
  • 10. Uold[i]=Unew[i]; } } sum=0; /*Computing error*/ for(i=0;i<=N;i++) sum+=pow(check(xn[i],T)-Uold[i],2); error=sqrt(sum); /*freeing memory*/ free(xn); free(tm); free(Unew); free(Uold); t2=omp_get_wtime(); /*prints results for required input from user*/ printf("%dt%dt%1gt%lgnnn",M,N,error,t2-t1); /*repeats above procedure for tabulated results*/ for(M=131072,N=128;M<=8388608,N<=1024;M=M*4,N=N*2) { t1=omp_get_wtime(); if((xn=(double*)malloc((N+1)*sizeof(double)))==NULL) exit(1); if((tm=(double*)malloc((M+1)*sizeof(double)))==NULL) exit(1); if((Unew=(double*)malloc((N+1)*sizeof(double)))==NULL) exit(1); if((Uold=(double*)malloc((N+1)*sizeof(double)))==NULL) exit(1); a=0; b=1; T=2; h=(b-a)/N; k=T/M; alpha=k*(1/pow(h,2)); xn[0]=a;xn[N]=b; tm[0]=0; Uold[0]=leftbound(tm[0]); Uold[N]=rightbound(tm[0]); for(i=1;i<N;i++) { xn[i]=a+i*h; Uold[i]=initial(xn[i]); } for(j=0;j<M;j++) 10
  • 11. { tm[j+1]=(j+1)*k; Unew[0]=leftbound(tm[j+1]); Unew[N]=rightbound(tm[j+1]); for(i=1;i<N;i++) { Unew[i]=alpha*Uold[i-1]+(1-2*alpha)*Uold[i] +alpha*Uold[i+1]+k*Function(xn[i],tm[j]); } for(i=0;i<=N;i++) { Uold[i]=Unew[i]; } } sum=0; for(i=0;i<=N;i++) { sum+=pow(check(xn[i],T)-Uold[i],2); } error=sqrt(sum); free(xn); free(tm); free(Unew); free(Uold); t2=omp_get_wtime(); printf("%dt%dt%1gt%lgn",M,N,error,t2-t1); } } /*Function for the forcing term*/ double Function(double x, double t) { double out; out=exp(-2*t)*cos(2*PI*x)*(-PI*cos(PI*t) +(3*pow(PI,2)-2)*(1-sin(PI*t))); return out; } /*Function for the left boundary condition*/ 11
  • 12. double leftbound(double t) { double out; out=(1-sin(PI*t))*exp(-2*t); return out; } /*Function for the right boundary condition*/ double rightbound(double t) { double out; out=(1-sin(PI*t))*exp(-2*t); return out; } /*Function for the initial condition*/ double initial(double x) { double out; out=cos(2*PI*x); return out; } /*Function for the exact solution*/ double check(double x, double t) { double out; out=(1-sin(PI*t))*exp(-2*t)*cos(2*PI*x); return out; } 12