B4J Question (math) calculating an integral

MegatenFreak

Active Member
Licensed User
Hello.
There's a math calculation I need to do. It begins with a function f(x) which I've already managed; however, the next step states:
"Find the integral, from -infinity to x, of the previous value." (I used the word 'infinity' instead of the actual symbol)
Is there anyway that could be done using the available math functions and libraries?
Thanks a lot in advance.
 

MegatenFreak

Active Member
Licensed User
Well, this is the whole formula:

All the stuff between the integral sign and "dt" can be easily calculated. All the variables are clear.
I realize this is not a math forum! I was just wondering if there is any way I can calculate the result of this for a given set of variables. It's something I need to calculate for a laboratory information system. It determines how accurate the results of a test are.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub AppStart (Args() As String)
    Log(CalcIntegral(-10000, 5000))
End Sub

Private Sub CalcIntegral(FromValue As Double, ToValue As Double) As Double
    Dim u As Double = 10
    Dim o As Double = 64
    Dim e2 As Double = Power(cE, -1/2)
    Dim denominator As Double = Sqrt(2 * cPI * o)
    Dim delta As Double = 0.001
    Dim Result As Double = 0 'C
    Dim t as Double
    For t = FromValue To ToValue Step delta
        Dim i As Double = Power(e2, Power((t - u) / o, 2)) / denominator
'        Log(i)
        Result = Result + i * delta
    Next
    Return Result
End Sub

Maybe I did a mistake but it looks like it converges to sqrt(o).
 
Upvote 0

MegatenFreak

Active Member
Licensed User
Wow! I did a few adjustments to fix the primary formula, and the result is actually close to the desired value, based on some examples!
You're amazing! That math is totally beyond me!
Speed could be an issue, though, especially since the 'from' value has to be really low (to simulate -infinity).
I'll try creating a ready-made EXCEL look-up table and load it into my app.
Thanks Erel.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The equation is the normal distribution curve.

Below my routine:

B4X:
'Normal distribution integral
'BeginValue and EndValue are the interval values
'Mu = mean value
'Sigma = standard deviation
'Delta = calculation interval
Private Sub CalcGaussIntegral(BeginValue As Double, EndValue As Double, Mu As Double, Sigma As Double, Delta As Double) As Double
    Private i, Integral, y0, y1 As Double
    Private Denominator As Double
  
    Delta = 0.1
    Denominator = Sqrt(2 * cPI)
    y0 = Power(cE, -(BeginValue - Mu) * (BeginValue - Mu) / 2 / Sigma / Sigma) / Sigma / Denominator
  
    For i = BeginValue + Delta To EndValue Step Delta
        y1 = Power(cE, -(i - Mu) * (i - Mu) / 2 / Sigma / Sigma) / Sigma / Denominator
        Integral = Integral + (y0 + y1) / 2 * Delta
        y0 = y1
    Next
    Return Integral
End Sub
The routine uses the trapeze integration formula.

With
CalcGaussIntegral(-5, 5, 0, 1, 0.001) you get 1, 100%
CalcGaussIntegral(-5, 0, 0, 1, 0.001) you get 0.5, 50%
CalcGaussIntegral(-1.96, 1.96, 0, 1, 0.001) you get 0.95, 95%

Maybe the calculation of the interval of confidence might be of interest for you.

Sorry, I am not as fast as Erel.

EDIT:

And the shape of the normal distribution curve and its integral drawn with xGraph.
Example corresponding to these parameters: CalcGaussIntegral(-5, 5, 0, 1, 0.001)

 
Last edited:
Upvote 0

MegatenFreak

Active Member
Licensed User
You're right, Klaus. It is indeed the normal distribution curve formula, which, strangely, is used for an entirely different purpose.
The only certified source of calculation this procedure has mentioned (in the ISO standard documents) is by using the Excel NORMDIST function. (for example, NORMDIST(3.0, 2.7, 0.2, TRUE). I got to that integral formula from microsoft office's explanation of excel formulas.
Thanks a lot Klaus. Your explanations made things clearer. I'm currently discussing it with the project's technical expert to see just how 'exact' he needs the answer to be. If going as far as 2 fractions is enough, a lookup table will do the job.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…