SlideShare a Scribd company logo
1 of 12
Download to read offline
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

What's hot

Newton's Forward/Backward Difference Interpolation
Newton's Forward/Backward  Difference InterpolationNewton's Forward/Backward  Difference Interpolation
Newton's Forward/Backward Difference InterpolationVARUN KUMAR
 
Applied Digital Signal Processing 1st Edition Manolakis Solutions Manual
Applied Digital Signal Processing 1st Edition Manolakis Solutions ManualApplied Digital Signal Processing 1st Edition Manolakis Solutions Manual
Applied Digital Signal Processing 1st Edition Manolakis Solutions Manualtowojixi
 
Newton’s Forward & backward interpolation
Newton’s Forward &  backward interpolation Newton’s Forward &  backward interpolation
Newton’s Forward & backward interpolation Meet Patel
 
adv-2015-16-solution-09
adv-2015-16-solution-09adv-2015-16-solution-09
adv-2015-16-solution-09志远 姚
 
Newton Forward Difference Interpolation Method
Newton Forward Difference Interpolation MethodNewton Forward Difference Interpolation Method
Newton Forward Difference Interpolation MethodAdeel Rasheed
 
Newton divided difference interpolation
Newton divided difference interpolationNewton divided difference interpolation
Newton divided difference interpolationVISHAL DONGA
 
Newton backward interpolation
Newton backward interpolationNewton backward interpolation
Newton backward interpolationMUHAMMADUMAIR647
 
Interpolation In Numerical Methods.
 Interpolation In Numerical Methods. Interpolation In Numerical Methods.
Interpolation In Numerical Methods.Abu Kaisar
 
Applied numerical methods lec9
Applied numerical methods lec9Applied numerical methods lec9
Applied numerical methods lec9Yasser Ahmed
 
Newton-Raphson Method
Newton-Raphson MethodNewton-Raphson Method
Newton-Raphson MethodJigisha Dabhi
 
Computational electromagnetics
Computational electromagneticsComputational electromagnetics
Computational electromagneticsSpringer
 
Gradient Descent
Gradient DescentGradient Descent
Gradient DescentJinho Choi
 
Open GL T0074 56 sm4
Open GL T0074 56 sm4Open GL T0074 56 sm4
Open GL T0074 56 sm4Roziq Bahtiar
 
Lab lecture 1 line_algo
Lab lecture 1 line_algoLab lecture 1 line_algo
Lab lecture 1 line_algosimpleok
 
Solution of non-linear equations
Solution of non-linear equationsSolution of non-linear equations
Solution of non-linear equationsZunAib Ali
 

What's hot (20)

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

Viewers also liked

241 Early management of brachial plexus inries
241 Early management of brachial plexus inries241 Early management of brachial plexus inries
241 Early management of brachial plexus inriesNeurosurgery Vajira
 
Mobile Future 2012 : Living with Mobile Technology, New Yorker Conference 2007
Mobile Future 2012 : Living with Mobile Technology, New Yorker Conference 2007Mobile Future 2012 : Living with Mobile Technology, New Yorker Conference 2007
Mobile Future 2012 : Living with Mobile Technology, New Yorker Conference 2007Younghee Jung
 
WordPress per una scuola (più) digitale
WordPress per una scuola (più) digitaleWordPress per una scuola (più) digitale
WordPress per una scuola (più) digitaleMarco Milesi
 
카드 2016년 3분기 en
카드 2016년 3분기 en카드 2016년 3분기 en
카드 2016년 3분기 enHyundai Finance
 
Module 6 Coping With Failure
Module 6 Coping With Failure Module 6 Coping With Failure
Module 6 Coping With Failure caniceconsulting
 
Fielding: Tom Jones - Power Point Presentation
Fielding: Tom Jones - Power Point PresentationFielding: Tom Jones - Power Point Presentation
Fielding: Tom Jones - Power Point PresentationShineLifeHeart
 
Geraes' Creative Communities Sustainable development through social design
Geraes' Creative Communities Sustainable development through social designGeraes' Creative Communities Sustainable development through social design
Geraes' Creative Communities Sustainable development through social designDESIS_Showcase
 
Ponencias mesa redonda - el empoderamiento de la mujer profesional - celia hil
Ponencias mesa redonda - el empoderamiento de la mujer profesional - celia hilPonencias mesa redonda - el empoderamiento de la mujer profesional - celia hil
Ponencias mesa redonda - el empoderamiento de la mujer profesional - celia hilCèlia Hil
 
Tarefa 7 1.ª Parte
Tarefa 7   1.ª Parte Tarefa 7   1.ª Parte
Tarefa 7 1.ª Parte guestc76ae9
 
Physical and chemical properties and changes
Physical and chemical properties and changesPhysical and chemical properties and changes
Physical and chemical properties and changesJamie Ayers
 
Packaging Design Trends Prediction - A Mashup of Function and Aesthetics
Packaging Design Trends Prediction - A Mashup of Function and AestheticsPackaging Design Trends Prediction - A Mashup of Function and Aesthetics
Packaging Design Trends Prediction - A Mashup of Function and AestheticsLogo Design Guru
 
MICROSURGICAL ANATOMY OF CRANIAL NERVES
MICROSURGICAL ANATOMY OF CRANIAL NERVESMICROSURGICAL ANATOMY OF CRANIAL NERVES
MICROSURGICAL ANATOMY OF CRANIAL NERVESpankaj patel
 

Viewers also liked (20)

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

Similar to Monte-Carlo PDE approximation converges

