IDL's function SFIT coefficients [message #88062] |
Mon, 17 March 2014 09:36  |
George[1]
Messages: 2 Registered: March 2014
|
Junior Member |
|
|
Hi Everyone,
I am stuck on what seems to be a very simple problem and I would be very grateful if I could get some assistance. That is, I want to use the coefficients (kx=coeff) from SFIT so I can have a functional form of the 2nd degree polynomial fit. However, this is not as straightforward as I thought...or I am missing something very obvious -- which I wouldn't put past me.
Note: there is another SFIT topic on this forum, but it did not help.
The problem: I have a 2d array of data that I would like to fit using SFIT. The values returned from SFIT match the data quite well (just eyeing it for now), but when I use the coefficients to reproduce the function, I get nonsense. The IDL help file says if the polynomial is 2nd order and max_degree is used then the coeffs are returned in an vector that looks like this: [k, y, y2, x, xy, x2]. Below is a block of code that shows how I used the coefficients to calculate my surface fit (s_fit).
x = [1.,2.,3.,4.,5.,6.,7.,8.,9.]
y=[100.,200.,300.,500.,1000.,2000.,3000.,5000.,10000.]
result = sfit(amp,2,kx=coeff,/max_degree) ;******amp is my 2d data array
s_fit = fltarr(n_elements(x),n_elements(y))
FOR ii=0,n_elements(y)-1 DO BEGIN
FOR kk=0,n_elements(x)-1 DO BEGIN s_fit[kk,ii]=coeff[0]+coeff[3]*x(kk)+coeff[1]*y(ii)+coeff[5] *x(kk)*x(kk)+coeff[4]*x(kk)*y(ii)+coeff[2]*y(ii)*y(ii)
ENDFOR
ENDFOR
I double and triple checked that the independent variables are not switched and that the coeffs match the IDL help file. Any suggestions would be greatly appreciated. Also, please let me know if you would like any additional information.
Regards,
George
|
|
|
Re: IDL's function SFIT coefficients [message #88063 is a reply to message #88062] |
Mon, 17 March 2014 10:12   |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
On Monday, March 17, 2014 12:36:03 PM UTC-4, George wrote:
> The problem: I have a 2d array of data that I would like to fit using SFIT. The values returned from SFIT match the data quite well (just eyeing it for now), but when I use the coefficients to reproduce the function, I get nonsense. The IDL help file says if the polynomial is 2nd order and max_degree is used then the coeffs are returned in an vector that looks like this: [k, y, y2, x, xy, x2]. Below is a block of code that shows how I used the coefficients to calculate my surface fit (s_fit).
>
>
>
> x = [1.,2.,3.,4.,5.,6.,7.,8.,9.]
>
> y=[100.,200.,300.,500.,1000.,2000.,3000.,5000.,10000.]
>
> result = sfit(amp,2,kx=coeff,/max_degree) ;******amp is my 2d data array
...
> FOR kk=0,n_elements(x)-1 DO BEGIN s_fit[kk,ii]=coeff[0]+coeff[3]*x(kk)+coeff[1]*y(ii)+coeff[5] *x(kk)*x(kk)+coeff[4]*x(kk)*y(ii)+coeff[2]*y(ii)*y(ii)
By default, SFIT assumes that X and Y are regularly sampled. Your Y values are not regularly sampled. I guess you need to use the /IRREGULAR keyword for that.
Craig
|
|
|
Re: IDL's function SFIT coefficients [message #88065 is a reply to message #88063] |
Mon, 17 March 2014 10:48  |
George[1]
Messages: 2 Registered: March 2014
|
Junior Member |
|
|
On Monday, March 17, 2014 12:12:33 PM UTC-5, Craig Markwardt wrote:
> On Monday, March 17, 2014 12:36:03 PM UTC-4, George wrote:
>
>> The problem: I have a 2d array of data that I would like to fit using SFIT. The values returned from SFIT match the data quite well (just eyeing it for now), but when I use the coefficients to reproduce the function, I get nonsense. The IDL help file says if the polynomial is 2nd order and max_degree is used then the coeffs are returned in an vector that looks like this: [k, y, y2, x, xy, x2]. Below is a block of code that shows how I used the coefficients to calculate my surface fit (s_fit).
>
>>
>
>>
>
>>
>
>> x = [1.,2.,3.,4.,5.,6.,7.,8.,9.]
>
>>
>
>> y=[100.,200.,300.,500.,1000.,2000.,3000.,5000.,10000.]
>
>>
>
>> result = sfit(amp,2,kx=coeff,/max_degree) ;******amp is my 2d data array
>
> ...
>
>> FOR kk=0,n_elements(x)-1 DO BEGIN s_fit[kk,ii]=coeff[0]+coeff[3]*x(kk)+coeff[1]*y(ii)+coeff[5] *x(kk)*x(kk)+coeff[4]*x(kk)*y(ii)+coeff[2]*y(ii)*y(ii)
>
>
>
> By default, SFIT assumes that X and Y are regularly sampled. Your Y values are not regularly sampled. I guess you need to use the /IRREGULAR keyword for that.
>
>
>
> Craig
Perfect! I misunderstood the meaning of /IRREGULAR, but when I read your post it "clicked." Thank you very much Craig.
On a side note, I should also say thank you for your MPFIT routines -- they've been a big help in other analyses.
Regards,
George
|
|
|