Skip to content

Commit

Permalink
Added extra root stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
savarsinha123 committed Mar 10, 2024
1 parent 2b679f0 commit d054b72
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 75 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ https://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm
Gauss-Legendre Quadrature approximates an integral over the interval $[-1, 1]$
by approximating the integral as a weighted sum of function evaluations of the
nth Legendre polynomial roots, where larger $n$ results in more accurate
approximations.
approximations. See here for more info:
https://en.wikipedia.org/wiki/Gauss%E2%80%93Legendre_quadrature

### Monte-Carlo Integration

Monte-Carlo integration refers to techniques which involve randomly sampling
from the integration domain, computing the MLE for the expected value of the
function, and then multiplying this mean by the volume of the subset.
function, and then multiplying this mean by the volume of the subset. See here
for more info:
https://en.wikipedia.org/wiki/Monte_Carlo_integration
10 changes: 5 additions & 5 deletions integration/gaussQuad.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
function I = gaussQuad(y, a, b, n)
function I = gaussQuad(f, a, b, n)
%{
gaussQuad: This function evalues the integral of y over the bounds
GAUSSQUAD: This function evalues the integral of f over the bounds
defined by a and b using n total nodes
Parameters:
y: integrand
f: integrand
a: lower bounds
b: upper bounds
n: number of nodes
Expand All @@ -20,7 +20,7 @@
weights = 2./((1 - roots.^2).*(Pderiv(roots)).^2);

% combine weights in multidimensional case
N = nargin(y);
N = nargin(f);
all_weights = 1;
for i = 1:N
all_weights = kron(all_weights, weights);
Expand All @@ -37,7 +37,7 @@

% evaluate integral
diffs = 0.5*(b - a);
I = prod(diffs) .* sum(all_weights .* y(ntuples{:})');
I = prod(diffs) .* sum(all_weights .* f(ntuples{:})');
end

function C = cartesian(varargin)
Expand Down
2 changes: 1 addition & 1 deletion integration/monteCarloIntegral.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function I = monteCarloIntegral(f, a, b, subset, n)
%{
monteCarloIntegral: This function evaluates the integral of f over
MONTECARLOINTEGRAL: This function evaluates the integral of f over
the subset using the assumption that the function is enclosed
within a rectangular region whose bounds are specified by a and b.
A total of n points are sampled to calculate this integral.
Expand Down
67 changes: 0 additions & 67 deletions roots/finding_zeros.m

This file was deleted.

39 changes: 39 additions & 0 deletions roots/muller.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function r = muller(y, x1, x2, x3, threshold)
%{
MULLER: Computes single solution to equation using Muller's method
Parameters:
y: Equation equal to zero
x1: First guess
x2: Second guess
x3: Third guess
threshold: Margin of error in function evaluation (how close to
zero does the evaluation have to be to stop recursing)
Returns:
Root computed
%}
r = muller_rec(y, [x1; x2; x3], threshold);
end

function r = muller_rec(y, x, threshold)
% fit quadratic through three guesses
xmatrix = [(x - x(3)).^2, (x - x(3)), ones(3, 1)];
yvalues = arrayfun(y, x);
coeff = xmatrix \ yvalues;
discrim = sqrt(coeff(2)^2 - 4 * coeff(1) * coeff(3));

% choose sign for denominator that maximizes its magnitude
if abs(coeff(2) + discrim) > abs(coeff(2) - discrim)
xnew = x(3) - 2 * coeff(3) / (coeff(2) + discrim);
else
xnew = x(3) - 2 * coeff(3) / (coeff(2) - discrim);
end

% if the accuracy threshold is met, return, else recurse with new guess
if abs(y(xnew)) > threshold
r = muller_rec(y, [x(2:3); xnew], threshold);
else
r = xnew;
end
end
21 changes: 21 additions & 0 deletions roots/newtonRaphson.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function r = newtonRaphson(y, x, threshold)
%{
MULLER: Computes single solution to equation using Newton-Raphson
Parameters:
y: Equation equal to zero
x: Initial guess
threshold: Margin of error in function evaluation (how close to
zero does the evaluation have to be to stop iterating)
Returns:
Root computed
%}

% calculate y'(x) numerically
deriv = @(x) (y(x + 1e-6) - y(x)) / 1e-6;
while abs(y(x)) > threshold
x = x - y(x)/deriv(x);
end
r = x;
end
Binary file added roots/roots.mlx
Binary file not shown.
Binary file added roots/roots.pdf
Binary file not shown.

0 comments on commit d054b72

Please sign in to comment.