Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions utils/quadroots.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


function [x1, x2] = quadroots(a, b, c)
% Calculates the roots of the quadratic equation 0 = a*x^2 + b*x + c, and
% returns them as a tuple. If a root is repeated, the elements of the tuple
Expand All @@ -17,10 +19,13 @@
% -------
% x1, x2: float
% Roots of the equation.
if nargin==0, test_quadroots; return; end
if nargin==0
test_quadroots;
return
end

x1 = (-b + sqrt(b*b - 4*a*c))/2*a;
x2 = (-b - sqrt(b*b - 4*a*c))/2*a;
x1 = (-b + sqrt(b*b - 4*a*c))/(2*a);
x2 = (-b - sqrt(b*b - 4*a*c))/(2*a);

%%%
function test_quadroots
Expand All @@ -30,4 +35,4 @@
[root1, root2] = quadroots(a, b, c);
root1exact = 2;
root2exact = 0.5;
assert(abs(root1exact - root1) < 1e-14 && abs(root2exact - root2) < 1e-14);
assert((abs(root1exact - root1) < 1e-14) && (abs(root2exact - root2) < 1e-14));
Loading