PDA

View Full Version : Fit Data to simple exponential

green12
10-04-2009, 02:16 PM
I am trying to fit some data to an exponential of the form:

y= a+ b*exp(-cx)

I have tried using many approaches and I am currently stuck using CURVEFIT. I am getting concatenate errors when I change the examples f and dF/dp in the FUNCT function. I have been trying to get this to work for too long. Can anyone just let me know why this is giving me so much trouble?

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]]

f=p(0)+p(1)*EXP(-(p(2)*x))
dFdP = [[1.0],[EXP(p(2)*x)],[-(x*p(1)*EXP(p(2)*x))]]

END

I am using the pv-wave example and changing it to my wanted fitting function.

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

green12
10-04-2009, 02:26 PM
or the function

y=a+b*LN(c*x)

would also satisfy my problem

green12
10-05-2009, 11:19 AM
nm i figured it out

hendrikh
12-23-2009, 07:53 AM
How did you fix it - running into the same problem ?