testing lsqcurvefit

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

testing lsqcurvefit

RICHARD Dominique
Hello,
I wanted to test the function lsqcurvefit (of the optim package) bu when I tried to run the example found at the bottom of the Octave-Forge page, I got an error : "error: 'p' undefined near line 4 column 9".
What's wrong with this example ?

  %% Example for user specified Jacobian.
  %% model function:
  function [F,J] = myfun (p, x)
    F = p(1) * exp (-p(2) * x);
    if nargout > 1
      J = [exp(- p(2) * x), - p(1) * x .* exp(- p(2) * x)];
    endif
  endfunction

  %% independents
  x = [1:10:100]';
  %% observed data
  y =[9.2160e-001, 3.3170e-001, 8.9789e-002, 2.8480e-002, 2.6055e-002,...
     8.3641e-003,  4.2362e-003,  3.1693e-003,  1.4739e-004,  2.9406e-004]';
  %% initial values:
  p0=[0.8; 0.05];
  %% bounds
  lb=[0; 0]; ub=[];
  %% Jacobian setting
  opts = optimset ("Jacobian", "on")

  [c, resnorm, residual, flag, output, lambda, jacob] = ...
      lsqcurvefit (@ (varargin) myfun(varargin{:}), p0, x, y, lb,  ub, opts)



--
Dominique RICHARD
Laboratoire de Génie des Procédés Catalytiques - CNRS-CPE Lyon
3, rue Victor Grignard F-69616 Villeurbanne cedex
Tél. (33) 4 72 43 17 52 - Fax (33) 4 72 43 16 73
_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: testing lsqcurvefit

siko1056
RICHARD Dominique wrote
Hello,
I wanted to test the function lsqcurvefit (of the optim package) bu when
I tried to run the example found at the bottom of the Octave-Forge page,
I got an error : "error: 'p' undefined near line 4 column 9".
What's wrong with this example ?

  %% Example for user specified Jacobian.
  %% model function:
  function [F,J] = myfun (p, x)
    F = p(1) * exp (-p(2) * x);
    if nargout > 1
      J = [exp(- p(2) * x), - p(1) * x .* exp(- p(2) * x)];
    endif
  endfunction

  %% independents
  x = [1:10:100]';
  %% observed data
  y =[9.2160e-001, 3.3170e-001, 8.9789e-002, 2.8480e-002, 2.6055e-002,...
     8.3641e-003,  4.2362e-003,  3.1693e-003,  1.4739e-004,  2.9406e-004]';
  %% initial values:
  p0=[0.8; 0.05];
  %% bounds
  lb=[0; 0]; ub=[];
  %% Jacobian setting
  opts = optimset ("Jacobian", "on")

  [c, resnorm, residual, flag, output, lambda, jacob] = ...
      lsqcurvefit (@ (varargin) myfun(varargin{:}), p0, x, y, lb,  ub, opts)




-- **
Dominique RICHARD
Laboratoire de Génie des Procédés Catalytiques - CNRS-CPE Lyon
3, rue Victor Grignard F-69616 Villeurbanne cedex
Tél. (33) 4 72 43 17 52 - Fax (33) 4 72 43 16 73
Hello Dominique Richard,

The error is subtle. I guess you put the code in some file and executed it. Doing this you defined a function file, not a script file, as one might expect. Therefore Octave asks you to provide the "p" and "x", and the code after "endfunction" would be ignored. The reason is, that the first non-comment keyword is "function" in the third line (see https://www.gnu.org/software/octave/doc/interpreter/Script-Files.html for details). To fix this, just write something like `disp("My cool example")` or alike before the "function" keyword, then you defined a script file.

It works for the package maintainer as demo function, as the code appears in another context there.

Maybe you report this as a documentation bug?

HTH,
Kai
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: testing lsqcurvefit

Olaf Till-2
On Thu, Apr 06, 2017 at 05:09:52AM -0700, siko1056 wrote:

> Hello Dominique Richard,
>
> The error is subtle. I guess you put the code in some file and executed it.
> Doing this you defined a function file, not a script file, as one might
> expect. Therefore Octave asks you to provide the "p" and "x", and the code
> after "endfunction" would be ignored. The reason is, that the first
> non-comment keyword is "function" in the third line (see
> https://www.gnu.org/software/octave/doc/interpreter/Script-Files.html for
> details). To fix this, just write something like `disp("My cool example")`
> or alike before the "function" keyword, then you defined a script file.
>
> It works for the package maintainer as demo function, as the code appears in
> another context there.
>
> Maybe you report this as a documentation bug?
Thanks for noticing, have changed order of instructions in the demos
of the two concerned functions to avoid this problem.

Olaf

--
public key id EAFE0591, e.g. on x-hkp://pool.sks-keyservers.net

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

signature.asc (836 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: testing lsqcurvefit

RICHARD Dominique
In reply to this post by RICHARD Dominique
@Kai, thank you for your clear explanation.
I tried 'demo lsqcurvefit' and it works fine

@Olaf, thank you for correcting the demos.

-- 
Dominique RICHARD
Laboratoire de Génie des Procédés Catalytiques - CNRS-CPE Lyon
3, rue Victor Grignard F-69616 Villeurbanne cedex 
Tél. (33) 4 72 43 17 52 - Fax (33) 4 72 43 16 73

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Loading...