Test of correlation function (correl).
MGL code:
new a 100 'exp(-10*x^2)' new b 100 'exp(-10*(x+0.5)^2)' yrange 0 1 subplot 1 2 0 '_':title 'Input fields' plot a:plot b:box:axis correl r a b 'x' norm r 0 1:swap r 'x' # make it human readable subplot 1 2 1 '_':title 'Correlation of a and b' plot r 'r':axis:box line 0.5 0 0.5 1 'B|'
C++ code:
void smgl_correl(mglGraph *gr)
{
mglData a(100),b(100);
gr->Fill(a,"exp(-10*x^2)"); gr->Fill(b,"exp(-10*(x+0.5)^2)");
gr->SetRange('y',0,1);
gr->SubPlot(1,2,0,"_"); gr->Title("Input fields");
gr->Plot(a); gr->Plot(b); gr->Axis(); gr->Box();
mglData r = a.Correl(b,"x");
r.Norm(0,1); r.Swap("x"); // make it human readable
gr->SubPlot(1,2,1,"_"); gr->Title("Correlation of a and b");
gr->Plot(r,"r"); gr->Axis(); gr->Box();
gr->Line(mglPoint(0.5,0),mglPoint(0.5,1),"B|");
}