<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <br>
    <font face="Courier New">Dear all,<br>
      <br>
      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.<br>
      <br>
      The second part introduces the function g(x, p) to model the data.<br>
      <br>
      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.<br>
      <br>
      Finally, part 4 addresses the fitting procedure.<br>
      <br>
      My problem is that with option = 1, it works fine, but when option
      = 2 it halts with the following error:<br>
      <br>
    </font>
    <blockquote><font face="Courier New">--> [P, err] = datafit(G, Z,
        po)</font><br>
      <font face="Courier New">at line     8 of function G       </font><br>
      <font face="Courier New">at line    16 of function costf   </font><br>
      <font face="Courier New">at line   -12 of function optim   </font><br>
      <font face="Courier New">at line   243 of function datafit (
        C:\Program
        Files\scilab-6.1.1\modules\optimization\macros\datafit.sci line
        255 )</font><br>
      <br>
      <font face="Courier New">Invalid index.</font><br>
      <font face="Courier New">--> toc</font><br>
      <font face="Courier New"> ans  =</font><br>
      <font face="Courier New">   0.6467990  </font><br>
      <br>
    </blockquote>
    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.<br>
    <br>
    May be somebody with more experience in the use of this function can
    give a hint on what's going on...<br>
    <br>
    Regards,<br>
    <br>
    Federico Miyara<br>
     <br>
    <pre style="font-family:Monospaced;font-style:normal;font-size:16.0;"><span style="color:rgb(51,153,6);font-style:italic;">// 1 Data to approximate (Te = F(logf0))</span>
<span style="color:rgb(0,0,0);">logf</span> <span style="color:rgb(92,92,92);">=</span> <span style="color:rgb(50,185,185);">linspace</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">2.3025851</span><span style="color:rgb(0,0,0);">,</span> <span style="color:rgb(188,143,143);">9.9010847</span><span style="color:rgb(0,0,0);">,</span> <span style="color:rgb(188,143,143);">7</span><span style="color:rgb(74,85,219);">)</span>
<span style="color:rgb(0,0,0);">Telogf</span> <span style="color:rgb(92,92,92);">=</span> <span style="color:rgb(74,85,219);">[</span> <span style="color:rgb(188,143,143);">0.0446706</span>  <span style="color:rgb(188,143,143);">0.0225414</span>  <span style="color:rgb(188,143,143);">0.0147556</span>  <span style="color:rgb(188,143,143);">0.0055733</span>  <span style="color:rgb(188,143,143);">0.010994</span> <span style="color:rgb(255,170,0);">...</span>
           <span style="color:rgb(188,143,143);">0.0189552</span>  <span style="color:rgb(188,143,143);">0.0277157</span><span style="color:rgb(74,85,219);">]</span>
<span style="color:rgb(0,0,0);">d</span>  <span style="color:rgb(92,92,92);">=</span>     <span style="color:rgb(74,85,219);">[</span><span style="color:rgb(92,92,92);">-</span><span style="color:rgb(188,143,143);">0.0338451</span> <span style="color:rgb(92,92,92);">-</span><span style="color:rgb(188,143,143);">0.0073327</span> <span style="color:rgb(92,92,92);">-</span><span style="color:rgb(188,143,143);">0.0076891</span> <span style="color:rgb(92,92,92);">-</span><span style="color:rgb(188,143,143);">0.0021065</span>  <span style="color:rgb(188,143,143);">0.0072043</span> <span style="color:rgb(255,170,0);">...</span>
           <span style="color:rgb(188,143,143);">0.0049894</span>  <span style="color:rgb(188,143,143);">0.0124498</span><span style="color:rgb(74,85,219);">]</span>
<span style="color:rgb(0,0,0);">logf0</span> <span style="color:rgb(92,92,92);">=</span> <span style="color:rgb(50,185,185);">log</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">10</span><span style="color:rgb(92,92,92);">*</span><span style="color:rgb(188,143,143);">10.</span><span style="color:rgb(92,92,92);">^</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">0</span><span style="color:rgb(255,170,0);">:</span><span style="color:rgb(188,143,143);">0.005</span><span style="color:rgb(255,170,0);">:</span><span style="color:rgb(188,143,143);">3.3</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(74,85,219);">)</span>
<span style="color:rgb(0,0,0);">Te</span> <span style="color:rgb(92,92,92);">=</span> <span style="color:rgb(50,185,185);">interp</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(0,0,0);">logf0</span><span style="color:rgb(0,0,0);">,</span> <span style="color:rgb(0,0,0);">logf</span><span style="color:rgb(0,0,0);">,</span> <span style="color:rgb(0,0,0);">Telogf</span><span style="color:rgb(0,0,0);">,</span> <span style="color:rgb(0,0,0);">d</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(0,0,0);">;</span>           
           
