|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | + |
| 4 | +namespace Algorithms.Numeric.Series |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Maclaurin series calculates nonlinear functions approximation |
| 8 | + /// starting from point x = 0 in a form of infinite power series: |
| 9 | + /// f(x) = f(0) + f'(0) * x + ... + (f'n(0) * (x ^ n)) / n! + ..., |
| 10 | + /// where n is natural number. |
| 11 | + /// </summary> |
| 12 | + public static class Maclaurin |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Calculates approximation of e^x function: |
| 16 | + /// e^x = 1 + x + x^2 / 2! + ... + x^n / n! + ..., |
| 17 | + /// where n is number of terms (natural number), |
| 18 | + /// and x is given point (rational number). |
| 19 | + /// </summary> |
| 20 | + /// <param name="x">Given point.</param> |
| 21 | + /// <param name="n">The number of terms in polynomial.</param> |
| 22 | + /// <returns>Approximated value of the function in the given point.</returns> |
| 23 | + public static double Exp(double x, int n) => |
| 24 | + Enumerable.Range(0, n).Sum(i => ExpTerm(x, i)); |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Calculates approximation of sin(x) function: |
| 28 | + /// sin(x) = x - x^3 / 3! + ... + (-1)^n * x^(2*n + 1) / (2*n + 1)! + ..., |
| 29 | + /// where n is number of terms (natural number), |
| 30 | + /// and x is given point (rational number). |
| 31 | + /// </summary> |
| 32 | + /// <param name="x">Given point.</param> |
| 33 | + /// <param name="n">The number of terms in polynomial.</param> |
| 34 | + /// <returns>Approximated value of the function in the given point.</returns> |
| 35 | + public static double Sin(double x, int n) => |
| 36 | + Enumerable.Range(0, n).Sum(i => SinTerm(x, i)); |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Calculates approximation of cos(x) function: |
| 40 | + /// cos(x) = 1 - x^2 / 2! + ... + (-1)^n * x^(2*n) / (2*n)! + ..., |
| 41 | + /// where n is number of terms (natural number), |
| 42 | + /// and x is given point (rational number). |
| 43 | + /// </summary> |
| 44 | + /// <param name="x">Given point.</param> |
| 45 | + /// <param name="n">The number of terms in polynomial.</param> |
| 46 | + /// <returns>Approximated value of the function in the given point.</returns> |
| 47 | + public static double Cos(double x, int n) => |
| 48 | + Enumerable.Range(0, n).Sum(i => CosTerm(x, i)); |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Calculates approximation of e^x function: |
| 52 | + /// e^x = 1 + x + x^2 / 2! + ... + x^n / n! + ..., |
| 53 | + /// and x is given point (rational number). |
| 54 | + /// </summary> |
| 55 | + /// <param name="x">Given point.</param> |
| 56 | + /// <param name="error">Last term error value.</param> |
| 57 | + /// <returns>Approximated value of the function in the given point.</returns> |
| 58 | + /// <exception cref="ArgumentException">Error value is not on interval (0.0; 1.0).</exception> |
| 59 | + public static double Exp(double x, double error = 0.00001) => ErrorTermWrapper(x, error, ExpTerm); |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Calculates approximation of sin(x) function: |
| 63 | + /// sin(x) = x - x^3 / 3! + ... + (-1)^n * x^(2*n + 1) / (2*n + 1)! + ..., |
| 64 | + /// and x is given point (rational number). |
| 65 | + /// </summary> |
| 66 | + /// <param name="x">Given point.</param> |
| 67 | + /// <param name="error">Last term error value.</param> |
| 68 | + /// <returns>Approximated value of the function in the given point.</returns> |
| 69 | + /// <exception cref="ArgumentException">Error value is not on interval (0.0; 1.0).</exception> |
| 70 | + public static double Sin(double x, double error = 0.00001) => ErrorTermWrapper(x, error, SinTerm); |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Calculates approximation of cos(x) function: |
| 74 | + /// cos(x) = 1 - x^2 / 2! + ... + (-1)^n * x^(2*n) / (2*n)! + ..., |
| 75 | + /// and x is given point (rational number). |
| 76 | + /// </summary> |
| 77 | + /// <param name="x">Given point.</param> |
| 78 | + /// <param name="error">Last term error value.</param> |
| 79 | + /// <returns>Approximated value of the function in the given point.</returns> |
| 80 | + /// <exception cref="ArgumentException">Error value is not on interval (0.0; 1.0).</exception> |
| 81 | + public static double Cos(double x, double error = 0.00001) => ErrorTermWrapper(x, error, CosTerm); |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Wrapper function for calculating approximation with estimated |
| 85 | + /// count of terms, where last term value is less than given error. |
| 86 | + /// </summary> |
| 87 | + /// <param name="x">Given point.</param> |
| 88 | + /// <param name="error">Last term error value.</param> |
| 89 | + /// <param name="term">Indexed term of approximation series.</param> |
| 90 | + /// <returns>Approximated value of the function in the given point.</returns> |
| 91 | + /// <exception cref="ArgumentException">Error value is not on interval (0.0; 1.0).</exception> |
| 92 | + private static double ErrorTermWrapper(double x, double error, Func<double, int, double> term) |
| 93 | + { |
| 94 | + if (error <= 0.0 || error >= 1.0) |
| 95 | + { |
| 96 | + throw new ArgumentException("Error value is not on interval (0.0; 1.0)."); |
| 97 | + } |
| 98 | + |
| 99 | + var i = 0; |
| 100 | + var termCoefficient = 0.0; |
| 101 | + var result = 0.0; |
| 102 | + |
| 103 | + do |
| 104 | + { |
| 105 | + result += termCoefficient; |
| 106 | + termCoefficient = term(x, i); |
| 107 | + i++; |
| 108 | + } |
| 109 | + while (Math.Abs(termCoefficient) > error); |
| 110 | + |
| 111 | + return result; |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Single term for e^x function approximation: x^i / i!. |
| 116 | + /// </summary> |
| 117 | + /// <param name="x">Given point.</param> |
| 118 | + /// <param name="i">Term index from 0 to n.</param> |
| 119 | + /// <returns>Single term value.</returns> |
| 120 | + private static double ExpTerm(double x, int i) => Math.Pow(x, i) / Factorial.Calculate(i); |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// Single term for sin(x) function approximation: (-1)^i * x^(2*i + 1) / (2*i + 1)!. |
| 124 | + /// </summary> |
| 125 | + /// <param name="x">Given point.</param> |
| 126 | + /// <param name="i">Term index from 0 to n.</param> |
| 127 | + /// <returns>Single term value.</returns> |
| 128 | + private static double SinTerm(double x, int i) => |
| 129 | + (Math.Pow(-1, i) / Factorial.Calculate(2 * i + 1)) * Math.Pow(x, 2 * i + 1); |
| 130 | + |
| 131 | + /// <summary> |
| 132 | + /// Single term for cos(x) function approximation: (-1)^i * x^(2*i) / (2*i)!. |
| 133 | + /// </summary> |
| 134 | + /// <param name="x">Given point.</param> |
| 135 | + /// <param name="i">Term index from 0 to n.</param> |
| 136 | + /// <returns>Single term value.</returns> |
| 137 | + private static double CosTerm(double x, int i) => |
| 138 | + (Math.Pow(-1, i) / Factorial.Calculate(2 * i)) * Math.Pow(x, 2 * i); |
| 139 | + } |
| 140 | +} |
0 commit comments