Re: help with mathematics [message #80306] |
Wed, 30 May 2012 22:09  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
On May 30, 3:24 pm, Russell Ryan <rr...@stsci.edu> wrote:
> On May 30, 8:52 am, nata <bernat.puigdomen...@gmail.com> wrote:
>
>> Thank you for reply Russell ! I am already reading the documentation for curvefit. I will try to solve the equation as you say...
>> These mathematical things are not obvious to me and sometimes I don't know what to do to solve this kind of problems.
>
>> Anyway, I will let you know if I don't find a good fit.
>> Thank you,
>
>> nata
>
> Hi nata,
> on a hunch I did the curvefit thing last night. The results don't
> really look very good, and it makes me wonder if the model is not very
> good....
I did this... (using my MPFIT package)
Rinit = [1,1,1.]
expr = '(P[0] + P[1]*X + P[2]*X^2 + ((1-P[1]-4*P[2])/12)*X^3)'
R = mpfitexpr(expr,x,y,1.0,Rinit, yfit=yfit)
plot, x,y
oplot, x, yfit, color='ff'xl
... and you're right, it doesn't look like a good model fit.
Craig
|
|
|
|
|
Re: help with mathematics [message #80318 is a reply to message #80313] |
Mon, 28 May 2012 21:02   |
Russell Ryan
Messages: 122 Registered: May 2012
|
Senior Member |
|
|
On May 28, 10:46 am, nata <bernat.puigdomen...@gmail.com> wrote:
> Hi All,
>
> I have a set of measurements, X and Y, and I need to fit a polynomial function with the following "constraints":
>
> Y = a + bX + CX^2 + ((1-b-4C)/12)X^3
>
> I tried to do this with the AMOEBA function but the result is not consistent at all. You can see my code below...
>
> I need some help with this problem. Could anyone help me with this problem ?
> Thank you in advance
>
> nata
>
> ; First define the function FUNC:
> FUNCTION FUNC P
> COMMON FUNC_XY, X, Y
> RETURN, MAX(ABS(Y - (P[0] + P[1]*X + P[2]*X^2 + ((1-P[1]-4*P[2]) / 12)*X^3)))
> END
>
> PRO PROGRAM
>
> COMMON FUNC_XY, X, Y
> X=[11.0,13.0,14.5,15.5,16.5,17.5,18.5,19.5,20.5,21.5,22.5,23 .5,24.5,25.5, $
> 26.5,27.5,28.5,29.5,30.5,31.5,32.5,33.5,34.5,35.5,37.0,39.0, 41.5,44.5,48.0]
>
> Y=[-0.921,-0.735,-0.627,-0.554,-0.439,-0.379,-0.313,-0.247,- 0.186,-0.126, $
> -0.092,-0.018,0.052,0.108,0.185,0.255,0.308,0.375,0.443,0.52 5,0.597,0.656, $
> 0.733,0.816,0.950,1.109,1.269,1.562,2.044]
>
> R = AMOEBA(1.0e-25, SCALE=[1e2,1e2,1e2], P0=[0,0,0], FUNCTION_VALUE=fval, FUNCTION_NAME='FUNC')
>
> PLOT, X, Y
> OPLOT, X, (R[0] + R[1]*X + R[2]*X^2. + ((1-R[1]-4*R[2])/12)*X^3.), COLOR=222
>
> END
Wait, you're not making any sense. Are you saying that you have two
vectors (x and y) to which you want to fit a function of the form:
ymodel = a + bX + CX^2 + ((1-b-4C)/12)X^3
and determine the coefficients a, b, c? If so, then there are a
dozen ways to do this and amoeba is among the worst. Not to mention,
your definition of the minimization function (while what IDL suggests)
is not very good, because it's not differentiable. Always minimize a
distance metric of the form
X^2 = sum_i (y_i - ymodel(x_i))^2
where i runs over points --- unless you have a very good reason *NOT*
to do this.. This way, you can take the derivative of X^2 w.r.t. each
parameter, set them to zero, and solve the resulting set of
equations. If the problem is linear (and this one is), then you can
solve the problem exactly by inverting a simple matrix.
Please look into curvefit.pro (comes with IDL) or mpfit.pro (built and
maintained by C. Markwardt).
Russell
|
|
|
Re: help with mathematics [message #80449 is a reply to message #80306] |
Thu, 31 May 2012 05:55  |
natha
Messages: 482 Registered: October 2007
|
Senior Member |
|
|
Hi guys,
Thank you for your interest and your help. You are right when you say that the model is not very good. I also tried different things and none of my fits are very good.
I changed the 3th coefficient using the following expression: (1-P[1]-2*ZMAX*P[2])/(3*ZMAX^2)
I've got a good result using ZMAX=100 but, in my study, ZMAX=100 is almost impossible so I started looking at the problem differently. Anyway, I did a lot of things during these last 2 days and I think that I don't need the fit anymore.
Thank you for your help. I really appreciate your replies,
nata
|
|
|