- Joined
- 5/28/23
- Messages
- 14
- Points
- 13
I'm learning from book
Below are my sample test code with real examble from option pricing
probem:
Thomans Algorithm yield different result against double sweep when BCR is not 0.
I have a guess it comes from boundary handling, also have a question, where is a[0] and c[Last one] getting use in Thomas Algorithm?
Financial Instrument Pricing Using C++, 2nd Edition
Below are my sample test code with real examble from option pricing
probem:
Thomans Algorithm yield different result against double sweep when BCR is not 0.
I have a guess it comes from boundary handling, also have a question, where is a[0] and c[Last one] getting use in Thomas Algorithm?
C++:
#include <iostream>
#include <vector>
#include "LUSolver.hpp"
#include "DoubleSweep.hpp"
#include "print_utils.hpp"
int main(){
//std::size_t J = 4;
//double h = 1.0 / static_cast<double>(J);
// Boundary conditions
double BCL = 0.0; // LHS
//double BCR = 20.19801326693245; // Option real payoff, but even use a rounding value 20 below will lead to different value
double BCR = 20.0;
// Double Sweep
// std::vector<double> a(J+1,1.0);
// std::vector<double> b(J+1,-2.0);
// std::vector<double> c(J+1,1.0);
// std::vector<double> r(J+1, -2.0*h*h); // Right-hand side
std::vector<double> a = {0.0, 0.0352, 0.7031, 2.0039, 0.0};
std::vector<double> b = {0.0, -57.4453, -59.3438, -62.5078, 0.0};
std::vector<double> c = {0.0, 0.5977, 1.8281, 3.6914, 0.0};
std::vector<double> r = {0.0, -2.9883, -288.6328, -708.7500, 0.0};
std::vector<double> a2(a.begin() + 1, a.end() - 1);
std::vector<double> b2(b.begin() + 1, b.end() - 1);
std::vector<double> c2(c.begin() + 1, c.end() - 1);
std::vector<double> r2(r.begin() + 1, r.end() - 1);
r2[0] -= BCL;
r2[r2.size() - 1] -= BCR;
LUTridiagonalSolver<double> mySolver3(a2, b2, c2, r2);
DoubleSweep<double> mySolver(a, b, c, r, BCL, BCR);
print("solution using Thomas LU Decomposition :{}\n", mySolver3.solve());
//solution using Thomas LU Decomposition :[0.1064, 5.2293, 11.8262, ]
print("solution using Double Sweep : {}\n", mySolver.solve());
//solution using Double Sweep : [0.0, 0.1067, 5.2559, 12.6882, 20.0, ]
return 0;
}
;
Attachments
Last edited: