summaryrefslogtreecommitdiff
path: root/modules/optimization/demos/datafit/demo_datafit.sci
blob: df7221836f77ee00fd01a359da1a23992a43f14f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) ????-2008 - INRIA
// Copyright (C) 2010 - DIGITEO - Allan CORNET
//
// This file is distributed under the same license as the Scilab package.
//

function demo_datafit()

    function y = FF(x)
        // parametric function model
        y = a*(x-b)+c*x.*x;
    endfunction

    function e = G(p, z)
        // datafit external computes the error
        a = p(1),
        b = p(2),
        c = p(3),
        y = z(1),
        x = z(2),
        e = y - FF(x)
    endfunction

    // create the experimental data
    X = [];
    Y = [];
    a = 34;
    b = 12;
    c = 14;
    for x=0:.1:3, Y=[Y,FF(x)+100*(rand()-.5)];X=[X,x];end
    Z = [Y; X];
    //show the data points
    my_handle = scf(100001);
    clf(my_handle, "reset");
    demo_viewCode(SCI + "/modules/optimization/demos/datafit/demo_datafit.sci");
    plot(X, Y, "+");
    l=legend(_("Experimental data"),2);
    sleep(500);
    // solve the non linear data fitting
    [p,err] = datafit(G,Z,[3;5;10])
    // show the fitting curve
    drawlater()
    plot(X,FF(X), "r");
    delete(l);
    l = legend([_("Experimental data"); _("Fitting function")],2);
    drawnow();

endfunction