help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: testing lsqcurvefit


From: siko1056
Subject: Re: testing lsqcurvefit
Date: Thu, 6 Apr 2017 05:09:52 -0700 (PDT)

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



--
View this message in context: 
http://octave.1599824.n4.nabble.com/testing-lsqcurvefit-tp4682759p4682761.html
Sent from the Octave - General mailing list archive at Nabble.com.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]