Home c++ Calculation of a function with the help of a Taylor series

Calculation of a function with the help of a Taylor series

Author

Date

Category

Real function can be represented by a series of Taylor, the radius of the convergence of which is determined by the position of the singular points of the function. Any final segment of the Taylor series is a polynomial, so it can be calculated using addition and multiplication operations.

The program calculates the ln (1-x) function. Help please remake calculations in the code for the exp (x ^ 2 function .

# include & lt; stdio.h & gt;
#Include & lt; stdlib.h & gt;
#Include & lt; math.h & gt;
// This Function Is Calculating The Power Of X
Double Powx (Double X, Int i)
{
  Switch (I)
  {
    Case 0:
      Return 1;
    Case 1:
      Return x;
    Default:
      RETURN X * POWX (X, I - 1);
  }
}
// This Function Is Calculating The Value Of A Member of the Serie
Double Membvalcalc (Double X, Int i)
{
  RETURN -1 * (POWX (X, I) / I);
}
INT MAIN ()
{
  / * ACC = accuracy; result = aproximated value; ET_Result = Contain Library Value;
    AprDiff = Is The Difference Between Library's Value and The Result of this Program's Work * /
  Double X, ACC, Result, Membval, Et_result, AprDiff, Achievedacc;
  int i, j = 0,
      n; // N - IS The Maximal Member of Serie In The Calculation
  PrintF ("This Program Will Calculate Function LN (1-X). \ n");
  Printf ("Please, Enter X Value Between -1.0 and 1.0 AS 'Double' Type:");
  ScanF ("% LF", & amp; x);
  // Area of ​​Definition of this Function; IF X NOT Belong To This Area, The Program Will Be Closed
  if ((X & LT; -0.9999999) || (X & GT; 0.9999999))
  {
    PrintF ("Error: X Must Be The Value Between -1 and 1. \ n");
    Return -1;
  }
  Printf ("Please, Enter N Value As 'Integer' Type:");
  Scanf ("% d", & amp; n);
  Printf ("Enter Accuracy of Calculation AS 'Double' Type:");
  Scanf ("% LF", & amp; ACC);
  If (ACC & LT; 0)
  {
    PrintF ("Error: Accuracy Is Negative Number.");
    Return -1;
  }
  RESULT = 0.0;
  // This for Loop Is Calculating Each Member of The Serie and Increase The Result Variable by This Value
  for (i = 1; i & lt; = n; i ++)
  {
    Membval = Membvalcalc (X, I);
    j = i;
    if ((Membval & gt; 0) & amp; & amp; (Membval & lt; = ACC))
    {
      j = i - 1;
      Break;
    }
    ELSE If ((Membval & LT; 0) & amp; & amp; ((Membval * -1) & lt; = ACC))
    {
      j = i - 1;
      Break;
    }
    ELSE.
    {
      Result + = Membval;
    }
  }
  ET_RESULT = log (1 - x);
  if (result & gt; = 0) & amp; & amp; (et_result & gt; = 0))
  {
    APRDIFF = RESULT - ET_RESULT;
  }
  ELSE if ((Result & lt; 0) & amp; & amp; (et_result & lt; 0))
  {
    APRDIFF = (result * -1) - (et_result * -1);
  }
  ELSE.
  {
    APRDIFF = RESULT + ET_RESULT;
  }
  Achievedacc = Membvalcalc (x, j);
  if (APRDIFF & LT; 0)
  {
    APRDIFF * = -1;
  }
  if (Achievedacc & lt; 0)
  {
    Achievedacc * = -1;
  }
  PrintF ("Total Quantity of Calculated Members of the Serie:% D \ N", j);
  Printf ("The Achieved Accuracy of Calculation IS% F \ N", Achievedacc);
  PrintF ("The result log (1-x) is:% LF \ N", Result);
  Printf ("Using the Math.h Library We Could Get The Result Log =% LF \ N"
      et_result);
  Printf ("The Difference Between Calculated Approximation and Library's Value:% LF",
      APRDIFF);
  Return 0;
}

Answer 1, Authority 100%

in general, insert where you need – here is a function that believes exp (x ^ 2) with the accuracy EPS decomposition in a Taylor series:

double expx2 (double x, double eps)
{
  Double Sum = 1.0, Term = 1.0;
  x = x * x;
  For (int n = 1; Term & gt; eps; ++ n)
  {
    SUM + = TERM * = X / N;
  }
  Return Sum;
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions