Professional Documents
Culture Documents
Using Matlab:
Coding functions on the fly:
– e.g., f=@(x) exp(x) - (x+2)
– x=(0:0.01:2);plot(x,f(x))
– [to add title]
– title(‘ Chart Title’)
– Find x st f(x) = 0
– If vector. Need to use . In front of operation, e.g., 10.^b instead 10^b
– Refer to ASCII table
– 0 at start of sequence refers to positive number (IEEE-754 Binary 32
(float))
Using ; hides output
X + eps(x) ~= x
However, x + eps(x)/2 == x
Numerical Derivatives:
– slide 7: Total Error = alpha * (1/h) + beta * h, where beta * h is the
truncation error and alpha * (1/h) is the round-off error.
Root Search:
– refer to bisection function
– sample: myfun=@(x)exp(x)-1
[r,x]=bisection(myfun,-1,2,1e-4)
– shortcoming of bisection method: will only return one solution even if
multiple solutions exist
– Regula Falsi method: (comparing with bisection)
– https://www.geeksforgeeks.org/difference-between-bisection-
method-and-regula-falsi-method/
Quadrature
– Gaussian integration: just apply formula. see slide 38. split f(x) into
w(x)g(x), using table in slide 39
Linear algebra:
Construct a quadratic spline which satisfies with the following conditions: from
a symmetric function
● It passes by the points x(i) and x(i+1)
● It is continuous
● Its first derivative is continuous
● Since we know the function is symmetric, we want that the first
derivative in (-1,0) is the opposite as the first derivative in (0,1)
What is the value of the spline in x=0.5 ?
to enter matrix:
– A = [1 2 3; 4 5 6;7 8 9] (e.g.,)
to enter column vector:
– B = [1;2;3]
use A\B to solve system of linear equations [matlab solution]
L U decomposition using Gauss Elimination:
[L U] = lu(sparse(X),0),
where X is the matrix
Interpolation:
⸻—end⸻—
plot(x,y,’o’,xx,yy);
To interpolate based on curve:
% Get y value at x = 0.5
x_val = 0.5;
y_val = interp1(xx, yy, x_val);
⸺
Gauss-Hermite estimation:
% Define function to integrate
f = @(x) exp(-x.^2);
% Compute integral
I = gauss_hermite_integrate(f, w, x);
% Display result
disp(['Approximate integral: ', num2str(I)]);
⸻
Trees: (25th Mar)
– americanBinomialVec preferred to americanBinomialPricer
– Solving X = ln(s), system of 3 equations with 2 unknowns:
– NEED TO USE [ ]!!!
– e.g., in [d,u,p] = parametersBinomGBMCRR(0.07,0,0.3,2,4)
–