Hi,

This is just a problem of variable scope, "g" is used internally by datafit, likely. All user parameters or functions used in G should be given by using a list, i.e. :

function  e=G(p, z, g)
   option  =  1
   select  option
   case  1
      gg  =  p(1)  +  p(2)*abs(z(1)).^p(3)  -  p(4)*exp(-p(5)*z(1).^2);
      e  =  z(2)  -  gg;
   case  2
      e  =  z(2)  -  g(z(1),  p)
   end
endfunction

// 4 Parameter algorithm adjustment
Z = [logf0 - 6.2515185; Te]; po = [0.005 -0.00013 4 0.01 1.7]
tic
[P,  err]  =  datafit(list(G,g),  Z,  po)
toc

S.

Le 09/09/2021 à 12:23, Jakub Kopac a écrit :
Hello,

looks like a conflict with internals or bug,
changing the function name of "g" will solve the problem.

Best regards

J.K.


st 8. 9. 2021 o 0:18 Federico Miyara <fmiy...@fceia.unr.edu.ar <mailto:fmiy...@fceia.unr.edu.ar>> napísal(a):


    Dear all,

    I have a problem using datafit() to adjust the parameters of a
    model. The script below has four parts. The first one actually is
    a synthesis of the data I'm working with. It just creates the data
    (logf0, Te) to model.

    The second part introduces the function g(x, p) to model the data.

    Part three creates the "gap" function required for datafit(). It
    does so in two alternative ways selected with the variable option.
    If option = 1, the definition of the gap function is given inside
    the body of that function. If option = 2, it uses the model
    function previously created.

    Finally, part 4 addresses the fitting procedure.

    My problem is that with option = 1, it works fine, but when option
    = 2 it halts with the following error:

        --> [P, err] = datafit(G, Z, po)
        at line     8 of function G
        at line    16 of function costf
        at line   -12 of function optim
        at line   243 of function datafit ( C:\Program
        Files\scilab-6.1.1\modules\optimization\macros\datafit.sci
        line 255 )

        Invalid index.
        --> toc
         ans  =
           0.6467990

    The normal time to do the full fitting is about 40 s. The time of
    the aborted process is 0.64 s, long enough to have done some
    iterations, which is baffling. I tried to see inside the datafit()
    source but frankly I don't understand what may be happening.

    May be somebody with more experience in the use of this function
    can give a hint on what's going on...

    Regards,

    Federico Miyara

    // 1 Data to approximate (Te = F(logf0))
    logf  =  linspace(2.3025851,  9.9010847,  7)
    Telogf  =  [  0.0446706   0.0225414   0.0147556   0.0055733   0.010994  ...
                0.0189552   0.0277157]
    d   =      [-0.0338451  -0.0073327  -0.0076891  -0.0021065   0.0072043  ...
                0.0049894   0.0124498]
    logf0  =  log(10*10.^(0:0.005:3.3))
Te = interp(logf0, logf, Telogf, d); // 2 Analytic model
    function  y=g(x, p)
        y  =  p(1)  +  p(2)*abs(x).^p(3)  -  p(4)*exp(-p(5)*x.^2);
    endfunction

    // 3 Function to minimize
    // (Gap or difference between data and model)
    function  e=G(p, z)
        option  =  1
        select  option
        case  1
           gg  =  p(1)  +  p(2)*abs(z(1)).^p(3)  -  p(4)*exp(-p(5)*z(1).^2);
           e  =  z(2)  -  gg;
        case  2
           e  =  z(2)  -  g(z(1),  p)
        end
    endfunction

    // 4 Parameter algorithm adjustment
Z = [logf0 - 6.2515185; Te]; po = [0.005 -0.00013 4 0.01 1.7]
    tic
    [P,  err]  =  datafit(G,  Z,  po)
    toc


    
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient>
        Libre de virus. www.avast.com
    
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient>


    <#m_2079245045849932327_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
    _______________________________________________
    users mailing list
    users@lists.scilab.org <mailto:users@lists.scilab.org>
    http://lists.scilab.org/mailman/listinfo/users
    <http://lists.scilab.org/mailman/listinfo/users>


_______________________________________________
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users

--
Stéphane Mottelet
Ingénieur de recherche
EA 4297 Transformations Intégrées de la Matière Renouvelable
Département Génie des Procédés Industriels
Sorbonne Universités - Université de Technologie de Compiègne
CS 60319, 60203 Compiègne cedex
Tel : +33(0)344234688
http://www.utc.fr/~mottelet

_______________________________________________
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users

Reply via email to