comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: mpfit of parametric data?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: mpfit of parametric data? [message #40281] Wed, 28 July 2004 22:56
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
"jamiesmyth_uni@yahoo.ca" <jamiesmyth_uni@yahoo.ca> writes:

> Thanks for your reply Craig. I think I have a handle on fitting both X
> and Y but I've taken a step backward this evening. I am having a very
> difficult time fitting even one of these time series and I'm not
> entirely sure why. I've read the FAQ again...
>
> Here is a quick and dirty example of what I observe. Essentially, I am
> completely unable to fit the sine oscillation unless I start with a
> very good first guess at the parameters. In particular, I cannot seem
> to fit both the linear trend and the oscillations. Can anyone take a
> stab at why this is so?

Greetings, this is not a big surprise. As I say on the FAQ page,
choosing the initial parameters is one of the key elements in getting
a good fit. This is especially true for an oscillatory function such
as yours, which can have multiple minima. Your initial guess, P1 =
[1,1,1,1,1], is too far from the global solution, so MPFIT gets
trapped at a different local minimum.

In these types of situations, you will probably have to proceed
iteratively. For example, first removing long term trends, then
fitting periodic signals. You may have to perform a Fourier transform
to locate periodic signals and to provide a good inital estimate of
the period/frequency.

Good luck,
Craig

--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
Re: mpfit of parametric data? [message #40282 is a reply to message #40281] Wed, 28 July 2004 20:50 Go to previous message
jamiesmyth_uni@yahoo. is currently offline  jamiesmyth_uni@yahoo.
Messages: 6
Registered: July 2004
Junior Member
Thanks for your reply Craig. I think I have a handle on fitting both X
and Y but I've taken a step backward this evening. I am having a very
difficult time fitting even one of these time series and I'm not
entirely sure why. I've read the FAQ again...

Here is a quick and dirty example of what I observe. Essentially, I am
completely unable to fit the sine oscillation unless I start with a
very good first guess at the parameters. In particular, I cannot seem
to fit both the linear trend and the oscillations. Can anyone take a
stab at why this is so?

Thanks in advance.
Jamie

--- snip ---
function nutation_fm, p, X=x, Y=y, err=err, forward=fw
; linear terms + cosine w phase
model = p(0) + p(1)*x + p(2)*cos( p(3)*x + p(4) )
if keyword_set(fw) then return, model $
else return, (y-model)/err
end

;;; MAIN

; Create some realistic data (1% noise)
n = 1024 ; number of samples
t = dindgen(n) * 0.25 ; time
p0 = [3.02d, 0.0057, 0.03, 0.435, 2.3] ; emperically determined to be
ok
data = nutation_fm(p0,x=t,/forward) + 0.01 * randomn(seed, n, /double)

; Try and fit to the nutation model
err = dblarr(n)+0.02d ; lax error
p1 = [1.0d, 1.0, 1.0, 1.0, 1.0] ; initial guess parameters
f = {x:t, y:data, err:err}
;p = mpfit('nutation_fm', p0, functargs=f) ; use 'correct' first guess
p = mpfit('nutation_fm', p1, functargs=f)

; Plot the result
model = nutation_fm(p,x=t,/forward)
plot, (data-model)/err

End--- snip ---

> Greetings,
>
> This is actually really easy.
>
> You are actually trying to fit two functions simultaneously, X and Y.
> On the other hand, you could consider this to be one *single*
function
> which has twice as many elements.
>
> Your independent variable is still T, but your new function would be
> the concatenation of X and Y. For example,
>
> U = [X, Y]
>
> You do the same for your error or weight values. Within your
> function, you need to perform the same operations to join the model X
> and Y values into a single model function.
>
> You may be worried that U and T are not of the same size, but that
> doesn't matter! Formally you don't even need an independent variable
> at all. It's just there as a convenience.
>
> That's it. Happy fitting!
> Craig
Re: mpfit of parametric data? [message #40289 is a reply to message #40282] Wed, 28 July 2004 13:05 Go to previous message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
"jamiesmyth_uni@yahoo.ca" <jamiesmyth_uni@yahoo.ca> writes:
> I suppose this is really a question for Craig but I figure here is as
> good a place to ask as any... Does anyone know how I can go about
> fitting parametric data using MPFIT? I have done a fair bit of 1d
> fitting with mpfit (MPFITEXPR) but I'm really stumped on this one. I
> want to fit to the following parametric parameterisation:
>
> x = (a0+a1*t) + sin(a2*t+phase)
> y = (b0+b1*t) + cos(b2*t+phase)
>
> where, a0, a1, a3, b0, b1, b2 and phase are all fit parameters.
>
> The intention is to try and fit the motion of a spinning top that
> precesses. I have very long running (but noisy) time series data for
> the x and y values. Alternatively, you can think of me having x(t) and
> y(t) sampled at identical times. I am mainly interested in the phase
> parameter.
>
> This is proving considerably more difficult than I expected it to be!

Greetings,

This is actually really easy.

You are actually trying to fit two functions simultaneously, X and Y.
On the other hand, you could consider this to be one *single* function
which has twice as many elements.

Your independent variable is still T, but your new function would be
the concatenation of X and Y. For example,

U = [X, Y]

You do the same for your error or weight values. Within your
function, you need to perform the same operations to join the model X
and Y values into a single model function.

You may be worried that U and T are not of the same size, but that
doesn't matter! Formally you don't even need an independent variable
at all. It's just there as a convenience.

That's it. Happy fitting!
Craig

--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Import Arc Export File to IDLffShape
Next Topic: Translate characters/string size to data/normal coordinates?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 14:52:52 PDT 2025

Total time taken to generate the page: 0.00435 seconds