Home c++ please please find the values ​​of the SINX / X function using...

please please find the values ​​of the SINX / X function using the Taylor series

Author

Date

Category

Find the values ​​of the SINX / X function using a series of Taylor on the range from Hnacht to the Clacon of the DX interval and accuracy. I wrote a function but after the second iteration value is not correct. I write in C++ Builder, below code:

{
  Long Double XS, XE, DX, E, Y;
  XS = STRTOFLOAT (EDIT1 - & GT; text);
  XE = STRTOFLOAT (EDIT2 - & GT; TEXT);
  DX = STRTOFLOAT (EDIT3 - & GT; TEXT);
  E = STRTOFLOAT (EDIT4 - & GT; text) ;;
  Memo1- & gt; lines- & gt; append ("f \ tx \ tn");
  For (long double x = xs; x & lt; xe + dx / 2; x + = dx) {
    int n = 0;
    LONG DOUBLE AN = X;
    y = 0;
    While (Fabs (AN) & GT; E) {
      y + = an;
      N ++;
      AN * = ((POW (-1, N)) * POW (X, 2 * N)) / FACT (2 * N + 1);
    }
    Memo1- & GT; Lines- & GT; Append (Floattostrf (y, fffixed, 10, 2) + "\ t" + Floattostrf (x, fffixed, 10, 2)
      + "\ t" + inttostr (n));
  }
}
// ---------------------------------------------------- ---------------------------
LONG DOUBLE FACT (INT N)
{
  if (n & lt; 0)
    Return 0;
  if (n == 0)
    Return 1;
  ELSE.
    RETURN N * FACT (N - 1);
}

Answer 1

In general, you have a mistake that if you want to search for sin (x) / x , then the Taylor formula will look like this:

sin (x) / x = 1 - x ^ 2/3! + X ^ 5/5! + ... + (-1) ^ n-1 * x ^ (2n - 2) / (2n - 1)!

That is, you need to multiply not on x ^ (2n) , and on x ^ (2n - 2) .

Also not better to calculate the factorial and degree, and not every time on a new one?

Here is an example code if you do in the console:

void functionlor (Float Xstart, Float Xfinish, Float XStep, Float EPS) {
  Long double FACT = 1;
  LONG DOUBLE POW_X = 0;
  Long long int c = 0;
  INT SIGN = 1;
  long double x = xstart;
  LONG DOUBLE TEMP;
  LONG DOUBLE SINX_X;
  LONG DOUBLE AN = 0;
  While (x + xstep & lt; = xfinish) {
    FACT = 1;
    POW_X = 1;
    SIGN = 1;
    C = 1;
    sinx_x = sin (x) / x;
    temp = 0;
    do {
      AN = (SIGN * POW_X) / FACT;
      TEMP + = AN;
      SIGN * = -1;
      FACT = FACT * (C + 1) * (C + 2);
      C + = 2;
      POW_X * = X * X;
    } While (/ * Fabs (AN) & GT; EPS * / SINX_X & lt; = temp - eps || sinx_x & gt; = temp + eps);
    COUT & LT; & LT; TEMP & LT; & LT; "" & lt; & lt; x & lt; & lt; Endl;
    x + = xstep;
  }
}

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