[go: up one dir, main page]

0% found this document useful (0 votes)
82 views7 pages

5116 Notes

1. The document discusses using MATLAB for coding functions, numerical derivatives, root searching, quadrature, linear algebra, and interpolation. 2. Key MATLAB concepts covered include defining anonymous functions, using vectors, hidden output, LU decomposition, and piecewise linear and Gaussian interpolation. 3. The document provides sample MATLAB code for bisection root searching, Regula Falsi method, Gaussian integration, Chebyshev interpolation, and piecewise linear interpolation.

Uploaded by

Koh JZ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views7 pages

5116 Notes

1. The document discusses using MATLAB for coding functions, numerical derivatives, root searching, quadrature, linear algebra, and interpolation. 2. Key MATLAB concepts covered include defining anonymous functions, using vectors, hidden output, LU decomposition, and piecewise linear and Gaussian interpolation. 3. The document provides sample MATLAB code for bisection root searching, Regula Falsi method, Gaussian integration, Chebyshev interpolation, and piecewise linear interpolation.

Uploaded by

Koh JZ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Fe5116

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

Sample function (vector):


– myfun1=@(x) (1-cos(x))./(x.^2);

Following function should be written as follows in matlab


f(x) = exp(x) - x.^2 [note the .]

Kahn summation algorithm

X + eps(x) ~= x
However, x + eps(x)/2 == x

Mul refers to multiplication


– Use Horner rule to reduce no. of operations
– Horner rule leads to n sum and n mil, where n represents order of
polynomial.
Computation cost:
- flop refers to no. of floating operations

Quiz 2: content from Feb 11


– lectures were on 11 Feb, 4 Mar;11 Mar

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:

⸺to solve Chebishev on data⸺


% Define the data points
x = [-1, -0.5, 0, 0.5, 1];
y = [1, 0.5, 0, 0.5, 1];

% Degree of the Chebyshev polynomial


n = 4;

% Construct the Vandermonde matrix


V = zeros(length(x), n+1);
for i = 1:length(x)
V(i,:) = chebyshevT(0:n, x(i));
end

% Solve the least squares problem


c = V \ y';

% Evaluate the Chebyshev polynomial at 100 equally spaced points in [-1, 1]


xx = linspace(-1, 1, 100);
yy = zeros(size(xx));
for i = 1:length(xx)
yy(i) = chebyshevT(0:n, xx(i)) * c;
end

% Plot the data points and the Chebyshev polynomial


plot(x, y, 'o', xx, yy);

⸻—end⸻—

piecewise linear interpolation:


% Define the data points
x = [0 1 2 3 4];
y = [0 2 1 4 3];

% Define the query points


xi = linspace(0, 4, 101);

% Perform piecewise linear interpolation


yi = interp1(x, y, xi, 'linear');

% Plot the data points and the interpolated curve


plot(x, y, 'o', xi, yi);

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);

% Gauss-Hermite weights and abscissa for 3 points


w = [0.2954, 1.1816, 0.2954];
x = [-0.7746, 0, 0.7746];

% 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)

Use gbm_forward_price to solve: A stock price follows a geometric


motion with constant parameters. The 2y forward price is 12. The spot price
is 10. What is the 1y forward price?

inverse of Diagonal matrix.


matrix = [[1,2],[3,4]]
D = [[sqrt(1),0],[0,sqrt(4)]]
D inv = [[1/sqrt(1)],[1/sqrt(4)]]

You might also like