Я пытаюсь найти оценку Trapezoidal Rule функции Goempertz и использовать ее для измерения разницы между ожидаемой продолжительностью жизни для 50-летнего курильщика и 50-летнего некурящего, но мой код дал мне дерьмовые ответы.Интеграция функции Goempertz в C++ glitch
Функция Goempertz для человека в возрасте до 50 лет могут быть закодированы как:
exp((-b/log(c))*pow(c,50)*(pow(c,t)-1))
где b
и c
постоянные, и мы должны интегрировать ее от 0 до бесконечности (очень большое количество), чтобы получить ожидаемая продолжительность жизни.
Для некурящего, продолжительность жизни может быть рассчитана с помощью: констант b = 0,0005, c = 1,07. И для курильщика продолжительность жизни может быть рассчитана с помощью констант b = 0,0010, c = 1,07.
const double A = 0; // lower limit of integration
const double B = 1000000000000; // Upper limit to represent infinity
const int N = 10000; //# number of steps of the approximation
double g(double b, double c, double t) //
{//b and c are constants, t is the variable of integration.
return exp((-b/log(c))*pow(c,50)*(pow(c,t)-1));
}
double trapezoidal(double Bconst, double Cconst)
{
double deltaX = (B-A)/N; //The "horizontal height" of each tiny trapezoid
double innerTrap = 0; //innerTrap is summation of terms inside Trapezoidal rule
for (int i = 0; i <= N; i++)
{
double xvalue;
if (i == 0) // at the beginning, evaluate function of innerTrap at x0=A
{
xvalue = A;
}
else if (i == N) //at the end, evaluate function at xN=B
{
xvalue = B;
}
else //in the middle terms, evaluate function at xi=x0+i(dX)
{
xvalue = A + i * deltaX;
}
if ((i == 0) || (i == N)) //coefficient is 1 at beginning and end
{
innerTrap = innerTrap + 1*g(Bconst, Cconst, xvalue);
}
else // for all other terms in the middle, has coefficient 2
{
innerTrap = innerTrap + 2*g(Bconst, Cconst, xvalue);
}
}
return (deltaX/2)*innerTrap;
}
int main()
{
cout << "years 50 year old nonsmoker lives: " << trapezoidal(0.0005,1.07) << endl;
cout << "years 50 year old smoker lives: " << trapezoidal(0.0010,1.07) << endl;
cout << "difference between life expectancies: " << trapezoidal(0.0005,1.07)-trapezoidal(0.0010,1.07) << endl;
return 0;
}
* Я не уверен, как mathjax работает в переполнении стека * Это потому, что это не так. – Borgleader
Хорошо, хорошо знать. Благодаря! – shoestringfries
@shoestringfries Вы можете указать ссылку на бумагу, из которой вы взяли формулу? – kvorobiev