# LabVIEW

cancel
Showing results for
Did you mean:

Solved!
Go to solution

## Can curve fit VI's handle piece-wise defined functions?

I am trying to use the (Constrained) Nonlinear Curve Fit VI to analyze a step response.  Is it possible to define a parameter to fit a time shift?  That way I won't have to be so careful about getting the precise starting point of the analysis window.  E.g. y(t) = f(x(t-t0),a=[a0, at1]) or y(t) = f(x(t),a=[a0, a1, t0])?  It requires an IF (case statement) inside the function.  You can do this in Excel so I would have thought LabVIEW could do it, too.  See attached simplified example of a time-delayed linear fit.  It gives Error -20041 occurred at NI_Gmath.lvlib:Nonlinear Curve Fit LM.vi:1. Possible reason(s): Analysis: The system of equations cannot be solved because the input matrix is singular.

Regards,

Message 1 of 7
(3,075 Views)

## Re: Can curve fit VI's handle piece-wise defined functions?

Hi

Thank you for contacting National Instruments, my name is Andrea and I’ll be assisting you with your issue.

I am going to check the vi that you have attached.

Regards,

Message 2 of 7
(2,967 Views)

## Re: Can curve fit VI's handle piece-wise defined functions?

If you open the Nonlinear Curve Fit LM.vi and place some probes you find that the matrix fed to the Inverse Matrix.vi is:

1785  153  0

153   20  0

0     0    0

That matrix is produced by abx.vi.  The LM function and gradient.vi calculates the values of your function and the gradient.  The second column of the gradient is all 1s and the third column is all zeros.

Perhaps this is a case where explicitly calculating the f'(X,a) in linearShift.vi might be worth a try.  Then you can assign suitable values at the discontinuity and avoid the singular matrix.

Lynn

Message 3 of 7
(2,963 Views)

## Re: Can curve fit VI's handle piece-wise defined functions?

Your t0 paramter is quantized, thus the partial derivatives won't work (changing the parameter has no effect except for the few points where it jumps by one over an infinitesimal small increment).

You need to rewrite the formula so you get a smooth shift. Shouldn't be too hard.

Message 4 of 7
(2,961 Views)
Solution
Accepted by topic author barkeram

## Re: Can curve fit VI's handle piece-wise defined functions?

Try this instead. Seems to work just fine. 😄

Message 5 of 7
(2,956 Views)

## Re: Can curve fit VI's handle piece-wise defined functions?

Thanks altenbach.  Your VI works.  Actually, after using yours as a reference, all you have to do to my original linearShift.vi is replace the Y[i] = X[i-n].vi with the subtract VI, and it works!  Not exactly sure why...  Your additional style improvements are also appreciated.

Message 6 of 7
(2,930 Views)

## Re: Can curve fit VI's handle piece-wise defined functions?

Looking at the code again, you should also delete these two error local variables. You are writing to both error out terminals from different places with different information, so the final outcome is totally upredictable.

Message 7 of 7
(2,920 Views)