LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Need help with array

Hi I'm having some difficulties with the following program. Basically this program takes in data points from a txt file (4 arrays), tries to do a 3D polynomial surface fit, and gives a 3D polynomial equation to the user.

The program works fine but I'm not getting the type of accuracy I need. After tracing through the Levenburg Marquadt example it seems that I need to do iteration and try to minimize Chi2.

The program takes data points for X and Y, does bunch power stuff, and puts them into a 2D array. This 2D array is then compared with the Z array to produce the polynomial equation.

My main difficulty right now is that I'm not too sure which data value in the 2D array I need to change. In fact, I'm not too certain what this 2D array represents. I would really appreciate if someone could give me a few pointers or a hand. Thank you very much.
0 Kudos
Message 1 of 30
(9,064 Views)

There are two main ways to do the fit:

  1. General LS fit
  2. Levenberg-Marquardt

Within certain limitations, both should give similar resuts, the general LS fit is however faster. It also does not need initial estimates for the coefficients (I set them all to zero for Lev-Mar).

I have modified my earlier example to do both, general LS fit and lev-mar so you can directly compare the two. Unfortunately, I no longer have LabVIEW 6.1 installed so I cannot test and reapir the downconverted version. I am thus posting both (LabVIEW 6.1 (as zipped folder) and LabVIEW 7.0 as llb). There probably will be some minor errors to correct before it will run in LabVIEW 6.1.

How to use: Whenever you change the order, method or algorithm, the VI calculates the new polynomial coefficients, displays the formula, calculates the full surface of the fit and displays the raw data and best fit plane on a 3D graph on tab 1. It also shows the fit points and residual in a 2D graph on tab 2. Tab 3 shows the list of coefficients and terms and allows you to calculate z for any give x,y pair. (you can also move the 3D cursor to do the same on tab 1 ;))

Please let me know if anything is not entirely clear. Good luck!

(There are probably some bugs, I did not spend much time on this) 🙂

Message Edited by altenbach on 07-06-2005 03:58 PM

Download All
0 Kudos
Message 2 of 30
(9,044 Views)
Hi altenbach

Thanx for the help (again).

To me it looks like both General LS fit and Levenberg-Marquardt fit both produce the same results. Unfortunately this example you provided did exactly the same thing.... it only iterated once.

Basically what I want to do now is to be able to loop through the General LS Fit/Levenberg-Marquardt fit algorithm so I can minimize the residue (or Chi2) to a set value (ie 5e-3).

Like I mentioned before, my problem right now is I'm trying to figure out a way to manipulate the xy 2D array. It looks like the 2D array is as the follow...

(2rd order gives you a ;6* ___ 2D array)

row
1   X^0 * Y ^ 0
2   X^1 * Y ^ 0
3   X^0 * Y ^ 1
4   X^2 * Y ^ 0
5   X^1 * Y ^ 1
6   X^0 * Y ^ 2

I'm trying to multiply each element in each row by a set value depending on the residue/Chi2 value but it's not quite working. Right now the residue/Chi2 value just keeps increasing, making my estimation worse and worse. 😞

Take a look at 3DpolynomialFit.vi in the attached .dll. I got this idea through tracing Levenberg-Marquardt.vi....

Still working on it.
0 Kudos
Message 3 of 30
(9,036 Views)


@Honeywell wrote:
Unfortunately this example you provided did exactly the same thing.... it only iterated once.


This is incorrect. The Lev-mar subVI iterates until a minimum in chisquare is found (or until 1000 iterations have passed). I can virtually guarantee you that you won't find a better minimum manually. This is as good as it gets! 🙂
 
You can open Lev-mar to see the code. Put a probe on the [i] terminal to verify the number of iterations (you need to enable debugging first in VI properties. I have debugging turned of for efficiency). 2D polynomials are very well behaved and the absolute best fit is found within a handful of iterations.
 
Specifically with your data:
 
Order 0,  1 coefficient, 4 iterations
Order 1, 3 coefficients, 4 iterations
Order 2, 6 coefficients, 6 iterations 
Order 3, 10 coefficients, 9 iterations 
Order 4, 15 coefficients, 13 iterations 
Order 5, 21 coefficients, 16 iterations 
Order 6, 28 coefficients, 21 iterations
Order 7, 36 coefficients, 27 iterations 
Order 8, 45 coefficients, 30 iterations
 
Lev-Mar is extremely efficient to find the best combination of coefficients for a given model! 🙂
0 Kudos
Message 4 of 30
(9,035 Views)
Sorry should correct what I said I guess. I know Lev-mar subVI does iterates until a minimum in chisquare is found...

But in this VI... it's only calling Solve Linear Equations.vi which I don't think iterates (I might be wrong since both this VI and yours gives the same coefficients).
0 Kudos
Message 5 of 30
(9,034 Views)
0 Kudos
Message 6 of 30
(9,029 Views)
Yes, solve linear equations does not iterate, and it does not need to! 😉
 
It is basically the same as the general LS fit I am using, except that general LS fit is a bit more compact and also allows you to choose between several algoritms. It does not need to iterate because it is a direct mathematical procedure. All the magic is in the matrix operations.
 
(Similar to e.g. classic linear regression, which does not need to iterate either.)
 
Your problem can be expressed as a simple sum of terms: Z=Sum(Ai * Fi(x,y)) and is thus directly solvable using general LS fit. You just need to form the H matrix and solve the linear system.
 
You only need an iterative procedure (such as Levenberg Marquardt) if your problem is more complex, e.g. Z=y^A(1) + A(2)sin(x*y*A(3)).
0 Kudos
Message 7 of 30
(9,026 Views)
Hi altenbach,
 
 
I have a similar problem with 3D surface but with subtle differences. My application works in two phases. In the first calibration phase, it acquires readings, e.g. Sp for various standard values of P, Y. So we now have three 1D arrays. By plotting a standard relation (3D interpolation polynomial), the second stage requires calculation of Sp for any values of P and Y.
 
Can we use your program 3DPolyFitFULL3D_12.llb ???
 
 
Thanks and Regards,
ViHAR.
0 Kudos
Message 8 of 30
(8,378 Views)

 


@Vihar wrote:
I have a similar problem with 3D surface but with subtle differences.

Well, if the difference are only subtle, you can just start with my example and make some "subtle changes". 🙂

Sorry, I don't understand your problem description. can you make a small example with example data?

0 Kudos
Message 9 of 30
(8,365 Views)

Hi again,

 

I am sorry, but I am not a programmer and not very familiar with LabView either. Let's consider this example...

In phase one (as mentioned in my previous post), a calibration table is made for several readings of pitch (P) and yaw (Y), this is saved to a CSV spreadsheet file. Now there are four different coefficients involved that require four individual surface plots, so we shall consider only one, Sp, which is a function of both P and Y. I am attaching a spreadsheet file (with first row headers), with P in column 0, Y in column 1 and Sp in column 11.

All I need to do is to establish a parametric relation between these three variables. Once the relation is established (by way of an equation), I can proceed to phase two.

Phase two involves using the equation to calculate Sp for known (but non-standard) values of P and Y.

I hope this makes the situation clearer.

 

Regards,

ViHAR.

0 Kudos
Message 10 of 30
(8,356 Views)