T
T
tr1ck2014-07-13 21:21:18
C++ / C#
tr1ck, 2014-07-13 21:21:18

Why does the program loop (easy)?

Why is the program looping? Everything seems to be correct. The integral is considered by the trapezoid method with an accuracy of E=10^(-9).
#include "stdafx.h"
#include
#include
#include
using namespace std;
double f(double x)
{
return 2 * x / sqrt (1-x*x*x*x);
}
int _tmain(int argc, _TCHAR* argv[])
{
double a = 0, b = 0.9, n = 4, S = 0, h, integ, IntegPrev;
h = (b - a) / n;
for (int i = 1; i <= (n - 1); i++)
S += f(a + h*i);
integ = h * ((f(a) + f(b)) / 2 + S);
//calculate the first integral for n=4
do {
IntegPrev = integ;
n += 1;
h = (b - a) / n;
for (int i = 1; i <= (n - 1); i++)
S += f(a + h*i);
integ = h * ((f(a) + f(b)) / 2 + S);
} while (abs(IntegPrev - integ) > 0.000000001);
cout <<"qq" << integ;
getchar();
return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Misha Krinkin, 2014-07-13
@tr1ck

for (int i = 1; i <= (n - 1); i++)
S += f(a + h*i);

you forgot to reset S.
UPD. Paste the code as code, not as text - don't make a mess!!!!!!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question