Cart | Help
 You are here: NI Home > NI Developer Zone > NI Discussion Forums Select Community English Français Italiano 日本語 한국어 Español 简体中文 繁体中文 Português (BR)

## LabVIEW

Active Participant
Posts: 603

# piece-wise linear function

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).

Thanks,

Battler.

Active Participant
Posts: 363

# Re: piece-wise linear function

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 2009 .

DTA
Active Participant
Posts: 649

# Re: piece-wise linear function

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

Active Participant
Posts: 603

# Re: piece-wise linear function

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.

Active Participant
Posts: 603

# Re: piece-wise linear function

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

Active Participant
Posts: 649

# Re: piece-wise linear function

[ Edited ]

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
Active Participant
Posts: 603

# Re: piece-wise linear function

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.

Active Participant
Posts: 649

# Re: piece-wise linear function

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

Active Participant
Posts: 603

# Re: piece-wise linear function

G'day David,

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.

Active Participant
Posts: 649

# Re: piece-wise linear function

[ Edited ]

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