Click here to Skip to main content
Click here to Skip to main content
Go to top

Bell Curve, Gaussian Function, or Normal Distribution

, 3 Aug 2010
Rate this:
Please Sign up or sign in to vote.
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
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#
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
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#
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)

Share

About the Author

Tim-bot

United States United States
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 3 Aug 2010
Article Copyright 2010 by Tim-bot
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid