AmericanOption
AmericanOption
S.C.Benbow
The problem of solving the Black-Scholes equation for the valuation of Amer-
ican options is tackled using a Crank-Nicolson finite difference method for-
mulated in a Lagrangian frame.
I would like to thank Prof. M.J.Baines for his time and invaluable help with
this thesis. I have thoroughly enjoyed working with him, this dissertation has
given me an insight into the true nature of research and provided me with
motivation to continue with my studies. I would also like to acknowledge my
colleagues on the Msc course and the other members of the academic staff in
the Department of mathematics at the University of Reading.
This year has been both enjoyable and challenging. I would like to thank my
family, especially me wife, for all of their support and hard work in the last
twelve months in allowing me to study at Reading and take full advantage
of this wonderful opportunity.
1
Contents
1 Introduction 4
1.1 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.2 Aims . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Background 8
2.1 Model for Asset Prices . . . . . . . . . . . . . . . . . . . . . . 8
2.2 Black-Scholes Model . . . . . . . . . . . . . . . . . . . . . . . 10
2.3 Black-Scholes For European Option . . . . . . . . . . . . . . . 13
2.4 Modification to the model . . . . . . . . . . . . . . . . . . . . 14
2.5 American Option . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.6 American Call with Dividends . . . . . . . . . . . . . . . . . . 20
2.7 General Analysis of Call with Dividends . . . . . . . . . . . . 21
3 Transformation 25
3.1 American Options PDE . . . . . . . . . . . . . . . . . . . . . 25
3.2 Transformation to Diffusion Equation . . . . . . . . . . . . . . 26
4 Numerical Methods 28
4.1 Finite Difference Based Front Tracking Method . . . . . . . . 28
4.2 Invertbility . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2
4.3 Stability Analysis . . . . . . . . . . . . . . . . . . . . . . . . . 36
4.4 Local Analysis of the Free Boundary . . . . . . . . . . . . . . 38
4.5 Derivative Boundary Conditions . . . . . . . . . . . . . . . . 40
4.6 Description of Algorithm 1 . . . . . . . . . . . . . . . . . . . . 41
4.7 Algorithm 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.8 Invertbility-method two . . . . . . . . . . . . . . . . . . . . . 46
4.9 Description of Algorithm 2 . . . . . . . . . . . . . . . . . . . . 46
6 Results 55
6.1 Finite Differences . . . . . . . . . . . . . . . . . . . . . . . . . 55
6.2 Finite Element Algoritm . . . . . . . . . . . . . . . . . . . . . 63
7 conclusion 65
8 Reference 67
.1 Program 1 - pt1.f90 . . . . . . . . . . . . . . . . . . . . . . . . 69
.2 Program 2 - pt2.f90 . . . . . . . . . . . . . . . . . . . . . . . . 84
.3 Program 3 - pt3.f90 . . . . . . . . . . . . . . . . . . . . . . . . 95
3
Chapter 1
Introduction
The simplest financial option, a European call option, is a contract with the
following conditions
• At a prescribed time in the future, known as the expiry date the holder
of the option may do the following;
For the holder of the option, the contract is a right but not an obligation.
The other party to the contract, the individual who is known as the writer
does have a potential obligation, he must sell the asset if the holder chooses
to buy it.
The option confers to its holder a right without an obligation, and there-
fore has intrinsic value.
4
The main concerns in the valuation of options are:
• How much would one pay for this right, i.e. what is the value of an
option?
• How can the writer minimize the risk associated with his obligation?
Options have become extremely popular recently, primarily because they are
attractive to investors, both for speculation and for hedging, and because
there is now a systematic way to determine how much they are worth.
We let E denote the exercise price, i.e the cost of purchasing the option,
and S(T ) denote the underlying asset price at the expiry date. At expiry if
S(T ) > E then the holder of the call option may buy that asset for E and
then immediately sell it in the market for S(T ), gaining an amount S(T )−E.
Conversely, if E ≥ S(T ) then the holder gains nothing. The value of the call
option may therefore be expressed as
C = max(S(T ) − E, 0) (1.1.1)
Plotting S(T ) on the x-axis and C on the y-axis gives a payoff diagram as
shown in 1.1.
5
Figure 1.1: Payoff Diagram for a European Call
exercise, potentially it has a higher value. This report will focus on the valu-
ation of American options, which are mathematically more interesting than
their European counterparts since they can be interpreted as free boundary
problems.
The American option valuation problem can be shown to be uniquely spec-
ified by a series of constraints, which are similar to those of an ’obstacle’
problem. Since we do not know the location of the free boundary Sf (t) a
priori we are lacking one piece of information compared with the European
options. Not only must a value be assigned to the option but, we must also
determine when it is best to exercise the option.
1.2 Aims
The aims of this dissertation are to produce an accurate method for the val-
uation of American call options. The optimal exercise boundary is modeled
6
by a moving boundary. The time dependent boundary point is physically in-
terpreted as the division between two regions, one where we should hold the
option, and the other where we should exercise. This point is known as the
optimal exercise price. We seek a method which will determine accurately
the location of this free boundary, and furthermore provide a corresponding
valuation for the option at discrete time steps up until the expiry date.
The conventional approach is to transform the Black-Scholes equation into a
dimensionless parabolic equation and then discretise the problem using nu-
merical methods to find a solution1 . We propose a new method of solution,
in which the equations are discretised on a moving grid. Since the optimal
exercise boundary is time dependent, and found to increase in time for the
case of a call, we also propose a new technique for expanding the domain.
Finally, we intend to solve same the problem using a finite element approach,
and compare the accuracy between the two schemes.
1
see K.N PANAZOPOULOS ET AL
7
Chapter 2
Background
• The past history is fully reflected in the present price, which does not
hold any further information
With these two assumptions, the asset price is said to follow a Markov pro-
cess
8
Definition 1. A Markov process is a particular type of stochastic process
where only the present value of a variable is relevant in predicting the future.
The past history of the variable, and the way in which the present has emerged
from the past are irrelevant
µ is a measure of the average rate of growth of the asset price, also known
as drift.
The second contribution to dS/S models the random change in the asset
price in response to external effects. It is represented by a random sample
drawn from a normal distribution with mean zero. It adds contribution
σdX (2.1.2)
9
prices, is known as a Wiener process, it has the following properties:
The key question concerning the valuation of Options is: what is an option
worth at time t=0? The problem is to systematically determine a fair value
for an option, at the time at which the contract is entered into. Before we
proceed with any analysis, there are several assumptions that we must first
make
• The risk-free interest rate r and the asset volatility σ are known func-
tions of time over the life of the option.
10
• There are no associated transaction costs
• The underlying asset pays no dividends during the life of the option.
We now look for a function V (S, t) that gives the option value for any asset
price S ≥ 0 and at any time 0 ≤ t ≤ T . In this setting, V (S0 , 0) is the
required time-zero option value. We further assume that such a function
exists and is smooth in both variables. Ito’s Lemma provides us with a
derivative chain rule for stochastic functions; i.e. if f = f (W, t) where W is
some stochastic function.
df 1 d2 f
df = (σSdX + µSdt) + σ 2 S 2 2 dt (2.2.1)
dS 2 dS
1 2 2 ∂ 2V
µ ¶
dV ∂V ∂V
dV = σS dX + µS + σ S + dt (2.2.2)
dS ∂S 2 ∂S 2 ∂t
Π = V − ∆S (2.2.3)
dΠ = dV − ∆dS (2.2.4)
11
Combining 2.1.3, 2.2.2 and 2.2.3 we find that Π follows the random walk
1 2 2 ∂2
µ ¶ µ ¶
∂V ∂V ∂V
dΠ = σS − ∆ dX + µS + σ S + − µ∆S dt (2.2.5)
∂S ∂S 2 ∂S 2 ∂t
∂V
We can eliminate the random component by choosing ∆ = ∂S
. This results
in a portfolio whose increment is wholly deterministic
∂2V
µ ¶
∂V 1
dΠ = + σ2S 2 2 dt (2.2.6)
∂t 2 ∂S
The return on on an amount Π invested in a risk less asset would see a growth
of rΠdt in a time dt. If the right hand side of 2.2.6 were greater than this
amount, an arbitrager could make a guaranteed risk less profit by borrowing
an amount Π to invest in the portfolio. Conversely, if the right-hand side of
2.2.6 were less than rΠdt then the arbitrager would make a risk less, no cost,
instantaneous profit.
Thus we have
∂2V
µ ¶
∂V 1
rΠdt = + σ2S 2 2 dt (2.2.7)
∂t 2 ∂S
∂V
Substituting 2.2.3 and ∆ = ∂S
into 2.2.7 and dividing by dt we arrive at the
Black-Scholes partial differential equation
∂V 1 2 2 ∂2V ∂V
+ σ S + rS − rV = 0 (2.2.8)
∂t 2 ∂S 2 ∂S
Any derivative security whose price depends only on the current value of S
and on t, which is paid for up-front, must satisfy the Black-Scholes equation.
12
2.3 Black-Scholes For European Option
Since the equation is backward in time we must also impose a final condition
such as
C(0, t) = 0 (2.3.3)
As the asset price increases, it will become more likely that the option will
be exercised and the actual magnitude of the exercise price becomes less
13
important. We can therefore write that as S → ∞ the value of the option
becomes that of the asset
C(S, t) S as S → ∞ (2.3.4)
For the European options, without the possibility of early exercise 2.2.8 can
be solved exactly to give the Black-Scholes value of the call option.
Assuming that the interest rate and the volatility are constant, the explicit
solution for the European call is
Figure 2.3 below shows a European call value C(S, t) as a function of S for
several values of time to expiry, with r = 0.1 and σ = 0.2
14
Figure 2.1: American Call Value Prior to expiry
effect on the options price when dividend payment is incorporated into the
model.
When assets pay out dividends, the price of an option on a underlying asset is
affected by the payments. A modification must be made to the black-scholes
equation.
Suppose that in time dt the underlying asset pays out a dividend D0 Sdt
where D0 is a constant. The payment is independent of time, but dependent
15
on the stock price S. The Dividend Yield is defined as the proportion of the
asset price that is paid out per unit time in this way.
Arbitrage considerations show that in each time-step dt, the asset price must
fall by the amount of the dividend payment, in addition to the usual fluctu-
ations. The random walk of the asset price 2.1.3 is modified to become
Where
log(S/E) + (r − D0 + 12 σ 2 )(T − t)
d10 = √ (2.4.7)
σ T −t
√
d20 = d1 − σ T − t (2.4.8)
16
Figure 2.2: European Option Values with (lower) and without (lower) divi-
dends
17
2.5 American Option
American Options have the important additional feature that early exercise
is permitted at any time during the life of the option.
Definition 2. An American Call Option gives its holder the right, but not
the obligation, to purchase from the writer a prescribed asset for a prescribed
price at any time between the start date and a prescribed expiry date in the
future.
The formulae in section 2.3 and 2.4 do not necessarily agree with the
value of American options. The ability to exercise the option at any time
extends to the owner additional rights, and thus the American option has
potentially a higher value.
This opportunity would not last long before the value of the option was
pushed up by the demand of the arbitragers. We must therefore conclude
that when early exercise is permitted we must impose the constraint
18
In the case of American options there are some values of S for which it
is optimal from the holders point of view to exercise the American option. If
this were not the case the option would have the same value as the European
option, the Black-Scholes equation would hold for all S.
We denote this value, which varies with time, by Sf (t), and refer to it as
the optimal exercise price.
As we have already observed, since we do not know Sf a priori unlike the cor-
responding European problem, we do not know where to apply the boundary
conditions, and for this reason, the problem is called a free boundary problem.
• the option value must be greater than or equal to the payoff function
19
• the option value must be a continuous function of S
Also, since the option may be exercised at any time, we have that
20
2.7 General Analysis of Call with Dividends
We now also subtract off the payoff S − E for the call value C(S, t).
τ
S = Eex , t = T − 1 2 , C(S, t) = S − E + Ec(x, τ ) (2.7.1)
2
σ
the result is
∂c ∂ 2c ∂c
= + (k´− 1) − kc + f (x) (2.7.2)
∂τ ∂x2 ∂x
for −∞ < x, ∞ and τ > 0. The function c(x, 0),the initial profile, is given
by
c(x, 0) = max(1 − ex , 0) (2.7.3)
A graph of c(x, 0) is shown in 2.7. The two parameters k and ḱ are given by
r (r − D0 )
k= 1 2, ḱ = 1 2 (2.7.4)
2
σ 2
σ
Assuming that the free boundary does exist, x = xf (t), at this boundary we
have
∂c
c(xf (τ ), τ ) = (xf (τ ), τ ) = 0 (2.7.6)
∂x
1
We follow the same analysis as Wilmott,Mathematics of Financial Derivatives,Chapter
7.7
21
Figure 2.3: c(x,0)
We now have the constraint that c ≥ max(1 − ex , 0). The behavior of f (x),
the consumption/replenishment term, is critical to the behavior of the free
boundary. A graph of f (x) is shown in figure 2.7. f (x) is positive when
x < x0 where
x0 = log(k/(k − ḱ)) = log(r/D0 ) > 0 (2.7.7)
∂c(x, 0) ∂ 2 c(x, 0)
c(x, 0) = = =0 (2.7.8)
∂x ∂x2
∂c
= f (x) (2.7.9)
∂τ
For 0 < x < x0 , f (x) > 0 and thus c is positive. If x > x0 then f (x) < 0
and c will be negative. We have the constraint that c > 0 for x > 0 thus the
latter does not satisfy this constraint.
22
Figure 2.4: f(x)
If we hold the option in x > x0 the option falls below its intrinsic value
and the constraint is broken.
We must therefore take xf (0) = x0 since this is the only point consistent
with c(xf (0), 0) = 0
Figures 2.7 and 2.7 show the values of c(x, τ ) in the dimensionless diffusion
setting, and the original C(S, t) at times prior to expiry.
23
Figure 2.5: Local Solution c(x, τ )
24
Chapter 3
Transformation
25
˜ × [0, T ). The boundary conditions are given below
(0, B(t))
Vs (B̃(t), t) = 1 (3.1.6)
2r 2(r − D)
k1 = k2 = (3.2.1)
σ2 σ2
1 2
τ = σ (T − t) (3.2.2)
2
x = log(S/K) + (K2 − 1)τ (3.2.3)
˜ )/K) + (k2 − 1)τ
B(τ ) = log(B(τ (3.2.4)
ek1 τ
u(x, τ ) = (V (S, t) − S + K) (3.2.5)
K
26
Where
g(x, τ ) = ek1 τ ((k2 − k1 )ex−(k2 −1)τ + k1 ) (3.2.7)
This is the principle equation which we will attempt to solve using nu-
merical methods in the following section. The domain of equation 3.2.6
is (−∞, B(0)) × (0, 12 σ 2 T ). The boundary conditions for the American call
become
u(B(τ ), τ ) = 0 (3.2.11)
ux (B(τ ), τ ) = 0 (3.2.12)
27
Chapter 4
Numerical Methods
Method
In the PDE 3.2.6 u = u(x, t) is defined in a fixed frame of reference with co-
ordinate x and time t. The differential operator L1 involves space derivatives
only.
28
the moving coordinates x at time t
x = x̂(a, t) (4.1.1)
We have
u(x, t) = u(x̂(a, t), t) = û(a, t) (4.1.2)
∂ û ∂ x̂ ∂u ∂u
= · + (4.1.3)
∂t ∂t ∂ x̂ ∂t
Si = iδS, i = 0, · · · , N (4.1.5)
B(τ ) − x −
δS = (4.1.6)
N
τj = jδτ, j = 0, · · · , L (4.1.7)
1 2
δτ = σ T /L (4.1.8)
2
29
The grid used for this numerical scheme is shown in Figure 4.1.
∂U
For an interior point (i, j) on the grid, ∂S
is approximated by a central
difference formula
j j
∂U ∂Ui+1 − Ui−1
≈ (4.1.9)
∂S 2δS
∂U
To approximate the time derivative ∂τ
we use a forward difference approxi-
mation
∂U U j+1 − Uij
≈ i (4.1.10)
∂τ δτ
2
The second spatial derivative, ∂∂SU2 is approximated by
j
∂ 2U Ui−1 − 2Uij + Ui+1
j
≈ (4.1.11)
∂S 2 (δS)2
∂S Sij+1 − Sij
≈ (4.1.12)
∂τ δτ
30
We first discretised the PDE 4.1.4
à ! à !
j+1
Uij+1 − Uij Ui−1 − 2Uij + Ui+1
j+1 j
Ui−1 − 2Uij + Ui+1
j
= θ1 + θ2
δτ δS 2 δS 2
"Ã !#
Uij − Ui−1
j
Sij+1 − Sij
+
δS δτ
+ θ3 Gj+1 + θ4 Gji
¡ ¢
i (4.1.13)
θ1 + θ2 = θ3 + θ 4 = 1 (4.1.14)
As time increases the domain expands with B(τ ). The grid is appropriately
expanded by first determining the position of the free boundary then dividing
the domain into equal linearly spaced grid points. i.e if we let xj+1
N denote
the position of the free boundary, xf (τ ), then the grid points at the j + 1
time step are defined by xj+1 = x− + Ni xj+1
¡ ¢
N −x where i = 1, 2, . . . , N .
−
i
i
ẋi = (ẋN ) (4.1.15)
N
1
For θ = 0 the discretization is explicit, for θ = 2
we have the Crank-Nicolson
scheme, and for θ = 1 the method is implicit. In this dissertation we look
1
only at the θ = 2
case.
31
Re-arranging equation 4.1.13 we obtain
Where
δτ i
αi = (δS)2
> 0, βi = 2k > 0, γi = N δS
>0
Rearranging 4.1.16 we are left with
j+1
ci Ui−1 + ai Uij+1 + bi Ui+1
j+1
+ fi (Uij − Ui−1
j
)XNj+1 = ći Ui−1
j
+ ái Uij + b́i Ui+1
j
where
ci = −αi θ1 ći = αi θ2
ai = 1 + 2αi θ1 ái = 1 − 2αi θ2
bi = −αi θ1 b́i = αi θ2 (4.1.16)
~ j+1 = BU j + d~
T U j+1 + βX (4.1.17)
N
In order to find the location of free boundary at each successive time step,
we require one more piece of information. This is given by the derivative
∂C(B(τ ),τ )
boundary conditions 4.1.17. The condition ∂x
= 0 gives one extra
equation, namely uN −1 = uN . Writing this as a matrix equation with ??, we
have the equations
~ N = B~uj + d~
T ~uj+1 + βx (4.1.18)
hT ~u = 0
32
Where the components of T ,B,d and β are given by
2 + 2r −r 0 ··· 0
−r 2 + 2r −r
T = 0 . . . . . .
−r 0
.
. .
. .. ..
. −r
0 ··· 0 −r 2 + 2r
(4.1.19)
2 − 2r r 0 ··· 0
r 2 − 2r r
B = . .
.. ..
0 r 0
.
... ...
.
. r
0 ··· 0 r 2 − 2r
(4.1.20)
j j
u −u
di = 1
( − −
) i i−1 i j
2 g(x +ih,j∆τ )+g(x +ih,(j+1)∆τ ) −( xi −xi−1 )( N )xN
j j
u −u
βi = − i
N
i
j
i−1
j
x −x
i i−1
(4.1.21)
hT = 0 0 · · · · · · − 1 1 (4.1.22)
33
To simplify the notation we absorb the known quantity Buj into the d vector.
Writing this matrix equation explicitly we now have
uj1 d´1
2 − 2r −r 0 ··· 0 −β1
´
j
u2 d2
−r 2 − 2r −r . . . −β
2 ..
.
..
.
0 ... ... ..
−r 0 . .
.
. .
. = .
. ... ... ..
.. −r .
´
u j
d
N −1 N −1
0 ··· 0 −r 2 − 2r −βN −1
xj+1
N
0
0 0 · · · −1 1 0
0 0
This may be written symbolically as
~
T β~ ~u d´
= (4.1.23)
~hT 0 j+1
xN 0
Equation 4.7.5 can now be re-arranged to solve for xN
~ N = d~
T ~u + βx
~hT ~u = 0
~ ~
³ ´
⇒ ~u = T −1
d´ − βx −N
~
³ ´
⇒ ~hT T −1 d´ − T −1 βx
~ N = 0
˜
hT T −1 d´
⇒ xN = (4.1.24)
hT T −1 β̃
We have therefore defined a method for locating the free boundary xf (τ ) at
each successive time step of the algorithm. Once xj+1
N has been calculated,
we may determine the velocity at which the nodes move using the simple
equation
i
ẋi = ẋN (4.1.25)
N
34
We may then substitute into equation 4.7.5 to obtain
~ ~ j+1
T ~u = d´ − βxN (4.1.26)
4.2 Invertbility
The a′i s are the coefficients along the diagonal and the c′i s and the b′i s
are the coefficients on the lower and upper diagonal respectively. Then the
matrix A is non-singular.
1
If we consider the matrix T in the Crank-Nicolson, θ = 2
scheme, the coeffi-
cients are
ci = −αi θ1
ai = 1 + 2αi θ1
bi = −αi θ1
Clearly
1 + 2αi θ1 > 2αi θ1 ⇒ |ai | > ci | + |bi |
35
Therefore the matrix T is s.d.d and invertible
AU j+1 = BU j and ⇒ °W j ° ≤ K °W 0 °
° ° ° °
If the constant K is such that |K| ≤ 1 then the scheme is said to be stable.
Fourier or von Neumann’s method is the most precise and useful tool for
studying stability in the l2 norm. The Fourier method expresses the initial
values at the mesh points along t = 0 in terms of a finite Fourier series, then
considers the growth of a function that reduces to this series for t = 0 by a
seperation of variables method.
Fourier stability analysis can be restrictive however, since it can only be
applied to linear problems with constant coefficients and periodic boundary
conditions. The problem we are considering, u̇ − ux ẋ = uxx + g, is not a
linear equation. We can still proceed however, by ’linearising’ the problem,
and applying the stability condition locally at every interior point of the
domain.
We begin by making the substitution Unj = λn eikjδS and Xnj = ξn eikjδS . The
36
numerical scheme is given by
( )
j+1 j+1 j+1
uj+1
i − uj
i u i−1 − 2u i + u i+1
= θ + g(x− + ih, (m + 1)k)
k h2
( )
uji−1 − 2uji + uji+1
+ (1 − θ) + g(x− + ih, mk)
h2
uji − uji−1 i xj+1
N − xjN
+ (4.3.1)
h N k
eikjδS − λn eikjδS
µ ¶
λn+1 =
k
1 λn+1 eik(j−1)δS − 2λn+1 eikjδS + λn+1 eik(j+1)δS
µ ¶
=
2 h2
1 λn eik(j−1)δS − 2λn eikjδS + λn eik(j+1)δS
µ ¶
+
2 h2
ξn+1 eijkδS − ξn eijkδS
+ βi (4.3.2)
k
k ¡ ikδS −ikδS
¢ k ¡ ikδS −ikδS
¢
λn+1 −λn = λ n+1 e − e − 2 + λ n e − e − 2 +βi (ξn+1 − ξn )
2h2 2h2
ikδS ikδS
λn+1 (1 − 2µsin2 ( )) = λn (1 − 2µsin2 ( )) + (ξn+1 − ξn ) (4.3.3)
2 2
37
We therefore have two equations in two unknowns allowing us to solve for
amplification factors λ and ξ.
A graph of the initial date profile is given in figure 2.7. The domain of equa-
tion 3.2.6 is (−∞, B(0)) × (0, 21 σ 2 T ). Furthermore, from section 2.7 we know
where the free boundary, xf (0), must start. Clearly, the is a discrepancy be-
tween the points where the initial data falls to zero,i.e. x0N and the position
of xf (0).( see fig 4.4).
In moving the curve from x = 0 to x = xf (0) the finite difference algorithm
becomes unstable. We must find another way to advance the curve for the
first time step of the algorithm. Once this has been achieved, the algorithm
can then be used to progress the curve.
To see how the free boundary x = xf (τ ) initially moves away from xf (0)
38
we find an asymptotic solution that is valid close to expiry2 .
(x − x0 )f´(x0 ) = −k(x − x0 )
∂c ∂2c
= − k(x − x0 ) (4.4.2)
∂τ ∂x2
with
∂c
c= =0 (4.4.3)
∂x
Taken on
x = xf (τ ) (4.4.4)
xf (0) = x0 (4.4.5)
This local problem can be solved exactly ( see appendix ). The similarity
solution in terms of the variable
(x − x0 )
ξ= √ (4.4.6)
τ
is of the form
c = τ 3/2 c∗ (ξ) (4.4.7)
39
Where ξ0 is a constant taken to be 0.9034....
Equation 4.1.17 gives the condition that the derivative at the moving bound-
ary must be zero. In finite difference notation this can be written
uN −1 = uN (4.5.1)
Since we also have the boundary condition u(B(τ ), τ ) = 0 this implies that
uN −1 is also zero. A graph of this data is shown in figure 4.5 below.
The existence of a discontinuity in the data, where the function falls to zero,
causes instability close to the leading edge of the curve. This leads to poor
40
results in the region local to the moving boundary point.
In an effort to overcome this, we replace the zero derivative requirement by
an approximation. We seek a function of the form
ay 2 + by = 0 (4.5.2)
2ay + b = 0 (4.5.3)
ah2 = uN −1
a(2h)2 = uN −2 (4.5.4)
uN −2 = 4uN −1 (4.5.5)
The initial data profile presents two difficulties from a numerical perspective.
Firstly, the algorithm is found to be unstable when applied to the function
max(1−exp(x), 0). This problem is overcome by utilizing the approximation
to the free boundary derived in section 4.4. The second problem arises due
to the discontinuous data curve that results from imposing the boundary
41
Figure 4.4: Approximation to the Derivative
- Set τ = τ + ∆τ
END DO
For j = 2 to N − 1 DO
42
- Solve equation 4.1.24 for xiN
- Set τ = τ + ∆τ
END DO
4.7 Algorithm 2
To improve the accuracy of the algorithm used in section 4.1 we now introduce
a monitor function, the effect of which is to increase the number of grid points
in the local region(s) where the curve is changing rapidly. Likewise, regions
where the data is varying less rapidly will be assigned fewer grid points.
Our aim is to increase the resolution close to the moving boundary point β(τ ).
To understand the significance of this, we make a transformation back to
financial variables. Here, the moving boundary represents a division between
two points. Points to the left of the moving boundary represent asset prices
for which it would be unprofitable to enter into the option, whereas points
to the right are asset prices for which the option would be profitable. It is
therefore essential to gain an accurate approximation for this point.
The monitor function is described by
µ ¶
(xf (τ ) − x− )
xi = xf (τ ) − (N − i)2 (4.7.1)
N2
43
This function gives the position of each nodal point in terms of the free
boundary. The velocity of the nodes may be calculated from 4.7.1 as
(N − i)2
µ ¶
ẋi = 1− x˙f (τ ) (4.7.2)
N2
monitor function leads to a parabolic spacing of the grid points, see fig 4.7.
Since the x− spacing is no longer constant between grid points, the Crank-
Nicolson method used in the previous algorithm is no longer valid. In order
to apply a finite difference discretization we introduce a Lagrange polynomial
to approximate the second derivative.
The lagrange polynomial is degree two on the support {x0 , x1 , x2 } for the
function u(x). Replacing the difference approximation to the second deriva-
44
tive in equation gives
( j+1 j+1
)
j+1 j 2u j+1 2u
u −u i−1 2u i+1
i i =θ i
∆τ j+1 j+1 j+1 j+1 + j+1 j+1 j+1 j+1 + j+1 j+1 j+1 j+1
(x −x )(x −x ) (x −x )(x −x ) (x −x )(x −x )
i−1 i i−1 i+1 i i−1 i i+1 i+1 i−1 i+1 i
( j j
)
2u j 2u
i−1 2u i+1
+(1−θ) j j j j + j j i j j + j j j j
(x −x )(x −x ) (x −x )(x −x ) (x −x )(x −x )
i−1 i i−1 i+1 i i−1 i i i+1 i−1 i+1 i
j j µ ¶ j+1 j
u −u (N −i)2 x −x
+θ(g(x− +ih,j∆τ ))+(1−θ)(g(x− +ih,(j+1)∆τ ))+ i
j
i−1
j 1− 2
N
∆τ
N
x −x N
i i−1
~ N = d~´
T ~u + βx (4.7.4)
hT ~u = 0
k k
1 − α´2 − α´3 ··· ··· 0
−k k k
α´2 1 − α´3 − α´4
... ... ... ..
T =
0 .
(4.7.5)
. ..
... ... ...
.
. .
0 ... ... − αNk´−2 1 − k
αN´−1
d´i = k j
(
k
αi−1 ui−1 + 1+ αi )uji + αi+1
k uj
i+1
}
à ! j j
(N −i)2 u −u
βi = − 1−
N2
i
j
i−1
j
x −x
i i−1
hT = 0 0 · · · · · · − 1 1 (4.7.6)
Where
45
Writing this matrix equation explicitly we have
1 − αk´2 − αk´3 0 ··· −β1 uj1 d´1
−k k k ´
j
α´1 1 − α´3 − α´4 · · · −β2 u2 d2
0 ... ... ... .. .
..
.
..
.
. = .
. ... ... ... ..
.. . .. ..
´
0 ... ... − αNk´−2 1 − αNk´−1 −βN −1 uN −1 dN −1
j
0 0 ··· ··· −1 1 0 xN 0
The solution method for xN remains unchanged. We solve for xN as before,
~ ~
and express the solution as ~u = T −1 (d´ − βxN ).
The introduction of the monitor function, reducing the spatial interval near
the moving boundary, modifies the T matrix given in equation 4.7.6. The ’r’
46
k
values, h2
, are no longer a constant and must be evaluated at each pair of
nodes within the domain. In our modified algorithm, the T matrix contains
k
entries α
where α is equivalent to h in our previous method when the domain
was divided into linear elements. Clearly as the spacing becomes smaller, the
k k
magnitude of α
becomes larger. When the ration α
becomes larger than 1 the
solution method is found to become unstable. To rectify this we precondition
the matrices as follows;
Let D be the diagonal of T . Multiply both sides of equation 4.1.26 by D−1
to obtain
D−1 T ~u = D−1 d~ − D−1 βx
~ j+1
N (4.9.1)
√
- Set the Velocity of xN to ξ0 τ
47
- Set τ = τ + ∆τ
END DO
For j = 2 to N − 1 DO
- Set τ = τ + ∆τ
END DO
48
Chapter 5
5.1 Introduction
In this section we attempt to find a solution to the American call problem us-
ing a finite element method. The previous approach has been to replace the
continuous operation of differentiation with the discrete operation of finite
differences. We then reformulate the equations in terms of finite differences.
The fundamental idea of the finite element method is the replacement of con-
tinuous functions by piecewise approximations. The most elementary choice
of basis functions is the piecewise linear polynomials, this is the approxima-
tion we shall use in this report.
We divide the interval (−∞, B(0)) into linearly spaced elements. The parti-
tion is defined by
£ ¤
x− = x0 , x1 , · · · , xj−1 , xj , xj+1 , · · · , xN +1 = xf (5.1.1)
49
Define the basis functions ( ’hat’ functions ) as follows
0 : 0 ≤ x ≤ xi−1
x−xi−1 : xi < x ≤ xi+1
xi −xi−1
φi (x) =
xi+1 −x
xi+1 −xi : xi < x ≤ xi+1
0 : xi+1 < x ≤ 1
The functions φi are piecewise linear, the derivatives φ́i are constant on
(xi , xi+1 ) for each i = 0, 1, · · · , n.
0 : 0 ≤ x ≤ xi−1
1
: xi < x ≤ xi+1
hi−1
φ́i (x) =
−1
hi
: xi < x ≤ xi+1
0 : xi+1 < x ≤ 1
Finally let
N
X +1
U (x) = Ui φi (x) (5.1.2)
i=0
be a trial function, once differentiable between each xi . This is the piecewise
linear Finite Dimensional Representation.
50
5.2 Weak Form
Now θ, the area under the curve, is not constant in time, since we have a
source term g(x, τ ) that adds and subtracts ’mass’.
Z xi+1 (t)
Let φi udx = ci θ (5.2.3)
xi−1 (t)
P
be our new monitor function, where ci is a fraction such that i ci θ = 1. See
dψ
?? below We now define ẋ to be eqaul to dx
, the ’velocity’ potential.
At interior points, 1 ≤ i ≤ N − 1
xi+1 xi+1 (t) xi+1 (t)
dφi dφi dψ
Z Z Z
− (t) ux dx + φi gdx − u = ci θ̇ (5.2.4)
xi−1 (t) dx xi−1 (t) xi−1 (t) dx dx
At i = 0
x2 (t) x2 x2 (t)
dφ1 dφ1
Z Z Z
xi+1 (t)
−ux |x=x− − ux dx+ φ1 gdx+φi uẋ |xi−1 (t) − uẋdx = c1 θ̇
x0 (t) dx x0 x0 (t) dx
(5.2.5)
At i = N
xN (t) xN (t)
dφN −1
Z Z
− ux dx + φN −1 gdx (5.2.6)
xN −1 (t) dx xN −2 (t)
51
Figure 5.2: Monitor Function
Where
xi+1
dφi dφj
Z
dx = K´ij
xi−1 dx dx
Z xi+1
dφi dφj
u dx = Kij
xi−1 dx dx
Z xi+1
φi gdx = Gi
xi−1
Z xi+1
φi φj dx = Mij (5.2.8)
xi−1
52
dψ
by the boundary condition ẋ = 0 at x = x− . This implies that dx
= 0 at
x = x− , i.e. ψ0 = ψ1 . Writing this as a matrix equation we have
Ḱ ~c ~
ψ f~
= (5.2.9)
~hT
0 θ̇ 0
ψ ~˙
~ = Ḱ −1 (~g − K~u − θ) (5.2.10)
~ = 0
~hT ψ
The steps of the algoritm are outlined below Set Initial Conditions u0 , B 0 , x0
For j = 1to N − 1 DO
For i = 1 to N − 1 DO
- Calculate gi
53
- Calculate Kij
~˙c
~ = ~g − K~u − θ~
- Solve Ḱ ψ
dψ
- Calculate nodal velocities ẋ by setting ẋ = dx
- Solve M~u = ~c
END DO
END DO
The ẋ values are integrated using a Runge-Kutta order four method, ui+1 =
ui + 61 (k1 + 2k2 + 2k3 + h4 ).
Elements of the K matrix are calculated exactly whilst the entries in g vector
and Ḱ matrix are evaluated using a numerical quadrature. ( n = 2: Simpsons
Rule ). Both the K and Ḱ matrices are tridiagonal, equation 5.2.10 is found
using a tridiagonal solver. ( Thomas Algorithm )
54
Chapter 6
Results
In this section we present the numerical results of the finite difference method.
We assume the following parameters
K = 10
55
Figure 6.1: {T = 1, σ = 0.2, r = 0.03, D = 0.8r}
S = expx−(k2 −1)τ K
t = −((2τ )σ 2 − T )
u(x, τ )K
V (S, t) = +S−K (6.1.2)
expk1 τ
We apply the numerical scheme for the three combinations of the parameters
given in 6.1.1, time to expiry is taken as 1.
Figures 6.1,6.1 and 6.1 show the data curves plotted in the dimensionless
σ2
( Diffusion ) setting. A time step of ∆τ = 2
/100 is taken,the spatial
increment,∆h, is 0.001. It is clear from figures that as we vary the pa-
rameters the is a recognisable change in the profile of the curve. The contour
of the curve is determined predominantly by the source term g which itself
is a function of the interest and dividend parameters.
56
0.8
0.7
0.6
0.5
u(x,t) 0.4
0.3
0.2
0.1
0
−1.5 −1 −0.5 0 0.5 1
x
From the figures we see that choosing a larger interest rate and dividend pa-
rameter causes the free boundary to move further along the x−axis, as would
be expected. Shown below is a plot of the ’source’ term g in the diffusion
equation 3.2.6
57
0.8
0.7
0.6
0.5
u(x,t)
0.4
0.3
0.2
0.1
0
−1.5 −1 −0.5 0 0.5 1
x
0.8
0.6
0.4
g(x,t)
0.2
−0.2
−4 −3 −2 −1 0 1 2
x
58
1.4
1.2
0.8
0.6
0.4
0.2
0
−4 −3.5 −3 −2.5 −2 −1.5 −1 −0.5 0 0.5 1
59
20
18
16
14
12
C(S,t)
10
0 5 10 15 20 25
S
25
20
15
C(S,t)
10
0 5 10 15 20 25 30
S
60
30
25
20
C(S,t)
15
10
0 5 10 15 20 25 30 35
S
61
The ratio of S/K represents the value of the option, for a given value of S
when the strike price is taken to be 10. The term ’moneyness’ represents the
fact that when the ratio S/K < 1 the value of the underlying stock is less
than the price of the option, and when the fraction S/K > 1 the asset price
has risen above the strike price. Referring to table 6.1 we see that as the vale
of the underlying stock approaches the strike price the option value begins
to increase, and once the stock price actually exceeds the strike, the value of
option rises quickly.
The numerical results from the finite difference method are displayed in col-
umn ’FD’. The values generated by our method are approximately 25 smaller
than the benchmark values. This suggest that our approximation to the free
boundary in the dimensionless setting is poor.
62
−40
−60
−80
−100
−120
−140
−160
−0.05 −0.04 −0.03 −0.02 −0.01 0
The results provided by the finite elements algorithm proved to be very poor.
The method was unsuccessful in accurately modeling the motion of the free
boundary; after a very small number of time steps the velocity of the nodes in
the region close to the moving boundary became too large by several orders
of magnitude.
In the finite element code, we calculate a velocity potential Ψ, where Ψ =
Σψφi , and then rescale the grid by calculating the nodal velocity as equal to
dΨi
the gradient of the velocity potential. (ẋ = dt
. It is then a simple matter of
integrating this velocity to obtain the new grid points. A graph of a typical
values of Ψvsx is shown below. Ultimately, determining the new node points
rests on solving the system of equations
Ḱψ = f´
63
40 in size, down to 0.01. This matrix was found to be ill-conditioned, which
lead to poor results for ψ.
In an effort to overcome this we take the same approach as section ??, pre-
multiplying by D−1 where the D matrix is the sum of the diagonal matrices.
This did not solve the problem of the steep gradient close to the free boundary
however, and due to time constraints we were unable to obtain any accurate
results from this method.
64
Chapter 7
conclusion
The finite difference method applied to the American Call problem produced
valuations to within −30% of the BENCH value. The undervaluing of the
option is a result of the algorithms inability to accurately resolve the true po-
sition of the moving boundary at each time step. The algorithm was found
to be unstable when applying the derivative boundary conditions directly,
hence we saught to approximate the derivative by the form of a parabola.
Although this improved stability, we believe that this reduces accuracy of
the method in determing the moving boundary, the velocity is consistently
over approximated.
The introduction of the monitor function improved results, further work is
required in analysing the effect of approximation to the boundary conditions,
there are also several further monitor functions that can be implemented. A
comparison of these methods would be the next step in our research.
We have devised a new method for the valuation of American options as free
boundary problems, transforming the equation to a Lagrangian frame, and
are also the first to introduce the Shur method for locating the free bound-
65
ary. ( To our Knowledge ) Due to time limitations we were unsuccessful in
applying the finite element algorithm to the problem. We were unable to re-
solve the problem caused by the ill-conditioned matrix when evaluating the
velocity potentials.
Concluding, further work is required in formulating the finite element ap-
proach to this problem and finding an alternative method of calculating the
velocity potential . A comparison could then be made between the accu-
racy of the finite difference method we have presented, and the finite element
method that was originally proposed.
66
Chapter 8
Reference
67
7 J.Hull Options,Futures, and Other Derivatives. Prentice-Hall Interna-
tional, 2003
68
.1 Program 1 - pt1.f90
PROGRAM AMERICAN OPTIONS707
DOUBLE PRECISION : : K, NEGINFINITY ,XN, Delt , T f i n a l , t , B, h , A, C, DelTau ,
Domain , Tau ,SOUT
DOUBLE PRECISION,ALLOCATABLE: : U ( : ) ,UTEMP( : ) ,RHS ( : ) , Z ( : ) ,Y ( : ) ,HT ( : ) ,V ( : ) ,
X ( : ) ,U2D ( : , : ) ,X2D ( : , : ) , S2D ( : , : ) ,V2D ( : , : )
INTEGER: : IOS , j , N, L , i
DOUBLE PRECISION,PARAMETER: : I r =0.03D0 , Sigma =0.5D0 ,D=0.8D0∗ I r ,KK=10.0D0
DOUBLE PRECISION,PARAMETER: : K1=2.0D0∗ I r / ( Sigma ∗ ∗ 2 . 0 D0) ,K2=2.0D0∗ ( I r −D) / (
sigma ∗ ∗ 2 . 0 D0)
8 PRINT∗ , ’ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ’
PRINT∗ , ’ ∗ ∗’
PRINT∗ , ’ ∗ CRANK NICOLSON SOLULTION ∗’
PRINT∗ , ’ ∗ ∗’
PRINT∗ , ’ ∗ DIFFUSION EQUATION ∗’
PRINT∗ , ’ ∗ ∗’
PRINT∗ , ’ ∗ ∗’
PRINT∗ , ’ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ’
16 PRINT∗
NEGINFINITY=−30.0D0
T f i n a l =(( Sigma ∗ ∗ 2 . 0 D0) / 2 . 0 ) ∗ 1 . 0 D0
Domain=NEGINFINITY
N=800
24 L=NINT ( ( T f i n a l / ( Domain/N) ∗ ∗ 2 ) )
print ∗ ,L
k=( T f i n a l /L)
DelTau=( T f i n a l ) /L
B=0.0D0
32 XN=B
69
ALLOCATE(U( 0 :N) ,UTEMP( 0 :N) ,RHS ( 1 : N−1) ,V( 1 : N−1) , Z ( 1 : N−1) ,Y( 1 : N−1) ,HT( 1 : N
−1)&
,X( 0 :N) ,X2D ( 0 : L , 0 : N) ,U2D ( 0 : L , 0 : N) ,&
S2D ( 0 : L , 0 : N) ,V2D ( 0 : L , 0 : N) )
U=0.0D0
UTEMP=0.0D0
40 RHS=0.0D0
V=0.0D0
Z=0.0D0
Y=0.0D0
HT=0.0D0
t =0.0D0
S2D=0.0
V2D=0.0
48
70
72 OPEN(UNIT=35 ,FILE=” x0 . dat ” ,IOSTAT=IOS )
OPEN(UNIT=36 ,FILE=” u0 . dat ” ,IOSTAT=IOS )
IF ( IOS/=0) THEN
71
PRINT∗ , ’ E r r o r Occured in Opening The Output File ’
112 STOP
END IF
! ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
!∗ MAIN PROGRAM ∗
!∗ ∗
! ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
120 j =0
DO i =0 ,N
X2D( j , i )=X( i )
END DO
128
DO i =0 ,N
END DO
DO i =1 ,(N−4)
HT( i ) =0.0D0
END DO
HT(N−3) =9.0D0 / 4 . 0 D0
HT(N−2)=−1.0D0
144 HT(N−1) =4.0D0
j =0
Tau =(0.5D0∗ ( Sigma ∗ ∗ 2 . 0 D0) ) ∗ ( 1 . 0 D0) ∗ (REAL( j ) /REAL(L) )
h=(B−NEGINFINITY) /N
72
CALL CRANK NICOLSON(N, k , U, RHS, B, X, t , h , DelTau , Tau )
152
DO i =0 ,N
X( i )=NEGINFINITY+(REAL( i ) /REAL(N) ) ∗ (B−NEGINFINITY)
END DO
160 DO i =0 ,N
WRITE(UNIT=13 ,FMT= ’( E12 . 6 ) ’ )X( i )
END DO
DO j =1 ,L
168 h=(B−NEGINFINITY) /N
DO i =1 ,N−1
184 END DO
h=(XN−NEGINFINITY) /N
73
DO i =0 ,N
X2D( j , i )=X( i )
END DO
DO i =0 ,N
END DO
208 B=XN
t=T f i n a l −(2.0∗ Tau/ sigma ∗ ∗ 2 )
SOUT=(exp (XN+(1−K2) ∗ tau ) ) ∗KK
WRITE(UNIT=70 ,FMT= ’( F12 . 6 ) ’ )SOUT
WRITE(UNIT=71 ,FMT= ’( F12 . 6 ) ’ ) Tau
216 END DO
74
WRITE(UNIT=25 ,FMT= ’( F12 . 6 ) ’ ) ( (X2D( j , i ) , j =50 ,50) , i =0 ,N)
WRITE(UNIT=26 ,FMT= ’( F12 . 6 ) ’ ) ( (U2D( j , i ) , j =50 ,50) , i =0 ,N)
WRITE(UNIT=27 ,FMT= ’( F12 . 6 ) ’ ) ( (X2D( j , i ) , j =60 ,60) , i =0 ,N)
WRITE(UNIT=28 ,FMT= ’( F12 . 6 ) ’ ) ( (U2D( j , i ) , j =60 ,60) , i =0 ,N)
232 WRITE(UNIT=29 ,FMT= ’( F12 . 6 ) ’ ) ( (X2D( j , i ) , j =70 ,70) , i =0 ,N)
WRITE(UNIT=30 ,FMT= ’( F12 . 6 ) ’ ) ( (U2D( j , i ) , j =70 ,70) , i =0 ,N)
WRITE(UNIT=31 ,FMT= ’( F12 . 6 ) ’ ) ( (X2D( j , i ) , j =80 ,80) , i =0 ,N)
WRITE(UNIT=32 ,FMT= ’( F12 . 6 ) ’ ) ( (U2D( j , i ) , j =80 ,80) , i =0 ,N)
WRITE(UNIT=33 ,FMT= ’( F12 . 6 ) ’ ) ( (X2D( j , i ) , j =89 ,89) , i =0 ,N)
WRITE(UNIT=34 ,FMT= ’( F12 . 6 ) ’ ) ( (U2D( j , i ) , j =89 ,89) , i =0 ,N)
WRITE(UNIT=35 ,FMT= ’( F12 . 6 ) ’ ) ( (X2D( j , i ) , j =89 ,89) , i =0 ,N)
WRITE(UNIT=36 ,FMT= ’( F12 . 6 ) ’ ) ( (U2D( j , i ) , j =89 ,89) , i =0 ,N)
240
75
CLOSE(UNIT=11)
CLOSE(UNIT=12)
CLOSE(UNIT=13)
CLOSE(UNIT=14)
272 CLOSE(UNIT=15)
CLOSE(UNIT=16)
CLOSE(UNIT=17)
CLOSE(UNIT=18)
CLOSE(UNIT=19)
CLOSE(UNIT=20)
CLOSE(UNIT=21)
CLOSE(UNIT=22)
280 CLOSE(UNIT=23)
CLOSE(UNIT=24)
CLOSE(UNIT=25)
CLOSE(UNIT=26)
CLOSE(UNIT=27)
CLOSE(UNIT=28)
CLOSE(UNIT=29)
CLOSE(UNIT=30)
288
CLOSE(UNIT=31)
CLOSE(UNIT=32)
CLOSE(UNIT=33)
CLOSE(UNIT=34)
CLOSE(UNIT=35)
CLOSE(UNIT=36)
CLOSE(UNIT=37)
296 CLOSE(UNIT=38)
CLOSE(UNIT=39)
CLOSE(UNIT=40)
CLOSE(UNIT=41)
CLOSE(UNIT=42)
CLOSE(UNIT=43)
CLOSE(UNIT=44)
CLOSE(UNIT=45)
304 CLOSE(UNIT=46)
CLOSE(UNIT=47)
76
CLOSE(UNIT=48)
CLOSE(UNIT=49)
CLOSE(UNIT=64)
CLOSE(UNIT=65)
312
! ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
!∗ ∗
!∗ FUNCTIONS AND SUBROUTINES ∗
!∗ ∗
320 ! ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
DO i =0 ,N
336
U2D( j , i )=U( i )
END DO
DO i =0 ,N
77
WRITE(UNIT=61 ,FMT= ’(4251 E21 . 6 ) ’ )U2D( j , i )
END DO
r=k / ( h ∗ ∗ 2 . 0 D0)
DO i =1 ,N−1
RHS( i )=r ∗U( i −1) +(2.0D0−2.0D0∗ r ) ∗U( i )+r ∗U( i +1) +1.0∗ k ∗ (G(X( i ) , Tau )+G(X(
i ) , Tau+DelTau ) )&
+2.0D0∗ (REAL( i ) /REAL(N) ) ∗ ( (U( i )−U( i −1) ) /h ) ∗(−B)
368 END DO
SUBROUTINE VEC(N, U, V, h )
IMPLICIT NONE
INTEGER,INTENT(IN) : : N
376 DOUBLE PRECISION,DIMENSION( 0 :N) ,INTENT(IN) : : U
DOUBLE PRECISION,DIMENSION( 1 : N−1) ,INTENT(OUT) : : V
DOUBLE PRECISION,INTENT(IN) : : h
INTEGER: : i
78
DO i =1 ,N−1
384
END DO
END FUNCTION G
400
Alpha =0.0
s =0.0
79
y =0.0
r=k / ( h ∗ ∗ 2 )
424 a=r
b =(2.0D0+2.0D0∗ r )
c=r
Alpha ( 0 )=b
S ( 0 )=RHS( 1 )
432
DO i =1 ,(N−3)
END DO
448 DO i =(N−4) ,1 , −1
y ( i ) =( s ( i )+c ∗y ( i +1) ) / Alpha ( i )
END DO
Sv =7.0D0
DO i =0 ,N
t=T f i n a l −(2.0∗ Tau/ sigma ∗ ∗ 2 )
456 U( i )=y ( i )
80
END DO
DO i =0 ,N
464
S2D ( j , i )=SS ( i )
RES( i ) =((U( i ) ∗KK) / ( exp (K1∗ ( tau ) ) ) )+SS ( i )−KK
V2D( j , i )=RES( i )
END DO
DO i =0 ,N
END DO
488
81
DOUBLE PRECISION : : a , b , c , r
INTEGER: : i
s =0.0
y =0.0
504
r=k / ( h ∗ ∗ 2 . 0 D0)
a=( r )
b =(2.0D0+2.0D0∗ r )
c=( r )
Alpha ( 1 )=b
S ( 1 )=RHS( 1 )
512
DO i =2 ,(N−1)
520 END DO
DO i =(N−2) ,1 , −1
528 END DO
DO i =1 ,N−1
Z ( i )=y ( i )
END DO
536
82
END SUBROUTINE TRIDIAG SOL1
s =0.0
552 Y=0.0
U=0.0
r=k / ( h ∗ ∗ 2 . 0 D0)
a=( r )
b =(2.0D0+2.0D0∗ r )
c=( r )
560
Alpha ( 1 )=b
S ( 1 )=V( 1 )
DO i =2 ,N−1
END DO
DO i =(N−2) ,1 , −1
83
576
END DO
DO i =1 ,N−1
Y( i )=U( i )
END DO
584
A=0.0
B=0.0
DO i =1 ,N−1
600 A=A+Ht ( i ) ∗Z ( i )
B=B+Ht ( i ) ∗Y( i )
END DO
XN=A/B
.2 Program 2 - pt2.f90
PROGRAM movingr
DOUBLE PRECISION : : K, NEGINFINITY ,XN, Delt , T f i n a l , t , h , A, C,KN, BN, l e f t ,
domain
84
DOUBLE PRECISION,ALLOCATABLE: : U ( : ) ,UTEMP( : ) ,RHS ( : ) , Z ( : ) ,Y ( : ) ,HT ( : ) ,V ( : ) ,
X( : ) , r1 ( : ) , r2 ( : ) , r3 ( : )
INTEGER: : IOS , j , N, L , i
DOUBLE PRECISION,PARAMETER: : I r =0.03D0 , Sigma =0.2D0 ,D=0.8D0∗ I r ,KK=10.0D0
DOUBLE PRECISION,PARAMETER: : K1=2.0D0∗ I r / ( Sigma ∗ ∗ 2 . 0 D0) ,K2=2.0D0∗ ( I r −D) / (
sigma ∗ ∗ 2 . 0 D0)
8 PRINT∗ , ’ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ’
PRINT∗ , ’ ∗ ∗’
PRINT∗ , ’ ∗ CRANK NICOLSON SOLULTION ∗’
PRINT∗ , ’ ∗ ∗’
PRINT∗ , ’ ∗ DIFFUSION EQUATION ∗’
PRINT∗ , ’ ∗ ∗’
PRINT∗ , ’ ∗ ∗’
PRINT∗ , ’ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ’
16 PRINT∗
NEGINFINITY=−30.0D0
T f i n a l =(( Sigma ∗ ∗ 2 . 0 D0) / 2 . 0 ) ∗ 1 . 0
Domain=NEGINFINITY
24 N=300
L=NINT ( ( T f i n a l / ( Domain/REAL(N) ) ∗ ∗ 2 ) )
PRINT∗ , ( T f i n a l / ( Domain/REAL(N) ) ∗ ∗ 2 )
k=T f i n a l /L
L e f t =−20.0D0
N=100
32 t =0.0D0
D e l t =1.0D0/L
BN=0.0D0
KN=((BN−LEFT) /N∗ ∗ 2 . 0 D0)
85
ALLOCATE(U( 0 :N) ,UTEMP( 0 :N) ,RHS ( 1 : N−1) ,V( 1 : N−1) , Z ( 1 : N−1) ,Y( 1 : N−1) ,HT( 1 : N
−1) ,X( 0 :N) ,&
40 r 1 ( 1 : N−1) , r 2 ( 1 : N−1) , r 3 ( 1 : N−1) )
U=0.0D0
UTEMP=0.0D0
RHS=0.0D0
V=0.0D0
Z=0.0D0
Y=0.0D0
48 HT=0.0D0
t =0.0D0
56 IF ( IOS/=0) THEN
PRINT∗ , ’ E r r o r Occured in Opening The Output File ’
STOP
END IF
! ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
!∗ ∗
!∗ MAIN PROGRAM ∗
64 !∗ ∗
! ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
X( 0 )=LEFT
WRITE(UNIT=11 ,FMT= ’( E12 . 6 ) ’ )X( 0 )
DO i =1 ,N−1
72 X( i )=BN−(KN∗ (N−i ) ∗ ∗ 2 . 0 D0)
WRITE(UNIT=11 ,FMT= ’( E12 . 6 ) ’ )X( i )
END DO
X(N)=BN
WRITE(UNIT=11 ,FMT= ’( E12 . 6 ) ’ )X(N)
86
CALL BOUNDARY CONDITIONS(N, U,X)
80 DO i =1 ,(N−4)
HT( i ) =0.0D0
END DO
HT(N−3) =9.0D0 / 4 . 0 D0
HT(N−2)=−1.0D0
HT(N−1) =4.0D0
88 t =1.0D0
XN=BN
DO j =0 ,L
t=t−d e l t
96 DO i =1 ,(N−1)
r 1 ( i ) =(( x ( i −1)−x ( i ) ) ∗ ( x ( i −1)−x ( i +1) ) )
r 2 ( i ) =(( x ( i )−x ( i −1) ) ∗ ( x ( i )−x ( i +1) ) )
r 3 ( i ) =(( x ( i +1)−x ( i −1) ) ∗ ( x ( i +1)−x ( i ) ) )
END DO
BN=XN
112
DO i =1 ,N−1
RHS( i )=RHS( i )−V( i ) ∗XN
END DO
87
KN=((BN−LEFT) /N∗ ∗ 2 . 0 D0)
X( 0 )=LEFT
120 WRITE(UNIT=11 ,FMT= ’( E12 . 6 ) ’ )X( 0 )
DO i =1 ,N−1
X( i )=BN−(KN∗ (N−i ) ∗ ∗ 2 . 0 D0)
WRITE(UNIT=11 ,FMT= ’( E12 . 6 ) ’ )X( i )
END DO
X(N)=BN
WRITE(UNIT=11 ,FMT= ’( E12 . 6 ) ’ )X(N)
128
DO i =1 ,(N−1)
r 1 ( i ) =(( x ( i −1)−x ( i ) ) ∗ ( x ( i −1)−x ( i +1) ) )
r 2 ( i ) =(( x ( i )−x ( i −1) ) ∗ ( x ( i )−x ( i +1) ) )
r 3 ( i ) =(( x ( i +1)−x ( i −1) ) ∗ ( x ( i +1)−x ( i ) ) )
END DO
END DO
CLOSE(UNIT=11)
144 CLOSE(UNIT=12)
! ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
!∗ ∗
!∗ FUNCTIONS AND SUBROUTINES ∗
!∗ ∗
152 ! ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
88
SUBROUTINE BOUNDARY CONDITIONS(N, U,X)
IMPLICIT NONE
INTEGER,INTENT(IN) : : N
DOUBLE PRECISION,DIMENSION( 0 :N) ,INTENT(IN) : : X
DOUBLE PRECISION,DIMENSION( 0 :N) ,INTENT(OUT) : : U
160 INTEGER: : i
DO i =0 ,N
U( i )=max ( 1 . 0 D0−EXP(X( I ) ) , 0 . 0 D0)
WRITE(UNIT=12 ,FMT= ’( E12 . 6 ) ’ )U( i )
END DO
tp=t−d e l t
DO i =1 ,N−1
184 RHS( i ) =2.0∗ r 1 ( i ) ∗U( i −1) +(2.0D0+2.0∗ r 2 ( i ) ) ∗U( i ) +2.0∗ r 3 ( i ) ∗U( i +1)+(k ) ∗ (
G(X( i ) , t )+G(X( i ) , tp ) )&
+(1.0D0−(((N−i ) ∗ ∗ 2 . 0 D0) /N∗ ∗ 2 . 0 D0) ) ∗ ( (U( i )−U( i −1) ) / ( x ( i )−x ( i −1) ) ) ∗(−XN
) ∗2.0
! PRINT∗ , i ,RHS( i )
END DO
89
192 SUBROUTINE VEC(N, U, V, h ,X)
IMPLICIT NONE
INTEGER,INTENT(IN) : : N
DOUBLE PRECISION,DIMENSION( 0 :N) ,INTENT(IN) : : U,X
DOUBLE PRECISION,DIMENSION( 1 : N−1) ,INTENT(OUT) : : V
DOUBLE PRECISION,INTENT(IN) : : h
INTEGER: : i
200
DO i =1 ,N−1
V( i ) = −(2.0D0−(((N−i ) ∗ ∗ 2 . 0 D0) /N∗ ∗ 2 . 0 D0) ) ∗ (U( i )−U( i −1) ) / ( x ( i )−x ( i −1) )
END DO
END FUNCTION G
90
INTEGER: : i
Alpha =0.0D0
s =0.0D0
y =0.0D0
a =2.0∗ r 1 ( 2 )
240 b =(2.0D0−2.0∗ r 2 ( 2 ) )
c =2.0∗ r 3 ( 2 )
Alpha ( 1 )=b
S ( 1 )=RHS( 1 )
248
DO i =2 ,(N−3)
a =2.0∗ r 1 ( i )
b =(2.0D0−2.0∗ r 2 ( i ) )
c =2.0D0∗ r 3 ( i )
Alpha ( i )=b−(a ∗ c / Alpha ( i −1) )
S ( i )=RHS( i ) +(a ∗S ( i −1)/ Alpha ( i −1) )
256
END DO
264
DO i =(N−4) ,3 , −1
y ( i ) =( s ( i )+c ∗y ( i +1) ) / Alpha ( i )
91
END DO
DO i =0 ,N
U( i )=y ( i )
272 WRITE(UNIT=12 ,FMT= ’( E12 . 6 ) ’ )U( i )
Print ∗ ,U( i )
END DO
s =0.0D0
a =2.0∗ r 1 ( 1 )
b =(2.0D0−2.0∗ r 2 ( 1 ) )
c =2.0D0∗ r 3 ( 1 )
296
Alpha ( 1 )=b
S ( 1 )=RHS( 1 )
DO i =2 ,(N−1)
a =2.0∗ r 1 ( i )
304 b =(2.0D0−2.0∗ r 2 ( i ) )
c =2.0∗ r 3 ( i )
92
Alpha ( i )=b−(a ∗ c / Alpha ( i −1) )
S ( i )=RHS( i ) +(a ∗S ( i −1)/ Alpha ( i −1) )
END DO
DO i =(N−2) ,1 , −1
END DO
320 DO i =1 ,N−1
Z ( i )=Q( i )
END DO
s =0.0
Y=0.0
344 a =2.0∗ r 1 ( 1 )
93
b=2.0 −2.0∗ r 2 ( 1 )
c =2.0∗ r 3 ( 1 )
Alpha ( 1 )=b
S ( 1 )=V( 1 )
DO i =2 ,N−1
352
a =2.0∗ r 1 ( i )
b=2.0D0−2.0∗ r 2 ( i )
c =2.0∗ r 3 ( i )
360 END DO
DO i =(N−2) ,1 , −1
368 END DO
DO i =1 ,N−1
Y( i )=D( i )
END DO
376
94
384 DOUBLE PRECISION : : A, B
INTEGER: : i
A=0.0D0
B=0.0D0
DO i =1 ,N−1
A=A+Ht ( i ) ∗Z ( i )
392 B=B+Ht ( i ) ∗Y( i )
END DO
XN=A/B
.3 Program 3 - pt3.f90
PROGRAM FINITE ELEMENTS V5n
DOUBLE PRECISION : : k , NEGINFINITY , Delt , T f i n a l , t , h ,SUMG1,SUMG2, Theta1 ,
ThetaNew , tau , SumC, Domain ,GINTERGRAL, DERIV,SUMG
DOUBLE PRECISION,ALLOCATABLE: : U ( : ) , Z ( : ) ,Y ( : ) ,X ( : ) ,G ( : ) ,KM( : , : ) ,KP ( : , : ) ,
Phi ( : ) , h V e c t o r ( : ) ,dPdX ( : ) , Cvec ( : ) , ThetaV ( : ) , f ( : ) ,&
W( : ) ,HT ( : ) ,RHS ( : ) ,M( : , : ) ,RHS1 ( : )
INTEGER: : IOS , j , N, L , i , q
DOUBLE PRECISION,EXTERNAL: : Uprime
DOUBLE PRECISION,PARAMETER: : I r =0.03D0 , Sigma =0.2D0 ,D=0.8D0∗ I r ,KK=10.0D0
8 DOUBLE PRECISION,PARAMETER: : K1=2.0D0∗ I r / ( Sigma ∗ ∗ 2 . 0 D0) ,K2=2.0D0∗ ( I r −D) / (
sigma ∗ ∗ 2 . 0 D0)
PRINT∗ , ’ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ’
PRINT∗ , ’ ∗ ∗’
PRINT∗ , ’ ∗ FINITE ELEMENT SOLULTION ∗’
PRINT∗ , ’ ∗ ∗’
PRINT∗ , ’ ∗ DIFFUSION EQUATION ∗’
PRINT∗ , ’ ∗ ∗’
16 PRINT∗ , ’ ∗ ∗’
PRINT∗ , ’ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ∗ ’
95
PRINT∗
NEGINFINITY=−0.05D0
T f i n a l =(Sigma ∗ ∗ 2 . 0 ) / 2 . 0 D0
Domain=NEGINFINITY
24 N=100
L=NINT ( ( T f i n a l / ( Domain/N) ∗ ∗ 2 ) )
k=( T f i n a l /L)
D e l t =( T f i n a l ) /L
32
t =0.0D0
GINTERGRAL=0.0
Theta1 =0.0D0
B=0.0D0
f =0.0D0
40 RHS=0.0D0
ALLOCATE(U( 0 :N+1) ,G( 0 :N) , Z ( 1 : N−1) ,Y( 1 :N) ,X( 0 :N+1) ,KM( 1 : N, 0 : N+1) ,KP( 1 : N
− 1 , 0 :N) ,&
Phi ( 0 :N) , h v e c t o r ( 0 :N) ,dPdX ( 0 :N) , Cvec ( 1 : N−1)&
, ThetaV ( 0 :N) , f ( 1 : N−1) ,W( 0 :N) ,HT( 1 :N) ,RHS ( 1 : N−1) ,M( 1 : N− 1 , 0 :N) ,RHS1 ( 1 : N
−1) )
SumC=0.0D0
U=0.0D0
48 Y=0.0D0
Z=0.0D0
Y=0.0D0
KM=0.0D0
KP=0.0D0
t =0.0D0
f =0.0D0
96
M=0.0D0
56
! ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
!∗ ∗
!∗ MAIN PROGRAM ∗
!∗ ∗
88 ! ∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
DO i =0 ,N
97
X( i )=NEGINFINITY+(REAL( i ) /REAL(N) ) ∗ (B−NEGINFINITY)
END DO
104
HT( 1 ) =1.0D0
HT( 2 ) =−1.0D0
DO i =3 ,N
HT( i ) =0.0D0
112
END DO
t =1.0D0
DO i =0 ,N−1
Theta1=Theta1+ThetaV ( i )
END DO
WRITE(UNIT=29 ,FMT= ’( E12 . 6 ) ’ ) Theta1
128
DO q=1 ,1
GINTEGRAL=0.0
98
Tau=(( sigma ∗ ∗ 2 . 0 D0) / 2 . 0 D0) ∗ ( 1 . 0 D0) ∗ (REAL( q ) /REAL(L) )
136 DO i =1 ,N−1
SumC=SumC+Cvec ( i )
144 END DO
DO i =1 ,N−1
CALL SIMPSONS1(X,SUMG1, N, i , q , L)
CALL SIMPSONS2(X,SUMG2, N, i , q , L)
152
G( i )=SUMG1+SUMG2
END DO
G(N)=G(N−1)
160
DO i =1 ,N−1
END DO
168 KP( 1 , 1 )=KP( 1 , 1 ) / 2 . 0 D0
99
DO j =2 ,N−2
END DO
DO j =1 ,N−1
184
Y( j )=G( j )−RHS1( j )
192 DO j =1 ,N−1
f ( j )=Y( j )
END DO
200 DO i =1 ,N
END DO
100
CALL THETA SOLVE(N,W, Z , HT, ThetaNew )
WRITE(UNIT=22 ,FMT= ’( E14 . 8 ) ’ ) ThetaNew
216 DO j =1 ,N−1
END DO
Do i =1 ,N−1
224
CALL I n t e g r a t e G (X,SUMG, N, i , q , L)
GINTERGRAL=GINTERGRAL+SUMG
END DO
DO j =1 ,N−1
END DO
DO i =1 ,N
101
IF ( i /=N) THEN
dPdX( i ) =(((X( i )−X( i −1) ) ∗ ( Phi ( i )−Phi ( i −1) ) +(X( i +1)−X( i ) ) ∗ ( Phi (
i +1)−&
Phi ( i ) ) ) ) / ( (X( i )−X( i −1) ) +(X( i +1)−X( i ) ) )
256
ELSE
END DO
264 dPdX ( 0 ) =0.0D0
DO i =0 ,N
END DO
272 DO i =0 ,N
END DO
DO i =1 ,N−1
END DO
DO i =1 ,N−1
102
288
END DO
DO i =0 ,N
! PRINT∗ , i ,U( i )
296 !WRITE(UNIT=12 ,FMT= ’(E12 . 6 ) ’ )U( i )
END DO
! Theta1=ThetaNew
END DO
CLOSE(UNIT=11)
CLOSE(UNIT=12)
CLOSE(UNIT=13)
CLOSE(UNIT=14)
CLOSE(UNIT=15)
CLOSE(UNIT=16)
312 CLOSE(UNIT=17)
CLOSE(UNIT=18)
CLOSE(UNIT=19)
CLOSE(UNIT=20)
CLOSE(UNIT=21)
CLOSE(UNIT=22)
CLOSE(UNIT=23)
CLOSE(UNIT=24)
320 CLOSE(UNIT=25)
CLOSE(UNIT=26)
CLOSE(UNIT=27)
CLOSE(UNIT=28)
CLOSE(UNIT=29)
CLOSE(UNIT=30)
CLOSE(UNIT=31)
103
328 END PROGRAM FINITE ELEMENTS V5n
DO i =0 ,N
END DO
344
SUBROUTINE I n t e g r a t e G (X,SUMG, N, i , q , L)
IMPLICIT NONE
INTEGER,INTENT(IN) : : N, i , q , L
DOUBLE PRECISION,DIMENSION( 0 :N+1) ,INTENT(IN) : : X
DOUBLE PRECISION,INTENT(OUT) : : SUMG
352 DOUBLE PRECISION : : h , XI0 , XI2 , XI1 , a , b , XI ,SUM, Tau
DOUBLE PRECISION,EXTERNAL: : GF
DOUBLE PRECISION,PARAMETER: : Sigma =0.4D0
INTEGER: : k ,M
SUMG=0.0
360
M=INT (N∗ 2 0 0 )
a=X( i −1)
b=X( i )
104
XI0=GF( b , Tau )+GF( a , Tau )
368 XI1 =0.0D0
XI2 =0.0D0
DO k=1 ,M−1
XI=a+k∗h
XI2=XI2+GF( XI , Tau )
ELSE
XI1=XI1+GF( XI , Tau )
END IF
END DO
END SUBROUTINE I n t e g r a t e G
SUBROUTINE SIMPSONS1(X,SUMG1, N, i , q , L)
392 IMPLICIT NONE
INTEGER,INTENT(IN) : : N, i , q , L
DOUBLE PRECISION,DIMENSION( 0 :N+1) ,INTENT(IN) : : X
DOUBLE PRECISION,INTENT(OUT) : : SUMG1
DOUBLE PRECISION : : h , XI0 , XI2 , XI1 , a , b , XI ,SUM, Tau
DOUBLE PRECISION,EXTERNAL: : GF
DOUBLE PRECISION,PARAMETER: : Sigma =0.4D0
INTEGER: : k ,M
400
M=N∗200
a=X( i −1)
105
b=X( i )
DO k=1 ,M−1
XI=a+k∗h
416
END IF
424 END DO
SUBROUTINE SIMPSONS2(X,SUMG2, N, i , q , L)
IMPLICIT NONE
INTEGER,INTENT(IN) : : N, i , q , L
DOUBLE PRECISION,DIMENSION( 0 :N+1) ,INTENT(IN) : : X
DOUBLE PRECISION,INTENT(OUT) : : SUMG2
DOUBLE PRECISION : : h , XI0 , XI2 , XI1 , a , XI , b ,SUM, Tau
440 DOUBLE PRECISION,PARAMETER: : Sigma =0.4
DOUBLE PRECISION,EXTERNAL: : GF
INTEGER: : k ,M
106
Tau=(( Sigma ∗ ∗ 2 . 0 D0) / 2 . 0 D0) ∗ ( 0 . 5 D0) ∗ (REAL( q ) /REAL(L) )
a=X( i )
b=X( i +1)
448 M=N∗200
h=ABS( (X( i +1)−X( i ) ) ) /M
DO k=1 ,M−1
456
XI=a+k∗h
END IF
END DO
107
DOUBLE PRECISION,PARAMETER: : K1=2.0D0∗ I r / ( Sigma ∗ ∗ 2 . 0 D0) ,K2=2.0D0∗ ( I r −D) / (
sigma ∗ ∗ 2 . 0 D0)
END FUNCTION GF
108
520 a=X( i −1)
b=X( i )
M=INT (N∗ 2 0 0 )
h=ABS( (X( i )−X( i −1) ) ) /M
XI0=f u n c ( a )+f u n c ( b )
XI1 =0.0D0
528 XI2 =0.0D0
XI =0.0D0
DO k=1 ,M−1
XI=a+k∗h
XI2=XI2+f u n c ( XI )
ELSE
XI1=XI1+f u n c ( XI )
END IF
END DO
a=X( i )
b=X( i +1)
XI0=f u n c ( a )+f u n c ( b )
XI1 =0.0D0
XI2 =0.0D0
XI =0.0
109
DO k=1 ,M−1
560
XI=a+k∗h
XI2=XI2+f u n c ( XI )
ELSE
XI1=XI1+f u n c ( XI )
568
END IF
END DO
a=X( i )
b=X( i +1)
XI0=f u n c ( a )+f u n c ( b )
584 XI1 =0.0D0
XI2 =0.0D0
XI =0.0
DO k=1 ,M−1
XI=a+k∗h
XI2=XI2+f u n c ( XI )
ELSE
XI1=XI1+f u n c ( XI )
110
END IF
600 END DO
a=X( i −1)
b=X( i )
608
XI0=f u n c ( a )+f u n c ( b )
XI1 =0.0D0
XI2 =0.0D0
XI =0.0D0
XI=a+k∗h
XI2=XI2+f u n c ( XI )
ELSE
624 XI1=XI1+f u n c ( XI )
END IF
END DO
111
DOUBLE PRECISION FUNCTION f u n c ( x )
IMPLICIT NONE
640 DOUBLE PRECISION,INTENT(IN) : : x
f u n c =1.0D0
END FUNCTION f u n c
112
END FUNCTION UINTER
UN=U( i −1)
UM=U( i )
UP=U( i +1)
a=X( i −1)
696 b=X( i )
M=N∗200
h=ABS( (X( i )−X( i −1) ) ) /M
DO k=1 ,M−1
XI=a+k∗h
XI2=XI2+UINTER1(UN,UM, UP, XI , i , a , b )
712 ELSE
XI1=XI1+UINTER1(UN,UM, UP, XI , i , a , b )
113
END IF
END DO
b=X( i +1)
a=X( i )
h=(X( i +1)−X( i ) ) /M
XI0=UINTER2(UN,UM, UP, a , i , a , b )+UINTER2(UN,UM, UP, b , i , a , b )
XI1 =0.0D0
XI2 =0.0D0
728 XI =0.0D0
DO k=1 ,M−1
XI=a+k∗h
END IF
END DO
KP( i , i )=SUM1+SUM2
f=KP( i , i )
b=X( i +1)
a=X( i )
752
114
XI0=UINTER2(UN,UM, UP, a , i , a , b )+UINTER2(UN,UM, UP, b , i , a , b )
XI1 =0.0D0
XI2 =0.0D0
XI =0.0D0
XI=a+k∗h
XI2=XI2+UINTER2(UN,UM, UP, XI , i , a , b )
ELSE
768 XI1=XI1+UINTER2(UN,UM, UP, XI , i , a , b )
END IF
END DO
b=X( i )
a=X( i −1)
DO k=1 ,M−1
XI=a+k∗h
792
115
IF (MOD( k , i n t ( 2 ) ) /=0) THEN
XI2=XI2+UINTER1(UN,UM, UP, XI , i , a , b )
TEST=UINTER1(UN,UM, UP, XI , i , a , b )
ELSE
XI1=XI1+UINTER1(UN,UM, UP, XI , i , a , b )
800
END IF
END DO
S=0.0D0
a=−KP( 1 , 0 )
b=KP( 1 , 1 )
c ( 1 )=−KP( 1 , 2 )
Alpha ( 1 )=b
116
832 S ( 1 )=Y( 1 )
DO i =2 ,(N−1)
a=−KP( i , i −1)
b=KP( i , i )
c ( i )=−KP( i , i +1)
840
END DO
848 DO i =(N−2) ,1 , −1
END DO
Z (N)=Z (N−1)
Z ( 0 )=Z ( 1 )
856 DO i =0 ,N
Phi ( i )=Z ( i )
864
END DO
117
IMPLICIT NONE
872 INTEGER,INTENT(IN) : : N
DOUBLE PRECISION,DIMENSION( 0 :N) ,INTENT(INOUT) : : X
DOUBLE PRECISION,DIMENSION( 0 :N) ,INTENT(IN) : : dPdX
DOUBLE PRECISION,INTENT(IN) : : k
DOUBLE PRECISION,DIMENSION( 1 ,N−1) ,INTENT(IN) : : h v e c t o r
INTEGER: : i
DO i =0 ,N
880
X( i )=X( i )+k∗dPdX( i )
END DO
M=N∗300
a=X( i −1)
b=X( i )
904 h=ABS( (X( i )−X( i −1) ) ) /M
UN=U( i −1)
UM=U( i )
UP=U( i +1)
118
XI0=(a−X( i −1) ) ∗UINTER1(UN,UM, UP, a , i , a , b ) +(b−X( i −1) ) ∗UINTER1(UN,UM, UP
, b , i , a , b)
912 XI1 =0.0D0
XI2 =0.0D0
DO k=1 ,M−1
XI=a+k∗h
ELSE
XI1=XI1+(XI−X( i −1) ) ∗UINTER1(UN,UM, UP, XI , i , a , b )
END IF
END DO
928
b=X( i +1)
a=X( i )
936
DO k=1 ,M−1
119
XI=a+k∗h
TEST=UINTER2(UN,UM, UP, XI , i , a , b )
END IF
END DO
END SUBROUTINE C v e c t o r
968
M=(N∗ 3 0 0 0 )
a=X( i )
b=X( i +1)
984 h=ABS( (X( i +1)−X( i ) ) ) /M
120
UP=U( i +1)
UM=U( i )
XI0=UINTER(UP,UM, a , i , a , b )+UINTER(UP,UM, b , i , a , b )
992 XI1 =0.0D0
XI2 =0.0D0
DO k=1 ,M−1
XI=a+k∗h
XI2=XI2+UINTER(UP,UM, XI , i , a , b )
ELSE
XI1=XI1+UINTER(UP,UM, XI , i , a , b )
END IF
END DO
1008
ThetaV ( i )=SUMC1
121
s =0.0D0
y =0.0D0
1032 a=−KP( 1 , 0 )
b=KP( 1 , 1 )
c ( 1 )=−KP( 1 , 2 )
Alpha ( 1 )=b
S ( 1 )=f ( 1 )
1040 DO i =2 ,(N−1)
a=−KP( i , i −1)
b=KP( i , i )
c ( i )=−KP( i , i +1)
END DO
DO i =(N−2) ,1 , −1
END DO
DO i =1 ,N−1
Z ( i )=y ( i )
WRITE(UNIT=23 ,FMT= ’( E12 . 6 ) ’ ) Z ( i )
122
1064 END DO
1080
s =0.0D0
W=0.0D0
U=0.0D0
1088 a=−KP( 1 , 0 )
b=KP( 1 , 1 )
c ( 1 )=−KP( 1 , 2 )
Alpha ( 1 )=b
S ( 1 ) =(Cvec ( 1 ) /k )
DO i =2 ,N−1
1096
a=−KP( i , i −1)
b=KP( i , i )
c ( i )=−KP( i , i +1)
123
1104 END DO
DO i =(N−2) ,1 , −1
1112 END DO
DO i =1 ,N−1
W( i )=U( i )
WRITE(UNIT=24 ,FMT= ’( E12 . 6 ) ’ )W( i )
END DO
1120
A=0.0D0
B=0.0D0
DO i =1 ,N−1
A=A+Ht ( i ) ∗Z ( i )
1136 B=B+Ht ( i ) ∗W( i )
END DO
ThetaNew=A/B
124
END SUBROUTINE THETA SOLVE
DOUBLE PRECISION : : a , b
INTEGER: : i
1160 S=0.0D0
a=M( 1 , 0 )
b=M( 1 , 1 )
c ( 1 )=M( 1 , 2 )
Alpha =0.0D0
Alpha ( 1 )=b
S ( 1 )=RHS( 1 )
1168
DO i =2 ,(N−1)
a=M( i , i −1)
b=M( i , i )
c ( i )=M( i , i +1)
END DO
125
Z (N−1)=S (N−1)/ Alpha (N−1)
DO i =(N−2) ,1 , −1
1184
END DO
DO i =1 ,N−1
U( i )=Z ( i )
1192
END DO
U( 0 )=exp (K1∗ tau ) ∗ ( 1 . 0 D0−exp (NEGINFINITY−(K2−1.0D0) ∗ tau ) )
U(N) =0.0
a=X( i −1)
b=X( i )
MI=i n t (N∗ 2 0 0 )
h=ABS( (X( i )−X( i −1) ) ) /MI
1216
126
XI2 =0.0D0
XI =0.0
DO k=1 ,MI−1
1224 XI=a+k∗h
XI2=XI2 +((XI−a ) ∗ ∗ 2 )
ELSE
XI1=XI1 +((XI−a ) ∗ ∗ 2 )
1232 END IF
END DO
a=X( i )
b=X( i +1)
1240
XI=a+k∗h
XI2=XI2 +((b−XI ) ∗ ∗ 2 )
ELSE
1256 XI1=XI1 +((b−XI ) ∗ ∗ 2 )
127
END IF
END DO
M( i , i )=SUM1+SUM2
a=X( i )
b=X( i +1)
DO k=1 ,MI−1
1280
XI=a+k∗h
END IF
END DO
128
1296
a=X( i −1)
b=X( i )
DO k=1 ,MI−1
XI=a+k∗h
END IF
1320 END DO
129