<span style="color:rgb(51,153,6);font-style:italic;">// 2 Analytic model</span>
<span style="color:rgb(176,24,19);">function</span> <span style="color:rgb(131,67,16);font-weight:bold;">y</span><span style="color:rgb(92,92,92);">=</span><span style="color:rgb(0,0,0);text-decoration:underline;">g</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(131,67,16);font-weight:bold;">x</span><span style="color:rgb(0,0,0);">, </span><span style="color:rgb(131,67,16);font-weight:bold;">p</span><span style="color:rgb(74,85,219);">)</span>
   <span style="color:rgb(131,67,16);font-weight:bold;">y</span> <span style="color:rgb(92,92,92);">=</span> <span style="color:rgb(131,67,16);font-weight:bold;">p</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">1</span><span style="color:rgb(74,85,219);">)</span> <span style="color:rgb(92,92,92);">+</span> <span style="color:rgb(131,67,16);font-weight:bold;">p</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">2</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(92,92,92);">*</span><span style="color:rgb(50,185,185);">abs</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(131,67,16);font-weight:bold;">x</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(92,92,92);">.^</span><span style="color:rgb(131,67,16);font-weight:bold;">p</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">3</span><span style="color:rgb(74,85,219);">)</span> <span style="color:rgb(92,92,92);">-</span> <span style="color:rgb(131,67,16);font-weight:bold;">p</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">4</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(92,92,92);">*</span><span style="color:rgb(50,185,185);">exp</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(92,92,92);">-</span><span style="color:rgb(131,67,16);font-weight:bold;">p</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">5</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(92,92,92);">*</span><span style="color:rgb(131,67,16);font-weight:bold;">x</span><span style="color:rgb(92,92,92);">.^</span><span style="color:rgb(188,143,143);">2</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(0,0,0);">;</span>
<span style="color:rgb(176,24,19);">endfunction</span>

<span style="color:rgb(51,153,6);font-style:italic;">// 3 Function to minimize </span>
<span style="color:rgb(51,153,6);font-style:italic;">// (Gap or difference between data and model)</span>
<span style="color:rgb(176,24,19);">function</span> <span style="color:rgb(131,67,16);font-weight:bold;">e</span><span style="color:rgb(92,92,92);">=</span><span style="color:rgb(0,0,0);text-decoration:underline;">G</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(131,67,16);font-weight:bold;">p</span><span style="color:rgb(0,0,0);">, </span><span style="color:rgb(131,67,16);font-weight:bold;">z</span><span style="color:rgb(74,85,219);">)</span>
   <span style="color:rgb(0,0,0);">option</span> <span style="color:rgb(92,92,92);">=</span> <span style="color:rgb(188,143,143);">1</span>
   <span style="color:rgb(160,32,240);">select</span> <span style="color:rgb(0,0,0);">option</span>
   <span style="color:rgb(160,32,240);">case</span> <span style="color:rgb(188,143,143);">1</span>
      <span style="color:rgb(0,0,0);">gg</span> <span style="color:rgb(92,92,92);">=</span> <span style="color:rgb(131,67,16);font-weight:bold;">p</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">1</span><span style="color:rgb(74,85,219);">)</span> <span style="color:rgb(92,92,92);">+</span> <span style="color:rgb(131,67,16);font-weight:bold;">p</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">2</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(92,92,92);">*</span><span style="color:rgb(50,185,185);">abs</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(131,67,16);font-weight:bold;">z</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">1</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(92,92,92);">.^</span><span style="color:rgb(131,67,16);font-weight:bold;">p</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">3</span><span style="color:rgb(74,85,219);">)</span> <span style="color:rgb(92,92,92);">-</span> <span style="color:rgb(131,67,16);font-weight:bold;">p</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">4</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(92,92,92);">*</span><span style="color:rgb(50,185,185);">exp</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(92,92,92);">-</span><span style="color:rgb(131,67,16);font-weight:bold;">p</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">5</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(92,92,92);">*</span><span style="color:rgb(131,67,16);font-weight:bold;">z</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">1</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(92,92,92);">.^</span><span style="color:rgb(188,143,143);">2</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(0,0,0);">;</span>
      <span style="color:rgb(131,67,16);font-weight:bold;">e</span> <span style="color:rgb(92,92,92);">=</span> <span style="color:rgb(131,67,16);font-weight:bold;">z</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">2</span><span style="color:rgb(74,85,219);">)</span> <span style="color:rgb(92,92,92);">-</span> <span style="color:rgb(0,0,0);">gg</span><span style="color:rgb(0,0,0);">;</span>
   <span style="color:rgb(160,32,240);">case</span> <span style="color:rgb(188,143,143);">2</span>
      <span style="color:rgb(131,67,16);font-weight:bold;">e</span> <span style="color:rgb(92,92,92);">=</span> <span style="color:rgb(131,67,16);font-weight:bold;">z</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">2</span><span style="color:rgb(74,85,219);">)</span> <span style="color:rgb(92,92,92);">-</span> <span style="color:rgb(0,0,0);text-decoration:underline;">g</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(131,67,16);font-weight:bold;">z</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(188,143,143);">1</span><span style="color:rgb(74,85,219);">)</span><span style="color:rgb(0,0,0);">,</span> <span style="color:rgb(131,67,16);font-weight:bold;">p</span><span style="color:rgb(74,85,219);">)</span>
   <span style="color:rgb(160,32,240);">end</span>
