PDA

View Full Version : CURVEFIT function yielding -1.#IND0

green12
02-14-2009, 11:15 AM
I am trying to fit some data with the CURVEFIT function. Yet, when I run it my "resultsLarge" array yields only values like so: -1.#IND0 .

Here is what I am doing. di2 and largepixel are the same size and type.
p=[1,1,1,1,1,1]
wt = [1,1,1,1,1,1,1,1,1,1]
resultsLarge = CURVEFIT(di2, largepixel,wt,p)

Any guesses here? Thank for your time!

allan
02-16-2009, 05:59 PM
it looks like the algorithm produced a sqrt(-1) which can
occur if the problem is ill-posed and/or represented with
data of insufficient precision, so the easiest potential
workaround would be to ensure that both the input data
and the objective function are represented with double
precision (this will also ensure that the algorithm operates
in double precision); if this does not fix the problem, then
you might want to try a more robust regression routine
from the statistics toolkit; there are several of these but
the most general is called nonlinregress; it provides
various options for dealing with difficult problems,
including the ability to supply an analytical derivative;

green12
02-18-2009, 10:44 AM
p=[1,1,1,0,0,0,0,0,0]
p=DOUBLE(p)
wt = [1.,1.,1.,1.,1.,1.,1.,1.,1.,1.]
wt=double(wt)
di2=double(di2)
largepixel=double(largepixel)
resultsLarge = CURVEFIT(di2, largepixel,wt,p)

info, p
info, wt
info, di2
info, largepixel
print, resultsLarge

My output:
P DOUBLE = Array(3)
WT DOUBLE = Array(10)
DI2 DOUBLE = Array(10)
LARGEPIXEL DOUBLE = Array(10)
0.70071335 0.70071335 0.70071335 0.70071335
0.70071335 0.70071335 0.70071337 0.71607097
4.0366513 102.29689

First off the data types are all the same. Second the only way I can not get an error for resultsLarge is if I make the param vector more than six elements which the manual requires it must be. If i do make it only 6 element long it give the initial sqrt(-1) error.

The results i got are very wrong and do not make mathematical sense.

Any ideas?

green12
02-18-2009, 10:45 AM
Do i need to be doing something with the FUNCT function?

totallyunimodular
02-23-2009, 10:51 PM
green12,

I am not sure what version of PV-WAVE you are using, but in more recent versions ( >= 8.5 :confused:) the documentation for CURVEFIT states

The function to be fit must be defined and called with FUNCT

and there is a nice example.

green12
03-03-2009, 01:21 PM
I am using PV-WAVE 6. something,

Can you show me this example please?

donb
03-03-2009, 04:17 PM
Hi Green12,

Below is the code from our version 9 documentation. The code runs on version 7.51b, but we have no idea if it works on v6.x or not. Version 7.51b was released 8 years ago.....A very strong recommendation is to update your version of PV-WAVE!

Copy this code into a file named curvefit_example.pro, and then enter curvefit_example as the WAVE> prompt. Note the zoom window functionality when mousing over the plot window, serving as a visual comparison of the polynomial versus user-function fit to the x,y data.

PRO FUNCT, x, p, f, dfdp
; Define f = F(X,P), the function of x and three parameters
; that should be fit to the x,y data
f = p(0)*EXP(-(((x-p(1))/p(2))^2)/2)
; Define the derivative dF/dP
dFdP = [[EXP(-(((x-p(1))/p(2))^2)/2)], \$
[p(0)*EXP(-(((x-p(1))/p(2))^2)/2)*(x-p(1))/p(2)^2], \$
[p(0)*EXP(-(((x-p(1))/p(2))^2)/2)*(x-p(1))^2/p(2)^3]]
END

