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

Home » Public Forums » archive » Best fit line for sinusoid
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
Best fit line for sinusoid [message #92010] Wed, 30 September 2015 07:22 Go to next message
liam.steele is currently offline  liam.steele
Messages: 13
Registered: September 2015
Junior Member
Hi all,

I was wondering if there was a 'simple' way to get IDL to plot a best fit line for a sinusoidally-varying data set. For example, say temperatures were recorded each hour for 5 days at a certain location, with each measurement having an error of 2 deg C. Then we would have something like:

day = findgen(121)/24
temp = 15 + 10*sin(10*!pi*findgen(121)/120) + 5*randomu(seed, 121)
error = fltarr(121)+2

Is it possible from these three arrays for IDL to work out and plot a best fit line? I have searched online, and can't really find what I'm looking for. (surprisingly I have never had to plot a best fit line to anything before!)

Cheers,

Liam
Re: Best fit line for sinusoid [message #92011 is a reply to message #92010] Wed, 30 September 2015 07:37 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
liam.steele@gmx.co.uk writes:

> I was wondering if there was a 'simple' way to get IDL to plot a best fit line for a sinusoidally-varying data set. For example, say temperatures were recorded each hour for 5 days at a certain location, with each measurement having an error of 2 deg C. Then we would have something like:
>
> day = findgen(121)/24
> temp = 15 + 10*sin(10*!pi*findgen(121)/120) + 5*randomu(seed, 121)
> error = fltarr(121)+2
>
> Is it possible from these three arrays for IDL to work out and plot a best fit line? I have searched online, and can't really find what I'm looking for. (surprisingly I have never had to plot a best fit line to anything before!)

You will do well to start here:

http://cow.physics.wisc.edu/~craigm/idl/fitting.html

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.")
Re: Best fit line for sinusoid [message #92013 is a reply to message #92011] Wed, 30 September 2015 07:51 Go to previous messageGo to next message
liam.steele is currently offline  liam.steele
Messages: 13
Registered: September 2015
Junior Member
On Wednesday, 30 September 2015 15:37:47 UTC+1, David Fanning wrote:
> liam.steele@gmx.co.uk writes:
>
>> I was wondering if there was a 'simple' way to get IDL to plot a best fit line for a sinusoidally-varying data set. For example, say temperatures were recorded each hour for 5 days at a certain location, with each measurement having an error of 2 deg C. Then we would have something like:
>>
>> day = findgen(121)/24
>> temp = 15 + 10*sin(10*!pi*findgen(121)/120) + 5*randomu(seed, 121)
>> error = fltarr(121)+2
>>
>> Is it possible from these three arrays for IDL to work out and plot a best fit line? I have searched online, and can't really find what I'm looking for. (surprisingly I have never had to plot a best fit line to anything before!)
>
> You will do well to start here:
>
> http://cow.physics.wisc.edu/~craigm/idl/fitting.html
>
> 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.")

Aha! That looks far more useful than anything I found. Thanks very much.
Re: Best fit line for sinusoid [message #92014 is a reply to message #92013] Wed, 30 September 2015 08:19 Go to previous messageGo to next message
chris_torrence@NOSPAM is currently offline  chris_torrence@NOSPAM
Messages: 528
Registered: March 2007
Senior Member
On Wednesday, September 30, 2015 at 8:51:33 AM UTC-6, liam....@gmx.co.uk wrote:
> On Wednesday, 30 September 2015 15:37:47 UTC+1, David Fanning wrote:
>> liam.steele@gmx.co.uk writes:
>>
>>> I was wondering if there was a 'simple' way to get IDL to plot a best fit line for a sinusoidally-varying data set. For example, say temperatures were recorded each hour for 5 days at a certain location, with each measurement having an error of 2 deg C. Then we would have something like:
>>>
>>> day = findgen(121)/24
>>> temp = 15 + 10*sin(10*!pi*findgen(121)/120) + 5*randomu(seed, 121)
>>> error = fltarr(121)+2
>>>
>>> Is it possible from these three arrays for IDL to work out and plot a best fit line? I have searched online, and can't really find what I'm looking for. (surprisingly I have never had to plot a best fit line to anything before!)
>>
>> You will do well to start here:
>>
>> http://cow.physics.wisc.edu/~craigm/idl/fitting.html
>>
>> 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.")
>
> Aha! That looks far more useful than anything I found. Thanks very much.

Hi Liam,

Once you've done your fit, you can do an error bar plot like this:

day = dindgen(121)/24
temp = 15 + 10*sin(10*!pi*findgen(121)/120) + 5*randomu(seed, 121)
error = fltarr(121)+2
p = errorplot(day, temp, error, linestyle='none')
yfit = ...
p1 = plot(day, yfit, '2', /overplot)

Cheers,
Chris
Re: Best fit line for sinusoid [message #92015 is a reply to message #92010] Wed, 30 September 2015 09:07 Go to previous message
liam.steele is currently offline  liam.steele
Messages: 13
Registered: September 2015
Junior Member
On Wednesday, 30 September 2015 15:23:01 UTC+1, liam....@gmx.co.uk wrote:
> Hi all,
>
> I was wondering if there was a 'simple' way to get IDL to plot a best fit line for a sinusoidally-varying data set. For example, say temperatures were recorded each hour for 5 days at a certain location, with each measurement having an error of 2 deg C. Then we would have something like:
>
> day = findgen(121)/24
> temp = 15 + 10*sin(10*!pi*findgen(121)/120) + 5*randomu(seed, 121)
> error = fltarr(121)+2
>
> Is it possible from these three arrays for IDL to work out and plot a best fit line? I have searched online, and can't really find what I'm looking for. (surprisingly I have never had to plot a best fit line to anything before!)
>
> Cheers,
>
> Liam

Thanks for the tips everyone. I've managed to get something which I think looks correct, so that's good! I'll add the error bar plot when I work on the real data. The code I used was this:

day = findgen(121)/24
temp = 15 + 10*sin(10*!pi*findgen(121)/120)+5*randomu(seed, 121)
error = fltarr(121)+2
meanval = mean(temp)

expr = 'P[0] + P[1]*sin(P[2]*2*!pi*findgen(P[3])/(P[3]-1) + P[4])'
start = [meanval, (max(temp)-min(temp))/2, 5., 121, 0.]

result = MPFITEXPR(expr, day, temp, error, start)
fitline = result[0] + result[1]*sin(result[2]*2*!pi*findgen(result[3])/(result[3]- 1) + result[4])

plot, day, temp, psym=sym(2)
oplot, day, fitline
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Job Posting: Computer Research Specialist / Research Software Engineer / University of Florida
Next Topic: join

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

Current Time: Wed Oct 08 09:19:48 PDT 2025

Total time taken to generate the page: 0.00447 seconds