LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to reverse 4th order polynomial expression?

Yes, a linear interpolation with a dozen points or so should get you a great initial estimate. (Of course the interpolation example assumes that Y grows with X, if the calibration curve is descending, you'll need to flip things or it won't work).
 
Don't try to be more precise that you need to be. For example if your signal has only 12 bits of resolution, it would make little sense to be more accurate than that. 😉
0 Kudos
Message 11 of 19
(2,199 Views)
For example if your signal has only 12 bits of resolution, it would make little sense to be more accurate than that.

Well, I know that and you know that, but if I'm sitting in the room with my client, I'd rather not have to explain why the old file says 1.234567, and the new one says 1.2348655. As I said, the speed at which this REVERSE process happens is not terribly important.

Thanks for your thoughts...

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 12 of 19
(2,192 Views)

It says that X(i+1) = X(i) - (X(i) * X(i) - Y) / (2 * X(i)). This will find the square root of Y, given an initial guess of X(0). The more iterations you go, the closer you get to the true answer...   I didn't realize it was generalized to any function.

FWIW, the general method is to generate a function f(x) that will evaluate to 0 at your solution point.  Take its derivative with respect to x and call that derivative function g(x).  (I would normally call it f'(x) but the little apostrophe for "prime" doesn't show very well here...)

Then the N-R technique says that

x(i+1) = x(i) - ( f(x(i)) / g(x(i)) )

In the example you gave, you'd have:

1. solve x = sqrt(y)

2. solve x*x = y

3. solve x*x - y = 0

4. f(x) = x*x - y   ; g(x) = 2x

5. x(i+1) = x(i) - ( (x(i)*x(i) - y) / (2*x) )

Now pick an initial guess for x(i), and use it generate the next x, aka x(i+1). 

-Kevin P.

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 13 of 19
(2,182 Views)
I guess I don't understand step 4:

4. f(x) = x*x - y   ; g(x) = 2x

Like I said, it's been a long time since calculus class. Is it really true that the derivative of x*x-y is 2x ?

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 14 of 19
(2,174 Views)
Well, the derivative of x*x is 2x.  And in the context of this particular problem, "y" is a constant, and like all constants has a derivative of 0.
 
When you call the N-R routine to solve for x, you would pass in a specific constant value for y.  Thus, during the course of iterating a solution, y is a constant.  First you solve x*x - 5 = 0, then you solve x*x - 14 = 0, etc. 
 
Dunno if this helps.  The way I think & explain seems clear to me, but I can definitely see where things could get a little fuzzy.
 
Getting back on topic, you've got a curve for:
y = A + B*x^2 + C*x^3 + D*x^4.
 
Your function will be:
f(x) = A + B*x^2 + C*x^3 + D*x^4 - y,  where y is one of the specific constant values from your final data.
 
The derivative will be:
g(x) = 2*B*x + 3*C*x^2 + 4*D*x^3
 
All that's left is that for each specific value of y, you need a good initial guess for a value of x to help make sure you find the correct solution.  The short lookup tables that have been getting discussed should do just fine.
 
-Kevin P.
 
 
ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 15 of 19
(2,170 Views)
Well, thanks K.P. Somewhere in the back of my head, the three brain cells that remembered this stuff are tingling...;->

Getting back on topic, you've got a curve for: y = A + B*x^2 + C*x^3 + D*x^4.

Not exactly, but I get your point. The real curve has an E term:

y = A + B*x + C*x^2 + D*x^3 + E*x^4.
Your function will be: f(x) = A + B*x^2 + C*x^3 + D*x^4 - y, where y is one of the specific constant values from your final data.

That should be: f(x) = A + B*x + C*x^2 + E*x^3 + E*x^4 - y

The derivative will be: g(x) = 2*B*x + 3*C*x^2 + 4*D*x^3

That should be: g(x) = 1*B + 2*C*x + 3*D*x^2 + 4*E*x^3

Is that right?

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 16 of 19
(2,163 Views)

Oops, sorry, but yeah, thanks for getting what I meant to say instead of what I actually did say...

-Kevin P.

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 17 of 19
(2,151 Views)
For anyone who's interested, I have attached a solver to do the inverse evaluation, using the Newton-Raphson method:
X[i+1] = Y - F(X[i]) / G(X[i])
where F(x) = A-Y + Bx + Cxx + Dxxx + Exxxx
and G(x) = derivative of F(x) = B + 2Cx + 3Dxx + 4Exxx
It shows the number of iterations needed to get within a specified tolerance. Having a 0.0 value as the initial guess is a bad idea (it loops forever).

With A, B, C, D, E = 0, 0, 1, 0, 0, you get the square root function that we all know and love. It will happily converge on -4 as the square root of 16, if you give it the right starting guess. (That's correct, I suppose).

Thanks, K. P.

Steve Bird
Culverson Software - Elegant software that is a pleasure to use.
Culverson.com


LinkedIn

Blog for (mostly LabVIEW) programmers: Tips And Tricks

0 Kudos
Message 18 of 19
(2,136 Views)

Nice!

Yes, analytical derivatives are always better. (I recycled an old generic NR I wrote long ago using numerical derivatives and did not bother ;)).

However, you definitely need to implement a termination condition with a maximum number of iterations, because under certain inputs, your VI will go on forever, locking up your program. You also need the program to be aware if the covergence failed. If this will be a subVI, add an error input/output.

(My Newton-Raphson layout might be slightly more efficient, because it does not calculate the derivative if the function has already converged, e.g. if the initial estimate is already the correct solution. There are many different ways to do this, of course.)

Attached is a quick cleanup of my original example, now using analytical derivatives and implementing some primitive validation. 😄

(Sorry, I am still using PolyEval for the function and the derivative to keep the diagram simple ;))

Message 19 of 19
(2,121 Views)