From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Curve fitting with a piecewise function

Solved!
Go to solution

Hi

I have data (see attachment) that I'd like to fit with a piecewise function defined as

y(x)=A, if x<B

y(x)=A+(x-B)^C, if x>B

 

I learned how to use non-linear fitting VI to fit the data with the power law function over the entire inverval, but can not figure out yet how to introduce that condition for the argument.

 

I also saved the data into the attached VI.

 

Any help would be greatly appreciated.

 

Regards,

Misha

Download All
0 Kudos
Message 1 of 10
(3,904 Views)

I hope this one didn't end up too messy to understand.

I tried to keep it as general as possible so some things you wont need.

Ask if you dont understand something 🙂

See ya, and good luck!

(dont forget to kuddo and mark as solved 🙂

0 Kudos
Message 2 of 10
(3,887 Views)

mihonyc wrote:

y(x)=A, if x<B

y(x)=A+(x-B)^C, if x>B


That's not good, because you have a gap where x=B. One of the sides need to include an "equal" 😄

 

Writing a model that switches formulas based on X is simple. All you need is another parameter B and a case structure. The problem is in the discrete nature of the x data, which could cause problems for the partial derivatives. Since B is actually a parameter in the second function, I think it should work just fine. What have you tried so far?

 

 

0 Kudos
Message 3 of 10
(3,880 Views)

@Nihil wrote:

I hope this one didn't end up too messy to understand.



(Nihil: the VI you have attached seems corrupt and cannot be opened. What is the LabVIEW version?)

0 Kudos
Message 4 of 10
(3,879 Views)
Thank you very much for the replies!
Will try your VI on Mon morning.
I did not mean to exclude x=B, it can be added to ether of the intervals.

Regards
Misha
0 Kudos
Message 5 of 10
(3,870 Views)

altenbach, oops! here it goes again, (LV2010)

😉 thanks for taking a look 

0 Kudos
Message 6 of 10
(3,864 Views)
Solution
Accepted by topic author mihonyc

Seems to work just fine, but I think the model is insufficient for the data.

 

Here is a quick draft.

 

 

Download All
Message 7 of 10
(3,863 Views)

@Nihil wrote:

altenbach, oops! here it goes again, (LV2010)

😉 thanks for taking a look 


OK. You probably did not notice that the VI in the original question was LabVIEW 8.6, meaning he won't be able to look at your VI. Since you don't really answer the question about fitting, it probably does not really matter. 😉

 

There are a couple of problems with your code, for example uncoditionally building an array in a FOR loop using a shift register and a "built array" node is very inefficient. All you need is autoindexing at the right loop boundary. Autoindexing is significantly better because LabVIEW knows how big the array will be and can allocate it when the loop starts. You also don't need to wire [x] across the case structure, because the output is identical for all cases. Same with the X-array and the FOR loop. You already have the x-array fully formed before the loop starts, thus it does not need to be re-build inside the loop. They y array is the only item that needs to exit the loop, the x array can be wired around for the second bundle operation.(The LabVIEW compiler, especially in very recent version is getting better at recognizing these things and doing corrections under the hood, but why risk it?)

Message 8 of 10
(3,858 Views)

hey! thanks Alternbach 😉 
i've just come to understand the original question Smiley Frustrated 

0 Kudos
Message 9 of 10
(3,849 Views)

Thanks a lot

That's exactly what I was looking for

 

Misha

0 Kudos
Message 10 of 10
(3,810 Views)