03-29-2010 04:30 AM
Hi all,
Basic problem but I would like some tips:
I have a series of points (which is an approximation of a more complex curve). I want to fit linear lines between each of the points.
The end result should be a VI which I can feed an x-value into and return a y-value for the entire range covered by the series of linear lines (equations).
How best to go about this?
Thanks,
Battler.
Solved! Go to Solution.
03-29-2010 04:37 AM
This is similar to this ost, in which i have given an example way of doing this.
http://forums.ni.com/ni/board/message?board.id=170&message.id=246961#M246961
Another method is to use the 1D interpolate function, set it to linear to give you more data points within your graph to lookup/
Craig
03-29-2010 04:39 AM
I like to take a crack at it. But before I do...
Will you be working with the complex curve then trying to calculate the linear segments.
Will the number of linear segments vary or be fixed?
Will the curve always have the same approximate shape with a certain tolerance?
Can you supply a csv of the the xy data?
cheers
David
03-29-2010 06:01 AM
Hi Craig,
The problem with the Linear Interpolate function is that it's messy in that I have to produce a whole lot of x-points and then look up the closest y-value. It will be slow. Also it doesn't seem to form a single linear line between the points.
Each line will have a linear equation which is applicable over a certain interval. I want a way of combining the linear equations so that if I enter an x-value it will use the correct linear equation for that range and return the y-value.
Thanks.
03-29-2010 06:08 AM
Thanks David.
I dont have the complex curve. I will be using the points I have and linearly interpolating between the points.
The number of linear segments will be fixed (9).
Yes, they have the same approximate shape. Is that shape familiar to anybody? Can it be likened to a cubic? Remember it is shown on log/log scale.
I can give you the data:
y x
10 500
20 500
50 470
100 440
200 380
500 100
1000 50
2000 40
5000 39
10000 39
03-29-2010 08:03 AM - edited 03-29-2010 08:04 AM
Here is what I was thinking
I can't think of a way to handle the boundary segments where the gradient is infinite.
hope it helps
David
03-29-2010 04:20 PM
David,
This isn't going to work as it returns NaN (is discontinuous) at ends (or at all the x-data values).
Could this whole problem be solved only using the Threshold 1D Array VI?
Remember I only want to draw a staight line between each of the points (interpolate). I do not want to do any regression.
Any ideas, anybody?
Thanks,
Battler.
03-29-2010 05:50 PM
Hi,
Yep, my sort isn't doing what we need. The y-data needs to be in descending order when x repeats so that the linear fits stitch together.
To fix the whole thing I think we need to
Sort your input array by x ascending then by y descending
If x-value <= Min(x) then return Max(y)
If x-value >= than Max(x) then return Min(y)
If x-value > Min(x) and < Max (x) then use the threshold 1D array to pick the right linear fit for a given x.
"Remember I only want to draw a staight line between each of the points (interpolate). I do not want to do any regression."
Sorry, I am not following you there. I thought you wanted a function to return a y-value for a given x-value. Could you go into more detail, please? Perhaps with an example?
It would be good to get more real data sets to test the algorithm. I am interested to see if the low value x and high value x lines at the boundary are always vertical i.e like x=39 and x=500.
It pretty late here so I will sign off for the moment and catch up with you tomorrow.
cheers
David
03-29-2010 06:58 PM
G'day David,
Thanks for your assistance.
I think I have solved it with this:
No point in re-inventing the wheel! NI have done the job for us.
I think we may have got our x's and y's mixed up. I probably needed to communicate it better.
I want to be able to enter a value for x (col(0)) and come up with a value for y (col(1)).
Eg. if I put x=750 it returns y=75, which seems right to me..
Thanks for helping me to find these useful VIs.
03-30-2010 01:45 AM - edited 03-30-2010 01:47 AM
Morning!
With the axis swapped and the data in ascending order (with no repeat x) its easy peasy in the end. Glad you got what you needed.