Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

maclaurin series

/// <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);
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# how to crete array 
Csharp :: C# short getter setter 
Csharp :: how to minimum text length in textbox in c# 
Csharp :: how to skip bin/Debug/netcoreapp3.1/ on the reltaive path 
Csharp :: unity yield return 
Csharp :: add spaces in string 
Csharp :: replace first occurrence of character in string c# 
Csharp :: emgucv open image c# 
Csharp :: all substrings of a string c# 
Csharp :: compact in laravrl 
Csharp :: Data at the root level is invalid. Line 1, position 1. 
Csharp :: how to store some variables on the device in unity 
Csharp :: vb.net read text file line by line 
Csharp :: group by unique c# 
Csharp :: c# count directories in directory and subdirectories 
Csharp :: linq foreach c# 
Csharp :: how to use curl in asp.net c# 
Csharp :: get device name c# console 
Csharp :: exception handling in c# web api 
Csharp :: unity set parent canvas 
Csharp :: c# goto statement 
Csharp :: c# callback action lambda 
Csharp :: c# if isset 
Csharp :: Disable Debug.log Unity 
Csharp :: c# datagridview set column header alignment 
Csharp :: c# static 
Csharp :: How to make a simple console select screen using C# ReadKey 
Csharp :: how to check type in c# 
Csharp :: allow scroll with wheel mouse datagridview c# 
Csharp :: how to generate random unique id in c# 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =