I am porting a liquid chromatography simulator from MS-Excel (it works well) to Delphi so I can have an executable I can share with students and working chromatographers. The NormDist
function is central to that task. It seems that there is no Normal Distribution function (NormDist) native to Delphi.
Can anyone point me in the right direction?
CodePudding user response:
Since I'm not that deep into mathematics I simply used the keywords NormDist Excel Delphi
on a search engine, which led me to this unit having exactly the same function with the same parameters as in Excel. Better download all the units, since at least 2 others are needed.
An amalgamation of all needed code would be:
const
sqrt2pi = 2.5066282746310005; {sqrt(2*pi)}
function Erfc(X : Single) : Single;
var
t, z, ans : Double;
begin
z := abs(X);
t := 1.0/(1.0 0.5*z);
ans := t*exp(-z*z-1.26551223 t*(1.00002368 t*(0.37409196 t*(0.09678418
t*(-0.18628806 t*(0.27886807 t*(-1.13520398 t*(1.48851587
t*(-0.82215223 t*0.17087277)))))))));
if (X >= 0.0) then
Result := ans
else
Result := 2.0-ans;
end;
function NormSDist(Z : Single) : Single;
const
sqrt2 = 1.41421356237310;
begin
Result := 1.0-0.5*Erfc(Z/sqrt2);
end;
function NormDist(X, Mean, StandardDev : Single;
Cumulative : Boolean) : Single;
var
Z : Extended;
begin
if (StandardDev <= 0) then
raise Exception.Create('Invalid parameter');
Z := (X-Mean)/StandardDev;
if (Cumulative) then
Result := NormSDist(Z)
else
Result := exp(-Z*Z/2.0)/(StandardDev*sqrt2pi);
end;
CodePudding user response:
Also, http://www.lohninger.com/sdlindex.html has a stat package that has often provided me with what I need.