06-07-2006 12:17 PM
06-07-2006 12:51 PM
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...
Blog for (mostly LabVIEW) programmers: Tips And Tricks
06-07-2006 02:49 PM
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.
06-07-2006 03:21 PM
4. f(x) = x*x - y ; g(x) = 2xLike I said, it's been a long time since calculus class. Is it really true that the derivative of x*x-y is 2x ?
Blog for (mostly LabVIEW) programmers: Tips And Tricks
06-07-2006 04:10 PM
06-07-2006 04:51 PM
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?
Blog for (mostly LabVIEW) programmers: Tips And Tricks
06-08-2006 07:45 AM
Oops, sorry, but yeah, thanks for getting what I meant to say instead of what I actually did say...
-Kevin P.
06-09-2006 08:24 AM
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 + 4ExxxIt 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.
Blog for (mostly LabVIEW) programmers: Tips And Tricks
06-09-2006 10:39 AM
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 ;))