LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using Levenberg-Marquardt fitting where f(x,a) is provided by another VI

Solved!
Go to solution

Hi there,

 

I currently have a vi (theta_solve(subvi)) where I have 2 inputs; a parameter and a x position. the result of this is the y position at the prescribed x (and some other outputs which arent necessary).

 

I wish to fit this to my experimental data by optimising for the one parameter. I have been attemping to use the Levenberg-Marquardt non-linear fitting vi but I can not seem to access my saved function vi. I continually get errors saying invalid vi ref.

 

Can anyone give me suggestions on if I am going about this the right way or any tips on how to get my code working?

 

I have attached my vi's for a reference.

 

Thanks very much!

Download All
0 Kudos
Message 1 of 18
(4,077 Views)

Your subVI has the wrong connector pane. Make sure the connections correspond exactly to the template. See the help for more details.

Message 2 of 18
(4,072 Views)

Sorry where is this template?

 

Thanks

0 Kudos
Message 3 of 18
(4,067 Views)

As I said, look in the help. Look for the details of the F(x,a) control.

 

Try: ...\vi.lib\gmath\NumericalOptimization\LM model function and gradient.vit  (relative to the root install point of your LabVIEW version.)

Message 4 of 18
(4,063 Views)

Thank you. That was able to recognise my vi however now I have an error saying no solution can be found because the matrix is singular.

 

I am now a little confused, is this a result of my specific function or my coding?

 

In my head, I am essestially trying to try different parameters continually until the correct (as as close to as possible) parameter choice is reached so that the model matches the data. If this was what I want to do am I approaching it the right way?

 

Any help would be much appreciated!

0 Kudos
Message 5 of 18
(4,056 Views)

You did not include the model VI. Also please fill all controls with typical values, make values the default, save the VI, and attach once more.

 

(For example, does the array size of your parameter estimates correspond to the number of parameters in the model?)

Message 6 of 18
(4,050 Views)

Hi I have got it working now. Thanks alot.

 

But now I have another related question which you may be able to help me with. In my current vi's, I am solving for 2 parameters. However one I will know, so this can just be a constant and not solved for. I could just wire this as a constant in my model however I want to be able to control it from my main vi (where i call the non linear solver) as this will vary from expt to expt.

 

So what I am asking is if it is possible to write to a control in my model vi (Theta_solve.vi) from the vi which calls the non linear solver?At the moment the correct parameter is just passed to the vi as a parameter to be optimised for.

 

If you could help it would be much appreciated!

Download All
0 Kudos
Message 7 of 18
(4,025 Views)

You can send any information you want via the data variant to the model VI, for example which ones of the parameters should be varied. How many parameters there are depends on the size of the parameter array, so you need to set that accordingly in the main VI. There are many ways to do that.

 

There are a couple of things that seem terribly wrong. What is the purpose of the outer FOR loop in the model VI? You are simply doing the same calculation N times! Once is probably enough, so delete the outer FOR loop. Right? 

Message 8 of 18
(4,019 Views)

Of course. I just followed template and wasnt really thinking! Sorted that now.

 

Data variants.... Do I need to make a reference in the model vi and then access that variable reference from the main vi? Do I pass the variable through the non linear solver or can I do it as an aside to it so it remains just a constant?

 

The variable theta0 is currently defined by a control in both vi's but now I want the control in the main vi to change both controls.

 

Download All
0 Kudos
Message 9 of 18
(4,011 Views)
Solution
Accepted by tlin067

Easiest is to write the model using all parameters, send all parameters via the data variant and then substitute the fittable parameters.

 

Here is a simple modification. Just run with default parameters to fit for both. Uncheck "Fit?" for one of the parameters to keep it at the guess value. Etc.

 

Basically, you take all possible parameters and a boolean array where you can pick which one's to fit. A small FOR loop selects the subset of parameters selected for fitting as well as their original location. In the model, you take all parameters and substitute the fittable ones.

 

You should call the model VI directly to simulate the data to keep the code simple. I took out your automatic guess calculation, so put that back if needed.

Message 10 of 18
(3,999 Views)