EXCEL VBA integration by part

hafizahzulkipli88

New Member
Joined
Jan 26, 2017
Messages
2
Hi. I try to integrate this function but I'm not sure if what I'm doing is right or wrong.
I appreciate if someone can help. The function follow gamma distribution.

2h3d8gg.jpg



Code:
Sub Calculateloss()
'Numerical integration accuracy
Dim dx As Double
Dim qOpt As Double
Dim dx As Double
Dim k As Integer
Dim beta As Double

'Parameter values
k=2
beta=143
dx = 0.001
qOpt=550


'Variables to calculate results
Dim I1 As Double 'First part of Integral
I1 = 0
Dim I2 As Double 'Second
I2 = 0


'Variable x
Dim x As Double
Dim f_x As Double
Dim f_xplusdx As Double


For x = 0 To (qOpt - dx) Step dx
    f_x = WorksheetFunction.Gamma_Dist(x, k, beta, False)
    f_xplusdx = WorksheetFunction.Gamma_Dist(x + dx, k, beta, False)
    I1 = I1 + 0.5 * (x * f_x + (x + dx) * f_xplusdx) * dx
    I2 = I2 + 0.5 * (f_x + f_xplusdx) * dx
Next x


'Expected loss
Dim loss As Double
loss = (Av - qOpt + qOpt * I2 - I1)

'Output
Cells(2, 1) = loss

End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,223,881
Messages
6,175,159
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top