~ubuntu-branches/ubuntu/maverick/scidavis/maverick

« back to all changes in this revision

Viewing changes to scidavis/src/NonLinearFit.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ruben Molina
  • Date: 2009-09-06 11:34:04 UTC
  • Revision ID: james.westby@ubuntu.com-20090906113404-4awaey82l3686w4q
Tags: upstream-0.2.3
ImportĀ upstreamĀ versionĀ 0.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    File                 : NonLinearFit.cpp
 
3
    Project              : SciDAVis
 
4
    --------------------------------------------------------------------
 
5
    Copyright            : (C) 2006 by Ion Vasilief, Tilman Benkert
 
6
    Email (use @ for *)  : ion_vasilief*yahoo.fr, thzs*gmx.net
 
7
    Description          : NonLinearFit class
 
8
 
 
9
 ***************************************************************************/
 
10
 
 
11
/***************************************************************************
 
12
 *                                                                         *
 
13
 *  This program is free software; you can redistribute it and/or modify   *
 
14
 *  it under the terms of the GNU General Public License as published by   *
 
15
 *  the Free Software Foundation; either version 2 of the License, or      *
 
16
 *  (at your option) any later version.                                    *
 
17
 *                                                                         *
 
18
 *  This program is distributed in the hope that it will be useful,        *
 
19
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 
20
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 
21
 *  GNU General Public License for more details.                           *
 
22
 *                                                                         *
 
23
 *   You should have received a copy of the GNU General Public License     *
 
24
 *   along with this program; if not, write to the Free Software           *
 
25
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
 
26
 *   Boston, MA  02110-1301  USA                                           *
 
27
 *                                                                         *
 
28
 ***************************************************************************/
 
29
#include "NonLinearFit.h"
 
30
#include "MyParser.h"
 
31
#include "fit_gsl.h"
 
32
 
 
33
#include <QMessageBox>
 
34
 
 
35
        NonLinearFit::NonLinearFit(ApplicationWindow *parent, Graph *g)
 
36
: Fit(parent, g)
 
37
{
 
38
        init();
 
39
}
 
40
 
 
41
        NonLinearFit::NonLinearFit(ApplicationWindow *parent, Graph *g, const QString& curveTitle)
 
42
: Fit(parent, g)
 
43
{
 
44
        init();
 
45
        setDataFromCurve(curveTitle);
 
46
}
 
47
 
 
48
        NonLinearFit::NonLinearFit(ApplicationWindow *parent, Graph *g, const QString& curveTitle, double start, double end)
 
49
: Fit(parent, g)
 
50
{
 
51
        init();
 
52
        setDataFromCurve(curveTitle, start, end);
 
53
}
 
54
 
 
55
void NonLinearFit::init()
 
56
{
 
57
        setName(tr("NonLinear"));
 
58
        d_formula = QString::null;
 
59
        d_f = user_f;
 
60
        d_df = user_df;
 
61
        d_fdf = user_fdf;
 
62
        d_fsimplex = user_d;
 
63
        d_explanation = tr("Non-linear");
 
64
        d_init_err = false;
 
65
}
 
66
 
 
67
void NonLinearFit::setFormula(const QString& s)
 
68
{
 
69
        d_formula = s;
 
70
}
 
71
 
 
72
void NonLinearFit::setParametersList(const QStringList& lst)
 
73
{
 
74
        if ((int)lst.count() < 1)
 
75
        {
 
76
                QMessageBox::critical((ApplicationWindow *)parent(), tr("Fit Error"),
 
77
                                tr("You must provide a list containing at least one parameter for this type of fit. Operation aborted!"));
 
78
                d_init_err = true;
 
79
                return;
 
80
        }
 
81
 
 
82
        d_init_err = false;
 
83
        d_param_names = lst;
 
84
 
 
85
        if (d_p > 0)
 
86
        {//free previously allocated memory
 
87
                gsl_vector_free(d_param_init);
 
88
                gsl_matrix_free (covar);
 
89
                delete[] d_results;
 
90
        }
 
91
 
 
92
        d_p = (int)lst.count();
 
93
    d_min_points = d_p;
 
94
        d_param_init = gsl_vector_alloc(d_p);
 
95
        gsl_vector_set_all (d_param_init, 1.0);
 
96
 
 
97
        covar = gsl_matrix_alloc (d_p, d_p);
 
98
        d_results = new double[d_p];
 
99
 
 
100
        for (int i=0; i<d_p; i++)
 
101
                d_param_explain << "";
 
102
}
 
103
 
 
104
void NonLinearFit::calculateFitCurveData(double *par, double *X, double *Y)
 
105
{
 
106
    for (int i=0; i<d_p; i++)
 
107
        d_script->setDouble(par[i], d_param_names[i]);
 
108
 
 
109
    if (d_gen_function) {
 
110
        double X0 = d_x[0];
 
111
        double step = (d_x[d_n-1]-X0)/(d_points-1);
 
112
        for (int i=0; i<d_points; i++) {
 
113
            X[i] = X0+i*step;
 
114
            d_script->setDouble(X[i], "x");
 
115
            Y[i] = d_script->eval().toDouble();
 
116
        }
 
117
    } else {
 
118
        for (int i=0; i<d_points; i++) {
 
119
            X[i] = d_x[i];
 
120
            d_script->setDouble(X[i], "x");
 
121
            Y[i] = d_script->eval().toDouble();
 
122
        }
 
123
    }
 
124
}