I have searched so far and I know that there are several ways (1, 2, 3, 4) so far I have used the following code:
Fv_calc(:,2) = arrayfun(@(n) MaxPositiveRoot2DegreePolynomial(QuadraticCoefficients(n,:)), 1:size(QuadraticCoefficients,1));
Where MaxPositiveRoot2DegreePolynomial is the following function:
function root = MaxPositiveRoot2DegreePolynomial(c)
d = c./c(1);
root = eig([0 -d(3);1 -d(2)]);
condition = root == abs(root);
root = root(condition);
if isempty(root)
root = 0;
end
root = max(root);
end
Where QuadraticCoefficients is a 62271x3 matrix each row containing the coefficients a
, b
, c
of a general quadratic equation. ax^2+bx+c
Regarding the problem that I'm solving, all the roots will be real and so I've used a modified function for finding roots in order to not waste time to check if a root is real or not.
By profiling the code, I've found that each run of the function MaxPositiveRoot2DegreePolynomial
takes about 0.000047 seconds which will be about 2.914925371 seconds for 62271 runs. But the code
Fv_calc(:,2) = arrayfun(@(n) MaxPositiveRoot2DegreePolynomial(QuadraticCoefficients(n,:)), 1:size(QuadraticCoefficients,1));
takes 3.198597803 to run. So about 0.283672431 is taken just by the arrayfun
. I want to see if there is any way to lessen this time?