; In this example, the y-data is coming from a known equation
; of three parameters, f = p(0)*EXP(-(((x-p(1))/p(2))^2)/2)
; where p0=2, p1=3, p2=4. This function is also what will be
; fit to the x,y data. Thus, CURVEFIT should return the output
; parameter p with p(0)=2, p(1)=3, and p(2)=4.
PRO curvefit_example
; Define independent and dependent data to fit function to.
x = FINDGEN(601)/100
y = 2*EXP(-(((x-3)/4)^2)/2)
; Create a vector of weights, all with a value equal to 1
w = FLTARR(601) + 1
; Fit second degree polynomial to data. The returned
; polynomial coefficients serve as the initial guess for the
; curve fit.
p = POLY_FIT(x,y,2, yft)
p = [p(0), p(1), p(2)]
yfit = CURVEFIT(x,y,w,p)
; Set up PLOT colors and fonts, and plot every 20th data point.
TEK_COLOR
oldFont = !P.font
!P.font = 0
subset = INDGEN(30)*20
PLOT, x(subset), y(subset), Thick=2, Psym=1, \$
Yrange=[MIN(y)-.05,MAX(y)+.05], /Ynozero, Charsize=1.1, \$
Title='Data Points To Fit With F(x,p) = p!D0!N*e!U-(((x-p1)/p2)!S!E2!R )/2!N'
; Overplot the 2nd degree polynomial fit.
OPLOT, x, yft, Color=WoColorConvert(3)
; Overplot curve fit, where curve fit to data was defined in
; the FUNCT function definition above.
OPLOT, x, yfit, Color=WoColorConvert(2)
; Create a legend for the plot.
XYOUTS, 0.20, 0.85, '-- 2nd degree polynomial fit', /Normal, \$
Color=WoColorConvert(3)
p0 = STRTRIM(STRING(p(0), Format='(F3.1)'), 2)
p1 = STRTRIM(STRING(p(1), Format='(F3.1)'), 2)
p2 = STRTRIM(STRING(p(2), Format='(F3.1)'), 2)
XYOUTS, 0.20, 0.80, '-- F(x, p) fit with p0='+p0+',p1='+p1+', p2='+p2, \$
/Normal, Color=WoColorConvert(2)
; Use zoom window to compare polynomial fit to user-function fit.
ZOOM, /Continuous, Xsize=200, Ysize=200
!P.font = oldFont
END

Hope this helps,

Don B.

green12
03-08-2009, 07:53 PM
The example is insightful yes but i tired to replace x and y with the data I am working with and I still get errors. Here is my data. Sorry if I am not seeing a simple solution.
x=[112.521,104.295,88.3582,79.1376,67.51,57.46,42.133 8,27.9636,26.7330,2.462684]
y=[185.075,174.793,166.667,161.526,151.575,140.050,13 2.504,120.530,109.950,92.4129]

green12
03-08-2009, 08:30 PM
I only have ten points and I have made the necessary array dimension changes. and I even changed the function i'm fitting to f=a*exp(-x/b). I am lost for ideas.

ed
03-09-2009, 07:26 AM
I only have ten points and I have made the necessary array dimension changes. and I even changed the function i'm fitting to f=a*exp(-x/b). I am lost for ideas.

With the equation f = a * exp(-x/b) you may run into issues when the algorithm uses b=0 during a step. I would suggest changing the function to f = a * exp(-x*B) for the computations and then taking the inverse of the B result once the solution is obtained.

green12
04-08-2009, 08:57 PM
So I am still trying to get this to work. So I have altered Don's code to the function I would like to fit (in the FUCNT function). But It is not fitting correctly or at least how I want it to. I am guessing it has something to do with my initial guesses from the poly_fit parameters but again I'm not sure. Here is my code

PRO FUNCT, x, p, f, dfdp
; Define f = F(X,P), the function of x and three parameters
; that should be fit to the x,y data
f = p(0)*EXP(-(x*p(1)))
; Define the derivative dF/dP
dFdP = [[EXP(-(x*p(1)))], \$
[p(0)*EXP(-(x*p(1)))*(-p(1))]]
END

PRO ce
; Define independent and dependent data to fit function to.
x = FINDGEN(601)/100
y = 2*EXP((.2*x))
; Create a vector of weights, all with a value equal to 1
w = FLTARR(601) + 1
; Fit second degree polynomial to data. The returned
; polynomial coefficients serve as the initial guess for the
; curve fit.
p = POLY_FIT(x,y,2, yft)
print, "p-polyfit"
print, p
p = [p(0),p(1)]
print, "p-array"
print, p
yfit = CURVEFIT(x,y,w,p)
print, "p-curvefit"
print, p
; Set up PLOT colors and fonts, and plot every 20th data point.
TEK_COLOR
oldFont = !P.font
!P.font = 0
subset = INDGEN(30)*20
PLOT, x(subset), y(subset), Thick=2, Psym=1, \$
Yrange=[MIN(y)-.05,MAX(y)+.05], /Ynozero, Charsize=1.1, \$
Title='Data Points To Fit With F(x,p) = p!D0!N*e!U-(((x-p1)/p2)!S!E2!R )/2!N'
; Overplot the 2nd degree polynomial fit.
OPLOT, x, yft, Color=WoColorConvert(3)
; Overplot curve fit, where curve fit to data was defined in
; the FUNCT function definition above.
OPLOT, x, yfit, Color=WoColorConvert(2)
; Create a legend for the plot.
; Use zoom window to compare polynomial fit to user-function fit.
ZOOM, /Continuous, Xsize=200, Ysize=200
!P.font = oldFont
END