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: 

For "Nonlinear Curve Fit.vi", what does -20068 mean and how can I avoid it?

Hey guys,

I am trying to fit an oval in a set of 2D data points, which represents a partial oval.
So I am trying to see if "Nonlinear curve fit.vi" would work in the situation.

However, sometimes that math VI would give me an error -20068 and would return incorrect fitting parameter.
I can't figure out what condition would return that error so can you tell me
what the cause is and how I can avoid that error?

You can run the attached LabVIEW 2013 VI and see the error.

0 Kudos
Message 1 of 11
(5,084 Views)

I dug into the subVIs and, as near as I can tell, the wire I highlighted in the attached screenshot of "LM Numeric Gradient.VI" is somehow getting populated with NaNs.  When I trap the NaN and replace it with a number, the VI runs much longer and ultimately errors out with a different message "no optimization found".  I'll leave it to wiser minds than mine as to why the f(x) output of "LM call model function.VI" would have NaN's on it, but hopefully this helps point somebody in the right direction.  

0 Kudos
Message 2 of 11
(5,072 Views)

It appears that your model is very sensitive to some of the parameters. Changing h from 0 to +/-0.0001 will give negative numbers for arguments to the square root. 

 

Can you change the model (perhaps by a change of variables or tranformation away from the origin) such that negative value become much less likely?

 

Lynn

0 Kudos
Message 3 of 11
(5,041 Views)

Interesting point.

 

The model is a result of solving the ellipse equation for y,
(x^2)/(a^2) + (y^2)/(b^2) = 1

 

So I don't really see a simple way to make the negative values less likely...

but maybe I can use "Constrained Nonlinear Curve Fit" where I can bound the parameters then?

 

This is slowly drifting away from a LabVIEW problem but any help is appreciated! Smiley Wink

0 Kudos
Message 4 of 11
(5,026 Views)

As Lynn mentions, a negative argument in the sqrt function is a problem.  This will output NaN, causing the errors you are seeing.  Also, your model can only generate the points above the horizontal axis location, and not below, because the sqrt() result is being added instead of subtracted.

It appears that you have looked over the ellipse fitting example that ships with LabVIEW, as you are using the ellipse generator from that example.  Did you find a problem with that code?

-Jim

Message 5 of 11
(4,958 Views)

@jcyth wrote:

The model is a result of solving the ellipse equation for y, 

(x^2)/(a^2) + (y^2)/(b^2) = 1

 

So I don't really see a simple way to make the negative values less likely...

but maybe I can use "Constrained Nonlinear Curve Fit" where I can bound the parameters then?

 

This is slowly drifting away from a LabVIEW problem but any help is appreciated! Smiley Wink


The solution is to re-parameterize the problem and solve for something much less pathological 😄

 

You can directly fit for the above formula by providing x&y for all points to the model and optimize the four ellipse parameters until you get an array of all ones. 😉

 

I quicky coded this in the attached example and it seem to be quite stable, even with lots of noise.

 

See if this works for you.

 

Download All
Message 6 of 11
(4,939 Views)

Of course this works equally well for partial ellipses.

 

(If the arc is too small, you need to change the algorithm for the parameter guesses, of course).

 

Message 7 of 11
(4,925 Views)

I dont remember exactly why I switched over from the VI ref verison to the string equation model..

I think coworker and I thought the VI ref version didint work and so we just started to use the string equation model.

But VI ref version is definitely so much easier

0 Kudos
Message 8 of 11
(4,820 Views)

Your code helped me understand how the reparametrization was done with the NI example! thanks!

 

So the problem I am working with is detection of partial ovals with rotation.

I tried playing around with the mathematical model but the fitted oval has some trouble keeping up with big rotations.

Any ideas on how I did the modeling wrong?

 

ellipse model.png

 

Download All
0 Kudos
Message 9 of 11
(4,811 Views)

You implemented

        xp = x cos theta- x sin theta
        yp = y sin theta + y cos theta

inside "elipse model with rotation.vi".

 

Once that was fixed, there was a sign error on the on the rotation angle.  Changed the model to

        xp = x cos theta+ y sin theta
        yp = y cos theta - x sin theta

 

Now the fitted ellipse is rotated, and rotated in the correct direction, but there's a slight translation error some place.   

 

 

0 Kudos
Message 10 of 11
(4,789 Views)