comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » scattering data with MPFITFUN
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
scattering data with MPFITFUN [message #84962] Wed, 19 June 2013 15:49 Go to next message
Nafiseh Masoumzadeh is currently offline  Nafiseh Masoumzadeh
Messages: 10
Registered: June 2013
Junior Member
Hello,

I have some data which I simply want to fit p[0]*x^P[1] on them, But
I don't know I am receiving very far results!
I am using mpfitfun like this:

start=[0.001, 0.1]
results=MPFITFUN('MYFUNCTION', x, y, err, start)

I applied this function and points in MATLAB (fitting toolbox) , and I had reasonable coefficients .

I'd really appreciate your help since I really want to use MPFIT for my more complicated function.

regards,
nafis
Re: scattering data with MPFITFUN [message #84964 is a reply to message #84962] Wed, 19 June 2013 20:51 Go to previous messageGo to next message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
On Wednesday, June 19, 2013 6:49:43 PM UTC-4, Nafiseh Masoumzadeh wrote:
> Hello,
>
> I have some data which I simply want to fit p[0]*x^P[1] on them, But
> I don't know I am receiving very far results!
> I am using mpfitfun like this:
>
> start=[0.001, 0.1]
> results=MPFITFUN('MYFUNCTION', x, y, err, start)
>
> I applied this function and points in MATLAB (fitting toolbox) , and I had reasonable coefficients .

MPFITFUN can solve problems like this.

What happens when you say,
YMODEL = MYFUNCTION(X, START)
Do you get sensible values for YMODEL?

You should also be checking the STATUS and ERRMSG keywords to see if there is a more informative error message.

Craig Markwardt
Re: scattering data with MPFITFUN [message #84968 is a reply to message #84964] Thu, 20 June 2013 01:23 Go to previous messageGo to next message
Nafiseh Masoumzadeh is currently offline  Nafiseh Masoumzadeh
Messages: 10
Registered: June 2013
Junior Member
On Thursday, June 20, 2013 5:51:16 AM UTC+2, Craig Markwardt wrote:
> On Wednesday, June 19, 2013 6:49:43 PM UTC-4, Nafiseh Masoumzadeh wrote:
>
>> Hello,
>
>>
>
>> I have some data which I simply want to fit p[0]*x^P[1] on them, But
>
>> I don't know I am receiving very far results!
>
>> I am using mpfitfun like this:
>
>>
>
>> start=[0.001, 0.1]
>
>> results=MPFITFUN('MYFUNCTION', x, y, err, start)
>
>>
>
>> I applied this function and points in MATLAB (fitting toolbox) , and I had reasonable coefficients .
>
>
>
> MPFITFUN can solve problems like this.
>
>
>
> What happens when you say,
>
> YMODEL = MYFUNCTION(X, START)
>
> Do you get sensible values for YMODEL?
>
>
>
> You should also be checking the STATUS and ERRMSG keywords to see if there is a more informative error message.
>
>
>
> Craig Markwardt


yes, I got reasonable result from YMODEL. and for STATUS I got 6 and I don't have any ERRMSG. I checked in MPFIT script that 6 for STATUS means

FTOL is too small. no further reduction in
; the sum of squares is possible.

but I cannot figure out what is the problem?

I'd appreciate your help,

regards,
Nafis
Re: scattering data with MPFITFUN [message #84987 is a reply to message #84968] Thu, 20 June 2013 12:00 Go to previous messageGo to next message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
On Thursday, June 20, 2013 4:23:27 AM UTC-4, Nafiseh Masoumzadeh wrote:
> On Thursday, June 20, 2013 5:51:16 AM UTC+2, Craig Markwardt wrote:
>> On Wednesday, June 19, 2013 6:49:43 PM UTC-4, Nafiseh Masoumzadeh wrote:
>>
>>> Hello,
>>>
>>> I have some data which I simply want to fit p[0]*x^P[1] on them, But
>>> I don't know I am receiving very far results!
>>> I am using mpfitfun like this:
>>> start=[0.001, 0.1]
>>> results=MPFITFUN('MYFUNCTION', x, y, err, start)
>>>
>>> I applied this function and points in MATLAB (fitting toolbox) , and I had reasonable coefficients .
>> MPFITFUN can solve problems like this.
>>
>> What happens when you say,
>> YMODEL = MYFUNCTION(X, START)
>> Do you get sensible values for YMODEL?
>> You should also be checking the STATUS and ERRMSG keywords to see if there is a more informative error message.
>
> yes, I got reasonable result from YMODEL. and for STATUS I got 6 and I don't have any ERRMSG. I checked in MPFIT script that 6 for STATUS means
>
> FTOL is too small. no further reduction in
> ; the sum of squares is possible.
>
> but I cannot figure out what is the problem?

STATUS=6 is not necessarily a good or bad thing. It usually means that a best fit was achieved.

So let's assume the results of the IDL fit are RESULTS_IDL and the results from your Matlab fit are RESULTS_MATLAB. You can then compute the chi-square value for each.

CHI_START = TOTAL( (MYFUNCTION(X, START) - Y)^2 / ERROR^2 )

CHI_IDL = TOTAL( (MYFUNCTION(X, RESULTS_IDL) - Y)^2 / ERROR^2 )

CHI_MATLAB = TOTAL( (MYFUNCTION(X, RESULTS_MATLAB) - Y)^2 / ERROR^2 )

How different are these three values?

Craig
Re: scattering data with MPFITFUN [message #85016 is a reply to message #84987] Sat, 22 June 2013 14:37 Go to previous messageGo to next message
Nafiseh Masoumzadeh is currently offline  Nafiseh Masoumzadeh
Messages: 10
Registered: June 2013
Junior Member
On Thursday, June 20, 2013 9:00:33 PM UTC+2, Craig Markwardt wrote:
> On Thursday, June 20, 2013 4:23:27 AM UTC-4, Nafiseh Masoumzadeh wrote:
>
>> On Thursday, June 20, 2013 5:51:16 AM UTC+2, Craig Markwardt wrote:
>
>>> On Wednesday, June 19, 2013 6:49:43 PM UTC-4, Nafiseh Masoumzadeh wrote:
>
>>>
>
>>>> Hello,
>
>>>>
>
>>>> I have some data which I simply want to fit p[0]*x^P[1] on them, But
>
>>>> I don't know I am receiving very far results!
>
>>>> I am using mpfitfun like this:
>
>>>> start=[0.001, 0.1]
>
>>>> results=MPFITFUN('MYFUNCTION', x, y, err, start)
>
>>>>
>
>>>> I applied this function and points in MATLAB (fitting toolbox) , and I had reasonable coefficients .
>
>>> MPFITFUN can solve problems like this.
>
>>>
>
>>> What happens when you say,
>
>>> YMODEL = MYFUNCTION(X, START)
>
>>> Do you get sensible values for YMODEL?
>
>>> You should also be checking the STATUS and ERRMSG keywords to see if there is a more informative error message.
>
>>
>
>> yes, I got reasonable result from YMODEL. and for STATUS I got 6 and I don't have any ERRMSG. I checked in MPFIT script that 6 for STATUS means
>
>>
>
>> FTOL is too small. no further reduction in
>
>> ; the sum of squares is possible.
>
>>
>
>> but I cannot figure out what is the problem?
>
>
>
> STATUS=6 is not necessarily a good or bad thing. It usually means that a best fit was achieved.
>
>
>
> So let's assume the results of the IDL fit are RESULTS_IDL and the results from your Matlab fit are RESULTS_MATLAB. You can then compute the chi-square value for each.
>
>
>
> CHI_START = TOTAL( (MYFUNCTION(X, START) - Y)^2 / ERROR^2 )
>
>
>
> CHI_IDL = TOTAL( (MYFUNCTION(X, RESULTS_IDL) - Y)^2 / ERROR^2 )
>
>
>
> CHI_MATLAB = TOTAL( (MYFUNCTION(X, RESULTS_MATLAB) - Y)^2 / ERROR^2 )
>
>
>
> How different are these three values?
>
>
>
> Craig

Hello,

sorry for delay,

I don't know how to calculate error for each case, but here is the results for
(sum(residuals))^2 or TOTAL( (MYFUNCTION(X, START) - Y)^2 .

start

16.4292

IDl

106.596

Matlab

2.39294


I really don't know why for IDL is far from what should be. I think here I cannot attached any figures since in figure it is clear that how much the result from IDL is different from my data points!

I'd appreciate your help,

best regards,
Nafiseh
Re: scattering data with MPFITFUN [message #85019 is a reply to message #85016] Sun, 23 June 2013 18:59 Go to previous messageGo to next message
Craig Markwardt is currently offline  Craig Markwardt
Messages: 1869
Registered: November 1996
Senior Member
On Saturday, June 22, 2013 5:37:08 PM UTC-4, Nafiseh Masoumzadeh wrote:
> On Thursday, June 20, 2013 9:00:33 PM UTC+2, Craig Markwardt wrote:
>> On Thursday, June 20, 2013 4:23:27 AM UTC-4, Nafiseh Masoumzadeh wrote:
>>> On Thursday, June 20, 2013 5:51:16 AM UTC+2, Craig Markwardt wrote:
>>>> On Wednesday, June 19, 2013 6:49:43 PM UTC-4, Nafiseh Masoumzadeh wrote:
>>>>
>>>> > Hello,
>>>> >
>>>> > I have some data which I simply want to fit p[0]*x^P[1] on them, But
>>>> > I don't know I am receiving very far results!
>>>> > I am using mpfitfun like this:
>>>> > start=[0.001, 0.1]
>>>> > results=MPFITFUN('MYFUNCTION', x, y, err, start)
>>>> >
>>>> > I applied this function and points in MATLAB (fitting toolbox) , and I had reasonable coefficients .
>>>> MPFITFUN can solve problems like this.
>>>>
>>>> What happens when you say,
>>>> YMODEL = MYFUNCTION(X, START)
>>>> Do you get sensible values for YMODEL?
>>>> You should also be checking the STATUS and ERRMSG keywords to see if there is a more informative error message.
>>>
>>> yes, I got reasonable result from YMODEL. and for STATUS I got 6 and I don't have any ERRMSG. I checked in MPFIT script that 6 for STATUS means
>>>
>>> FTOL is too small. no further reduction in
>>> ; the sum of squares is possible.
>>>
>>> but I cannot figure out what is the problem?
>>
>> STATUS=6 is not necessarily a good or bad thing. It usually means that a best fit was achieved.
>>
>> So let's assume the results of the IDL fit are RESULTS_IDL and the results from your Matlab fit are RESULTS_MATLAB. You can then compute the chi-square value for each.
>> CHI_START = TOTAL( (MYFUNCTION(X, START) - Y)^2 / ERROR^2 )
>> CHI_IDL = TOTAL( (MYFUNCTION(X, RESULTS_IDL) - Y)^2 / ERROR^2 )
>> CHI_MATLAB = TOTAL( (MYFUNCTION(X, RESULTS_MATLAB) - Y)^2 / ERROR^2 )
>> How different are these three values?
>
>
> I don't know how to calculate error for each case, but here is the results for
> (sum(residuals))^2 or TOTAL( (MYFUNCTION(X, START) - Y)^2 .
>
> start
> 16.4292
>
> IDl
> 106.596
>
> Matlab
> 2.39294
>
> I really don't know why for IDL is far from what should be. I think here I cannot attached any figures since in figure it is clear that how much the result from IDL is different from my data points!

The chi-square value from the start is 16.4 and at finish of MPFITFUN it is 106.6? Sorry, but MPFITFUN will never make the chi-square value worse that it starts with.

Also, you say you don't know how to compute the error value, but you show that you are using "ERR" above in your function call. So I suspect that you made typographical errors.

Here is a complete worked example showing that MPFITFUN can solve this problem:
IDL> xx = 10^(dindgen(10)/3) ;; Evenly spaced in ALOG10(X)
IDL> ym = 5.337d * xx^(-1.672d) ;; Model Y values.
IDL> .compile
- function mypowerlaw, x, p
- return, p[0]*x^p[1] ;; Power law equation
- end
IDL> print, mpfitfun('mypowerlaw', xx, ym, 1d, [1d, 1d])
===> 5.337 -1.672
(final chi-square is zero because this is an exact solution; the starting chi-square value is 1274609)

And here is a second example show that it works with error bars as well.
IDL> ye = ym*0.1d ;; 10% errors
IDL> yy = randomn(seed,n_elements(ye))*ye + ym ;; Random Y values
IDL> print, mpfitfun('mypowerlaw', xx, yy, ye, [1d, 1d])
===> 5.2666485 -1.6647875
The solution is not exact because I added some random error to make it realistic. The final chi-square value is 8.58 for 8 degrees of freedom, and the starting value is 3.84e16).

So either your model function is not actually a power law. Or, you are not calling MPFITFUN() in the way that you say you are.

Craig Markwardt
Re: scattering data with MPFITFUN [message #85020 is a reply to message #85019] Sun, 23 June 2013 19:29 Go to previous message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Craig Markwardt writes:

> So either your model function is not actually a power law. Or, you are not calling MPFITFUN() in the way that you say you are.

Sigh....

Cheers,

David



--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: National Hiking Day
Next Topic: Avoiding Loops in IDL 8.2.2

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 15:39:46 PDT 2025

Total time taken to generate the page: 0.00674 seconds