-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b679f0
commit d054b72
Showing
8 changed files
with
71 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.