mirror of
https://github.com/aportelli/LatAnalyze.git
synced 2025-06-19 07:47:05 +01:00
big update: first fit implementation
This commit is contained in:
@ -16,6 +16,7 @@ endif
|
||||
|
||||
noinst_PROGRAMS = \
|
||||
exCompiledDoubleFunction\
|
||||
exFit \
|
||||
exMat \
|
||||
exMathInterpreter \
|
||||
exMin \
|
||||
@ -26,6 +27,10 @@ exCompiledDoubleFunction_SOURCES = exCompiledDoubleFunction.cpp
|
||||
exCompiledDoubleFunction_CFLAGS = -g -O2
|
||||
exCompiledDoubleFunction_LDFLAGS = -L../latan/.libs -llatan
|
||||
|
||||
exFit_SOURCES = exFit.cpp
|
||||
exFit_CFLAGS = -g -O2
|
||||
exFit_LDFLAGS = -L../latan/.libs -llatan
|
||||
|
||||
exMat_SOURCES = exMat.cpp
|
||||
exMat_CFLAGS = -g -O2
|
||||
exMat_LDFLAGS = -L../latan/.libs -llatan
|
||||
|
51
examples/exFit.cpp
Normal file
51
examples/exFit.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <latan/MinuitMinimizer.hpp>
|
||||
#include <latan/Plot.hpp>
|
||||
#include <latan/RandGen.hpp>
|
||||
#include <latan/XYStatData.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace Latan;
|
||||
|
||||
const Index nPoint = 20;
|
||||
const double exactPar[2] = {0.5,5.0}, dx = 10.0/static_cast<double>(nPoint);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// generate fake data
|
||||
XYStatData data(nPoint, 1, 1);
|
||||
RandGen rg;
|
||||
double x_k;
|
||||
auto f = [](const double x[1], const double p[2])
|
||||
{return p[1]*exp(-x[0]*p[0]);};
|
||||
|
||||
for (Index k = 0; k < nPoint; ++k)
|
||||
{
|
||||
x_k = k*dx;
|
||||
data.x(0, k)(0, 0) = x_k;
|
||||
data.y(0, k)(0, 0) = f(&x_k, exactPar) + rg.gaussian(0.0, 0.1);
|
||||
}
|
||||
data.yyVar(0, 0).diagonal() = DMat::Constant(nPoint, 1, 0.1*0.1);
|
||||
data.assumeXExact(0);
|
||||
|
||||
// fit
|
||||
DVec init = DVec::Constant(2, 0.5);
|
||||
|
||||
DoubleModel model(1, 2, f);
|
||||
FitResult p;
|
||||
MinuitMinimizer minimizer;
|
||||
|
||||
data.fitAllPoints();
|
||||
p = data.fit(model, minimizer, init, true, Minimizer::Verbosity::Debug);
|
||||
cout << "a= " << p(0) << " b= " << p(1)
|
||||
<< " chi^2/ndof= " << p.getChi2PerDof() << endl;
|
||||
|
||||
// plot result
|
||||
Plot plot;
|
||||
|
||||
plot << LogScale(Axis::y) << PlotData(data);
|
||||
plot.display();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Reference in New Issue
Block a user