<span style="color:rgb(176,24,19);">endfunction</span>

<span style="color:rgb(51,153,6);font-style:italic;">// 4 Parameter algorithm adjustment</span>
<span style="color:rgb(0,0,0);">Z</span> <span style="color:rgb(92,92,92);">=</span> <span style="color:rgb(74,85,219);">[</span><span style="color:rgb(0,0,0);">logf0</span> <span style="color:rgb(92,92,92);">-</span> <span style="color:rgb(188,143,143);">6.2515185</span><span style="color:rgb(0,0,0);">;</span> <span style="color:rgb(0,0,0);">Te</span><span style="color:rgb(74,85,219);">]</span><span style="color:rgb(0,0,0);">;</span> 
<span style="color:rgb(0,0,0);">po</span> <span style="color:rgb(92,92,92);">=</span> <span style="color:rgb(74,85,219);">[</span><span style="color:rgb(188,143,143);">0.005</span>  <span style="color:rgb(92,92,92);">-</span><span style="color:rgb(188,143,143);">0.00013</span>  <span style="color:rgb(188,143,143);">4</span>  <span style="color:rgb(188,143,143);">0.01</span>  <span style="color:rgb(188,143,143);">1.7</span><span style="color:rgb(74,85,219);">]</span>
<span style="color:rgb(50,185,185);">tic</span>
<span style="color:rgb(74,85,219);">[</span><span style="color:rgb(0,0,0);">P</span><span style="color:rgb(0,0,0);">,</span> <span style="color:rgb(0,0,0);">err</span><span style="color:rgb(74,85,219);">]</span> <span style="color:rgb(92,92,92);">=</span> <span style="color:rgb(0,0,0);">datafit</span><span style="color:rgb(74,85,219);">(</span><span style="color:rgb(0,0,0);text-decoration:underline;">G</span><span style="color:rgb(0,0,0);">,</span> <span style="color:rgb(0,0,0);">Z</span><span style="color:rgb(0,0,0);">,</span> <span style="color:rgb(0,0,0);">po</span><span style="color:rgb(74,85,219);">)</span>
<span style="color:rgb(50,185,185);">toc</span></pre>
  <div id="DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2"><br /> <table style="border-top: 1px solid #D3D4DE;">
        <tr>
      <td style="width: 55px; padding-top: 18px;"><a href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient" target="_blank"><img src="https://ipmcdn.avast.com/images/icons/icon-envelope-tick-round-orange-animated-no-repeat-v1.gif" alt="" width="46" height="29" style="width: 46px; height: 29px;" /></a></td>
                <td style="width: 470px; padding-top: 17px; color: #41424e; font-size: 13px; font-family: Arial, Helvetica, sans-serif; line-height: 18px;">Libre de virus. <a href="https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient" target="_blank" style="color: #4453ea;">www.avast.com</a>           </td>
        </tr>
</table>
<a href="#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2" width="1" height="1"> </a></div></body>
</html>