Fitting curve to data [message #93631] |
Wed, 14 September 2016 07:23  |
liam.steele
Messages: 13 Registered: September 2015
|
Junior Member |
|
|
Hi all,
I am trying to it a curve to some data points, and have managed to get confused with all the interpolate and spline IDL routines (and more importantly, can't seem to get any of them to work).
As an example, I have a range of depths (in cm) and a value of something at each depth, e.g.
depth = [0.01, 0.1, 0.4, 0.9, 3.5, 14.5, 57.9, 231.7, 926.8]
value = [2.1, 2.1, 1.9, 1.8, 1.3, 1.0, 0.8, 0.5, 0.4]
What I want to do is fit a curve to the data as best I can (the curve doesn't have to go through each point exactly), on a finer set of depths, e.g.
interp_depth = findgen(1001)
I've tried things like:
interp_val = spline(value,depth,interp_depth)
interp_val = interpol(value,depth,interp_depth,/spline)
Can anyone offer any help? Is there some sort of cubic least-squares routine or something I should use? I can get linear interpolation to work, but was wanting something more 'realistic'.
Cheers!
|
|
|
Re: Fitting curve to data [message #93632 is a reply to message #93631] |
Wed, 14 September 2016 08:06   |
wlandsman
Messages: 743 Registered: June 2000
|
Senior Member |
|
|
On Wednesday, September 14, 2016 at 10:23:09 AM UTC-4, liam....@gmx.co.uk wrote:
> Hi all,
>
> I am trying to it a curve to some data points, and have managed to get confused with all the interpolate and spline IDL routines (and more importantly, can't seem to get any of them to work).
>
> As an example, I have a range of depths (in cm) and a value of something at each depth, e.g.
>
> depth = [0.01, 0.1, 0.4, 0.9, 3.5, 14.5, 57.9, 231.7, 926.8]
> value = [2.1, 2.1, 1.9, 1.8, 1.3, 1.0, 0.8, 0.5, 0.4]
>
> What I want to do is fit a curve to the data as best I can (the curve doesn't have to go through each point exactly), on a finer set of depths, e.g.
>
> interp_depth = findgen(1001)
>
> I've tried things like:
>
> interp_val = spline(value,depth,interp_depth)
> interp_val = interpol(value,depth,interp_depth,/spline)
>
> Can anyone offer any help? Is there some sort of cubic least-squares routine or something I should use? I can get linear interpolation to work, but was wanting something more 'realistic'.
>
I think you will get much better results by recognizing that your X axis is logarithmic and doing something like
interp_depth = findgen(1001)+0.1
y = interpol( value,alog10(depth), alog10(interp_depth),/spline)
Of course you need to be very careful when extrapolating outside of the supplied X range. --Wayne
|
|
|
Re: Fitting curve to data [message #93633 is a reply to message #93632] |
Wed, 14 September 2016 08:11  |
liam.steele
Messages: 13 Registered: September 2015
|
Junior Member |
|
|
On Wednesday, 14 September 2016 16:06:21 UTC+1, wlandsman wrote:
> On Wednesday, September 14, 2016 at 10:23:09 AM UTC-4, liam....@gmx.co.uk wrote:
>> Hi all,
>>
>> I am trying to it a curve to some data points, and have managed to get confused with all the interpolate and spline IDL routines (and more importantly, can't seem to get any of them to work).
>>
>> As an example, I have a range of depths (in cm) and a value of something at each depth, e.g.
>>
>> depth = [0.01, 0.1, 0.4, 0.9, 3.5, 14.5, 57.9, 231.7, 926.8]
>> value = [2.1, 2.1, 1.9, 1.8, 1.3, 1.0, 0.8, 0.5, 0.4]
>>
>> What I want to do is fit a curve to the data as best I can (the curve doesn't have to go through each point exactly), on a finer set of depths, e.g.
>>
>> interp_depth = findgen(1001)
>>
>> I've tried things like:
>>
>> interp_val = spline(value,depth,interp_depth)
>> interp_val = interpol(value,depth,interp_depth,/spline)
>>
>> Can anyone offer any help? Is there some sort of cubic least-squares routine or something I should use? I can get linear interpolation to work, but was wanting something more 'realistic'.
>>
>
> I think you will get much better results by recognizing that your X axis is logarithmic and doing something like
>
> interp_depth = findgen(1001)+0.1
> y = interpol( value,alog10(depth), alog10(interp_depth),/spline)
>
> Of course you need to be very careful when extrapolating outside of the supplied X range. --Wayne
Brilliant! Thanks very much. I was thinking there must be something I could do RE logs, but never thought of just interpolating using the log of both!
I can now get on and do something useful! :-)
|
|
|