Seria Maclaurin
/// <summary>
/// Maclaurin series calculates nonlinear functions approximation
/// starting from point x = 0 in a form of infinite power series:
/// f(x) = f(0) + f'(0) * x + ... + (f'n(0) * (x ^ n)) / n! + ...,
/// where n is natural number.
/// </summary>
public static class Maclaurin
{
/// <summary>
/// Calculates approximation of e^x function:
/// e^x = 1 + x + x^2 / 2! + ... + x^n / n! + ...,
/// where n is number of terms (natural number),
/// and x is given point (rational number).
/// </summary>
/// <param name="x">Given point.</param>
/// <param name="n">The number of terms in polynomial.</param>
/// <returns>Approximated value of the function in the given point.</returns>
public static double Exp(double x, int n) =>
Enumerable.Range(0, n).Sum(i => ExpTerm(x, i));
public static double Sin(double x, int n) =>
Enumerable.Range(0, n).Sum(i => SinTerm(x, i));
public static double Cos(double x, int n) =>
Enumerable.Range(0, n).Sum(i => CosTerm(x, i));
public static double Exp(double x, double error = 0.00001) => ErrorTermWrapper(x, error, ExpTerm);
public static double Sin(double x, double error = 0.00001) => ErrorTermWrapper(x, error, SinTerm);
public static double Cos(double x, double error = 0.00001) => ErrorTermWrapper(x, error, CosTerm);
private static double ErrorTermWrapper(double x, double error, Func<double, int, double> term)
{
if (error <= 0.0 || error >= 1.0)
{
throw new ArgumentException("Error value is not on interval (0.0; 1.0).");
}
var i = 0;
var termCoefficient = 0.0;
var result = 0.0;
do
{
result += termCoefficient;
termCoefficient = term(x, i);
i++;
}
while (Math.Abs(termCoefficient) > error);
return result;
}
private static double ExpTerm(double x, int i) => Math.Pow(x, i) / Factorial.Calculate(i);
private static double SinTerm(double x, int i) =>
Math.Pow(-1, i) / Factorial.Calculate(2 * i + 1) * Math.Pow(x, 2 * i + 1);
private static double CosTerm(double x, int i) =>
Math.Pow(-1, i) / Factorial.Calculate(2 * i) * Math.Pow(x, 2 * i);
}
PrashantUnity