Question

I want to integrate a mathematical function (numberdensity-function) over a given size range. The function I want to integrate generally consists of fractions, exponents, the euler number and roots. What code can I use for this?

For example I have found the class "math", but I can't find any integration function within this class. Furthermore I don't know the syntax for the elements mentioned above (euler number...)

greetings streight

Was it helpful?

Solution

You might be looking for numerical integration.
This is a way to get 'close enough' to a numeric value for the integral, and is usually sufficient for most applications.

The wikipedia page links to AlgLib as an implementing library with a C++ impementation.

OTHER TIPS

This is a code I developed a while ago. It uses Simpson method.

#include <iostream>
#include <math.h>

using namespace std;

double NormSDist(double x);
template <typename T> T SimpsonMethod(T (*pfunc)(T), float a, float b,int n=100);

int main()
{
    double area=SimpsonMethod<double>(&NormSDist,-5,1.2);
    cout << area << endl;
    return 0;
}

template <typename T> T SimpsonMethod(T (*pfunc)(T), float a, float b,int n)
{
    T h,x,y,retVal;
    h=(b-a)/n;

    x=a;
    y=(*pfunc)(x);retVal=y;
    for(int i=1;i<n;i++)
    {
        x=a+i*h;
        if(i%2==0) y=2*(*pfunc)(x);
        if(i%2==1) y=4*(*pfunc)(x);
        retVal=retVal+y;
    }
    x=b;y=(*pfunc)(x);retVal=retVal+y;

    return retVal*h/3;
}

double NormSDist(double x)
{
    return 1/sqrt(2*3.1415926536)*exp(-0.5*x*x);
}

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top