LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

piece-wise linear function

Solved!
Go to solution

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?

 

piece-wise linear.jpg

 

Thanks,

 

Battler.

 

 

 

 

 

0 Kudos
Message 1 of 12
(5,383 Views)

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

LabVIEW 2012
0 Kudos
Message 2 of 12
(5,377 Views)

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

0 Kudos
Message 3 of 12
(5,375 Views)

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.

0 Kudos
Message 4 of 12
(5,356 Views)

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 

 

 

 

0 Kudos
Message 5 of 12
(5,353 Views)

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

Message Edited by David Crawford on 03-29-2010 02:04 PM
Download All
Message 6 of 12
(5,328 Views)

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.

0 Kudos
Message 7 of 12
(5,306 Views)

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

0 Kudos
Message 8 of 12
(5,290 Views)
Solution
Accepted by topic author battler.

G'day David,

 

Thanks for your assistance.

 

I think I have solved it with this:

 

piecewise linear interpolate.png 

 

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.

 

0 Kudos
Message 9 of 12
(5,282 Views)

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.

Message Edited by David Crawford on 03-30-2010 07:47 AM
0 Kudos
Message 10 of 12
(5,272 Views)