Re: mpfit of parametric data? [message #40282 is a reply to message #40281] |
Wed, 28 July 2004 20:50   |
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
|
|
|