Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Bell Curve, Gaussian Function, or Normal Distribution

5.00/5 (2 votes)
3 Aug 2010CPOL 24.8K  
Simple calculation to determin a value at a percentage along a bell curve. Also with an offcenter curve intersection thing....
The reason i'm posting this little code snippet is because i couldn't find anything like this. only math termonology which i don't understand, i just understand functions B)

at first i was trying to get a non-linear function for a volumn control, then we needed a distribution function for estimating materials we were going to need at work. so i came up with this finally.

VB.net
VB
Private Function GetBellCurvePoint(ByVal Percentage As Double, ByVal Midpoint As Double) As Double
        If Percentage > Midpoint Then
            Percentage = 1 - Percentage
            Return 1 - ((Percentage - ((1 - Percentage) * Percentage)) * (1 / (1 - Midpoint)))
        Else
            Return (Percentage - ((1 - Percentage) * Percentage)) * (1 / Midpoint)
        End If
    End Function


C#
C#
private double GetBellCurvePoint(double Percentage, double Midpoint)
{
    if (Percentage > Midpoint) {
        Percentage = 1 - Percentage;
        return 1 - ((Percentage - ((1 - Percentage) * Percentage)) * (1 / (1 - Midpoint)));
    } else {
        return (Percentage - ((1 - Percentage) * Percentage)) * (1 / Midpoint);
    }
}


just provide the percentage along the curve you want the value for and the midpoint (normally 0.5)

this next function just gives you an even distribution along the curve depending on the iterations count, and i'm calling it threshold instead of midpoint here cause it makes more sense in my application. the value part of the keyvaluepair is the difference from the previous point.

VB.net
VB
Public Function GetMatrix(ByVal Iterations As Integer, ByVal Threshold As Double) As KeyValuePair(Of Double, Double)()
        Dim MatrixList As New List(Of KeyValuePair(Of Double, Double))
        MatrixList.Add(New KeyValuePair(Of Double, Double)(0, 0))
        For i = 1 To Iterations
            Dim ThisPoint = GetBellCurvePoint(i / Iterations, Threshold)
            MatrixList.Add(New KeyValuePair(Of Double, Double)(ThisPoint, ThisPoint - MatrixList.Last.Key))
        Next
        Return MatrixList.ToArray
    End Function


C#
C#
public KeyValuePair<double, double>[] GetMatrix(int Iterations, double Threshold)
{
    List<KeyValuePair<double, double>> MatrixList = new List<KeyValuePair<double, double>>();
    MatrixList.Add(new KeyValuePair<double, double>(0, 0));
    for (i = 1; i <= Iterations; i++) {
        object ThisPoint = GetBellCurvePoint(i / Iterations, Threshold);
        MatrixList.Add(new KeyValuePair<double, double>(ThisPoint, ThisPoint - MatrixList.Last.Key));
    }
    return MatrixList.ToArray();
}


i believe this can also be used to calculate a gaussian function.
hope this helps someone that neeeded something like this!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)