help-octave
[Top][All Lists]
Advanced

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

Re: What to give as "real column vector of initial parameters" to nonlin


From: Doron Behar
Subject: Re: What to give as "real column vector of initial parameters" to nonlin_curvefit?
Date: Fri, 12 Jun 2020 21:16:14 +0300

Dimitri,

You are a savior.

   -=-
(\  _  /)
( \( )/ )
(       )
 `>   <'
 /     \
 `-._.-'

But I have some questions:

> There are multiple problems.
> 
> There are errors in the code. You seem to mix up X and Y variables in plots
> and fits.

It seemed weird to me that they got reversed - I just decided who is X
and who is Y when I plotted the raw data - no idea how I reached that
conclusion...

> You read data as integers(?) -- why? You want doubles.

Hmm OK good to know.

> Octave (and matlab) is primarily an interactive programming/developing
> environment.
> When your program crashes you still can inspect the variables left and see
> if the values

I know and it seemed that guessed_values_arr had the same content when I
used `;` and when I used `,`...

> of those variables give you some hints.
> You say "Using `;` instead of `,` doesn't work either", but the error is
> different!
> It should have given you a clue.

I saw the error was different but I couldn't figure out what to do.

> I found in general the curve fitting in "optim" package are more finicky
> than they should be.
> Out of those "leasqr" seems to work better for me than "nonlin_curvefit".
> 
> The parameters in the fit function are usually not the same parameters you
> would use in
> human definition of the function. Usually you want to make them be more
> similar to each other.

What do you mean by similar to each other?

> You should avoid divisions if possible.

You mean division of say x by a parameter right? Like I did with tau?

> In you case the function becomes:
> 
> exp(a*t)*(b*sin(w*t)+c*cos(w*t))
>
> (a,b,c,w -- fit parameters; w = 2*pi*frequency)

This is interesting - you chose to use:

b*sin(w*t) + c*cos(w*t)

And not:

j*sin(w*t +k)

While mathematically speaking (IIRC) that for every b and c, there exist
j and k such that both expressions are identical for all t...

Why does it work for the 1st option and not the 2nd?

> It looks to me it fits better if you add a small offset as well.

Hmm I tried without that offset and it seemed pretty much as good. You
are talking about 0.759 here:

> harmonic_param_func = @(t, p) (exp(-t * p(2)) .* (p(1)*sin(2*pi*p(3)*t) + 
> p(4)*cos(2*pi*p(3)*t)) + 0.759);

Right?

> Having said that, attached are a modified script and the output it produced
> for me.

Hmm

Reading your script, I had 1 more question:

Regarding:

> harmonic_param_func = @(t, p) (exp(-t * p(2)) .* (p(1)*sin(2*pi*p(3)*t) + 
> p(4)*cos(2*pi*p(3)*t)) + 0.759);

It made me notice leasqr and nonlin_curvefit expect a function object
with x and p reversed inside @(_, _) - Seems pretty confusing! Right?

Also, is it important to use `.*` and not `*` in this context?

Are there perhaps some "rules of thumb" you'd recommend to remember when
using these fitting functions?

Regards,
Doron.



reply via email to

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