help-octave
[Top][All Lists]
Advanced

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

Re: Custom jacobian (dfdp) in nonlin_curvefit - calling parameters?


From: Olaf Till
Subject: Re: Custom jacobian (dfdp) in nonlin_curvefit - calling parameters?
Date: Sat, 16 Nov 2019 08:40:03 +0100
User-agent: NeoMutt/20170113 (1.7.2)

On Fri, Nov 15, 2019 at 02:54:22PM +0100, Pavel Hofman wrote:
> Hi,
> 
> I am trying to find out, if providing custom jacobian results in faster
> execution than the default real finite differences. However, I got stuck at
> defining the custom dfdp function. Please is there any example which
> involves multiple independent values?
> 
> My case is fitting sum of two sine functions at vector of times t to
> observed values y. The problem is I do not know how the custom dfdp function
> should look like, with what params it is being called. In my code below my
> dfdp (f_dfdp) gets called with row of times (t) which I cannot convert to
> the jacobian matrix. The dfdp description in nonlin_residmin talks about
> some structures, but I was not able to find any example.

The interface of your functions and the way you call nonlin_curvefit
seem ok. The code inside f_dfdp() doesn't seem ok. I don't think the
documentation of the optim package contains an example for explicitly
computing a Jacobian with m-code (because this is not specific to the
optim package). But in the code of 'optim_problems.m' there is an
example:

  ret.curve.schittkowski_327.dfdp = ...
      @ (x, p) [1 + exp(-p(2) * (x - 8)), ...
                (p(1) + .49) * (8 - x) .* exp(-p(2) * (x - 8))];

Note that 'x' (or 't', in your code) is a column vector.

Olaf

> As of the unit test in nonlin_curfit.m (chittkowski_327) - I was able to
> find the commit diff but this case again looks like a single independent
> value is being used.
> 
> I very much appreciate any help, thanks a lot.
> 
> Best regards,
> 
> Pavel.
> 
> 
> t = 0:100;
> 
> function y = f_calc(f1, a1, s1, f2, a2, s2, t)
>   persistent PI2 = 2 * pi;
> 
>   y = a1 * cos(PI2 * f1 * t + s1) + a2 * cos(PI2 * f2 * t + s2);
> endfunction
> 
> function J = f_dfdp(f1, a1, s1, f2, a2, s2, t)
>   persistent PI2 = 2 * pi;
> 
>     J = [-PI2 * a1 * t * sin(PI2 * f1  * t + s1) , cos(PI2 * f1 * t + s1) ,
> -a1  * sin(PI2 * f1 * t + s1), -PI2 * a2 * t * sin(PI2 * f2 * t + s2),
> cos(PI2 * f2 * t + s2), -a2 * sin(PI2 * f2 * t + s2)];
> endfunction
> 
> 
> f = @(p, t) f_calc(p(1), p(2), p(3), p(4), p(5), p(6), t);
> 
> my_dfdp= @(p, t) f_dfdp(p(1), p(2), p(3), p(4), p(5), p(6), t);
> 
> settings = optimset ('dfdp', my_dfdp, 'Display','iter');
> 
> 
> init = [measF1; measA1; measPhase1;measF2; measA2; measPhase2];
> 
> 
> [p, model_values, cvg, outp] = nonlin_curvefit(f, init, t, y, settings);
> 
> 
> 
> 
> 

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

Attachment: signature.asc
Description: PGP signature


reply via email to

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