What is the error? Task calculation of the expansion function in a row. in the inner loop the value of the sum of the TEXT TASKS enter image description here

#include<cmath> #include<iostream> using namespace std; int factorial(int n) { int fact = 1; for (int i = 1; i <= n; i++) { fact *= i; } return fact; } int main() { int i, n; double x, xx, sum = 0.0, summa = 0.0, eps = 0.25; cin >> n; for (i = 0; i < n; i++) { cin >> x; xx = (-1) * x * x + 1; sum = eps + 1; if (abs(sum) > eps) { sum = pow(xx, i) / factorial(i); summa += sum; } cout << "Значение приближенное по eps:" << ' ' << summa << ' ' << endl; cout << "Значение без eps:" << ' ' << pow(M_E, xx) << endl; } } 

input data n = 5 x1 = -2.7 x2 = -0.1 x3 = 2.9 x4 = 17.9 x5 = 117.0

  • See augmented solution. Only you can’t do anything good at the last points ... - Harry
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

 if(abs(sum)>eps) 

change to

 while(abs(sum)<eps) 

with a corresponding sum change before the start of the loop, and remove the for loop. However, you have heaped up much more than one mistake. What do you actually want to calculate? Source - like

enter image description here

So? In short, give a clear TZ, then it will be easier to jot down a normal code to you than to correct it ...

Well, once the correction is made TZ ...

 const double eps = 1e-12; int main(int argc, const char * argv[]) { int N; cout << "Number of trials? "; cin >> N; for(int n = 0; n < N; ++n) { cout << "Trial " << (n+1) << ". x = "; double x; cin >> x; cout << "x = " << x; x = 1.0 - x*x; cout << "; exp(1-x^2) = " << exp(x); int i = 1; double term = 1.0, sum = 1.0; while(abs(term) > eps) { sum += term *= x/i++; } cout << "; series = " << sum << "\n\n"; } } 

Just keep in mind that this will actually be calculated well, somewhere at |x|<=4 . Further, this method simply will not work - due to the fact that the members will first grow strongly, and only then decrease, so that the calculation error will reduce everything to nothing. In the best case, if you change the order of calculations and try to summarize taking into account all the nuances - well, maybe a little more and it will turn out to raise the range. But neither for the 17.9 requested by you, much less for 117 you will get anything sensible ...

  • thanks just what mean eps = 1e-12; ? - Dmitry Polyakov
  • "The accuracy of the calculation of ε is set in the program itself" - Harry
  • but you can not write eps = 0,25? - Dmitry Polyakov
  • Yes, even a million! With what accuracy you want - with this and calculate ... Only then do not be surprised at the results :) - Harry
  • aa is understandable, thank you very much =) - Dmitry Polyakov