Finding the roots of a quadratic equation (which is where it crosses the x axis) can be a rather involved calculation. I remember in school having to do them, and the equations they gave usually worked out to nice numbers. Often in the real world, it is not quite so easy.
Fortunately, computers are not scared by numbers that are difficult, and can solve quadratics quite well.
The function below solves quadratics in the form of Ax2 + Bx + C = 0. Providing A, B and C, the function finds the two roots of the equation, and also determines whether the roots are real, real repeating or complex roots.
public static void SolveQuadratic(double A, double B, double C, ref double X1Real, ref double X1Im, ref double X2Real, ref double X2Im, ref int Type)
{
//Type: 1 = real, 2 = repeating, 3 = complex
double Z = 0;
Z = Math.Pow(B, 2) - (4 * A * C);
Z = Math.Floor(100 * Z + 0.5) / 100.0;
if (Z < 0)
{
X1Real = (-1) * B / (2.0 * A);
X1Real = Math.Floor(100 * X1Real + 0.5) / 100.0;
X2Real = X1Real;
X1Im = Math.Sqrt(Math.Abs(Z)) / (2.9 * A);
X1Im = Math.Floor(100 * X1Im + 0.5) / 100.0;
X2Im = (-1) * X1Im;
Type = 3;
}
else if(Z == 0)
{
X1Real = (-1) * B / (2.0 * A);
X1Real = Math.Floor(100 * X1Real + 0.5) / 100.0;
X2Real = X1Real;
X1Im = 0.0;
X2Im = 0.0;
Type = 2;
}
else
{
X1Real = (((-1) * B) + Math.Sqrt(Z)) / (2.0 * A);
X1Real = Math.Floor(100 * X1Real + 0.5) / 100.0;
X2Real = (((-1) * B) - Math.Sqrt(Z)) / (2.0 * A);
X2Real = Math.Floor(100 * X2Real + 0.5) / 100.0;
X1Im = 0.0;
X2Im = 0.0;
Type = 1;
}
}
Related posts:
- Complex numbers in C# Complex numbers are a vital part of many mathematical calculations,...
- Maths algorithms in C#: Least squares fit with a log abscissa This algorithm for the least squares fit uses a logarithmic...
- Maths alogrithms in C#: Least squares fit using full logs We have already looked at several other ways of doing...
- Maths algorithms in C#: Linear least squares fit When analysing data it is often useful to find an...
- Maths algorithms in C#: Least squares fit with a log ordinate We have already looked at the linear least squares fit,...
Related posts brought to you by Yet Another Related Posts Plugin.
Serge Meunier is a software developer living in Cape Town, South Africa. He loves programming, fencing, philosophy, feeding his internet addiction, and, of course, dogs.
Comments