A
A
alex_deerk2016-11-13 19:17:36
C++ / C#
alex_deerk, 2016-11-13 19:17:36

Recurrent formula precision?

It is necessary to calculate the function using the recursive ratio of the terms of the series.
36713a1e09924ccf81bd6d94f3663fb9.png
Here is my recursive formula:
e652fb6e97ba4029acbcd4f52a5f6cf8.jpg
Program code:

#include <cmath>
#include <iostream>
#include <conio.h>
using namespace std;

int main() {
  const double pi = 3.14159265358979323846;
  double x = pi / 2;
  double s = 0,
    eps = 1e-06;
  int k = 1;
  double elem = x * x;
  s = elem;
  while(fabs(elem) >= eps) {
    cout << k << " : " << elem << " : " << s << endl;
    ++k;
    elem *= (-4 * x * x) / ((2 * k + 1) * (2 * k + 2));
    s += elem;
  }
  cout << "S: " << s << endl;
  cout << "MATH: " << sin(x) * sin(x);
  _getch();
    return 0;
}

Actually, the problem is that the result of my formula is wildly different from the result of functions from the library. There are suspicions that the formula is not correct, but rewrote it several times.
053144f3ef1c4b0c89d974f78780598c.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2016-11-13
@alex_deerk

You have derived the formula for getting k+1 element from k-th, and in the program you use it to get k-th element from k-1. These are different formulas.

A
abcd0x00, 2016-11-18
@abcd0x00

It's just that the ratio is correct. Next, you need to choose the correct initial values ​​and the correct check to stop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question