Computational Method to Solve the Partial Differential Equations (PDEs)
Computational Method to Solve the Partial Differential  Equations (PDEs)Computational Method to Solve the Partial Differential  Equations (PDEs)
Computational Method to Solve the Partial Differential Equations (PDEs)Dr. Khurram Mehboob
 
Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...
Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...
Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...Alexander Litvinenko
 
3. convolution fourier
3. convolution fourier3. convolution fourier
3. convolution fourierskysunilyadav
 
Numerical modeling-of-gas-turbine-engines
Numerical modeling-of-gas-turbine-enginesNumerical modeling-of-gas-turbine-engines
Numerical modeling-of-gas-turbine-enginesCemal Ardil
 
Lecture 2 Introduction to digital image
Lecture 2 Introduction to digital imageLecture 2 Introduction to digital image
Lecture 2 Introduction to digital imageVARUN KUMAR
 
On problem-of-parameters-identification-of-dynamic-object
On problem-of-parameters-identification-of-dynamic-objectOn problem-of-parameters-identification-of-dynamic-object
On problem-of-parameters-identification-of-dynamic-objectCemal Ardil
 
Chapter_09_ParameterEstimation.pptx
Chapter_09_ParameterEstimation.pptxChapter_09_ParameterEstimation.pptx
Chapter_09_ParameterEstimation.pptxVimalMehta19
 
Low rank tensor approximation of probability density and characteristic funct...
Low rank tensor approximation of probability density and characteristic funct...Low rank tensor approximation of probability density and characteristic funct...
Low rank tensor approximation of probability density and characteristic funct...Alexander Litvinenko
 
Fast dct algorithm using winograd’s method
Fast dct algorithm using winograd’s methodFast dct algorithm using winograd’s method
Fast dct algorithm using winograd’s methodIAEME Publication
 
25285 mws gen_int_ppt_trapcontinuous
25285 mws gen_int_ppt_trapcontinuous25285 mws gen_int_ppt_trapcontinuous
25285 mws gen_int_ppt_trapcontinuousJyoti Parange
 
Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...
Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...
Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...Shizuoka Inst. Science and Tech.
 
Theoretical and Practical Bounds on the Initial Value of Skew-Compensated Cl...
Theoretical and Practical Bounds on the Initial Value of  Skew-Compensated Cl...Theoretical and Practical Bounds on the Initial Value of  Skew-Compensated Cl...
Theoretical and Practical Bounds on the Initial Value of Skew-Compensated Cl...Xi'an Jiaotong-Liverpool University
 

Similar to Monte-Carlo PDE approximation converges (20)

DCT
DCTDCT
DCT
 
DCT
DCTDCT
DCT
 
Computational Method to Solve the Partial Differential Equations (PDEs)
Computational Method to Solve the Partial Differential  Equations (PDEs)Computational Method to Solve the Partial Differential  Equations (PDEs)
Computational Method to Solve the Partial Differential Equations (PDEs)
 
Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...
Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...
Computing f-Divergences and Distances of\\ High-Dimensional Probability Densi...
 
3. convolution fourier
3. convolution fourier3. convolution fourier
3. convolution fourier
 
residue
residueresidue
residue
 
Numerical modeling-of-gas-turbine-engines
Numerical modeling-of-gas-turbine-enginesNumerical modeling-of-gas-turbine-engines
Numerical modeling-of-gas-turbine-engines
 
Lecture 2 Introduction to digital image
Lecture 2 Introduction to digital imageLecture 2 Introduction to digital image
Lecture 2 Introduction to digital image
 
Conference ppt
Conference pptConference ppt
Conference ppt
 
Dsp Lab Record
Dsp Lab RecordDsp Lab Record
Dsp Lab Record
 
numerical.ppt
numerical.pptnumerical.ppt
numerical.ppt
 
On problem-of-parameters-identification-of-dynamic-object
On problem-of-parameters-identification-of-dynamic-objectOn problem-of-parameters-identification-of-dynamic-object
On problem-of-parameters-identification-of-dynamic-object
 
Chapter_09_ParameterEstimation.pptx
Chapter_09_ParameterEstimation.pptxChapter_09_ParameterEstimation.pptx
Chapter_09_ParameterEstimation.pptx
 
Low rank tensor approximation of probability density and characteristic funct...
Low rank tensor approximation of probability density and characteristic funct...Low rank tensor approximation of probability density and characteristic funct...
Low rank tensor approximation of probability density and characteristic funct...
 
Fast dct algorithm using winograd’s method
Fast dct algorithm using winograd’s methodFast dct algorithm using winograd’s method
Fast dct algorithm using winograd’s method
 
25285 mws gen_int_ppt_trapcontinuous
25285 mws gen_int_ppt_trapcontinuous25285 mws gen_int_ppt_trapcontinuous
25285 mws gen_int_ppt_trapcontinuous
 
05_AJMS_332_21.pdf
05_AJMS_332_21.pdf05_AJMS_332_21.pdf
05_AJMS_332_21.pdf
 
Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...
Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...
Talk at SciCADE2013 about "Accelerated Multiple Precision ODE solver base on ...
 
Theoretical and Practical Bounds on the Initial Value of Skew-Compensated Cl...
Theoretical and Practical Bounds on the Initial Value of  Skew-Compensated Cl...Theoretical and Practical Bounds on the Initial Value of  Skew-Compensated Cl...
Theoretical and Practical Bounds on the Initial Value of Skew-Compensated Cl...
 
KAUST_talk_short.pdf
KAUST_talk_short.pdfKAUST_talk_short.pdf
KAUST_talk_short.pdf
 

Monte-Carlo PDE approximation converges

  • 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