LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Questions about Lem-Mar nonlinear curve fitting vi

Solved!
Go to solution

Hello, I'm trying to fit my data for my model and stucked now.

 

Before asking, here's my model to fit :

 

t = k*I + Ja + uw + T*tanh(A*w)

 

I have same numbers of data series of I, a, w, t.

I want to figure out best fit parameter sets of K, J, u, T, A.

I know this is underconstrained and there might be much of solutions but it's okay anyway.

Any of the solution is fine to use in my application.

 

I tried to build a running vi and model vi, and the vi runs and gives me very crude answer set.

Which means, it only goes 10~12 iterations and it ends and gives final solution, even though I set a ending criteria very strictly.

 

I wanna run at least 10000 or more iterations to fit my data, but it doesn't seem to give an advance.

 

May I ask for an advice in this situation?

below are my vi's and data sets.

0 Kudos
Message 1 of 7
(2,952 Views)

Why do you care how many iteration it takes to find a solution? If you get a good fit, the number of iterations does not matter. If it is within the termination parameters after 10 iterations, running it 9990 more times will not improve the fit much. The Nonlinear Curve Fit.vi will stop when it gets a good fit so you cannot force it to run more iterations.

 

You have the maximum iterations set to 10 in the termination cluster on the block diagram.

 

Are you sure your model is good? When I run the raw data on the model with the initial parameters, the amplitude range is -5000 to +3000 compared to +3 to +60 on the measured torque.  That makes me think there may be a problem in the model or the initial parameters.  The calculated best fit graph shape is very different from the measured results also.

 

Except for the J scale factor the output of the model looks almost exactly like the acceleration. The measured torque does not have the same shape as any of the input measured data, so the model should not be dominated by one component.

 

Lynn

0 Kudos
Message 2 of 7
(2,903 Views)
Solution
Accepted by topic author 이상엽

The real problem is in Fitting model.vi. The partial derivatives are wrong. Actually each partial derivative is calculated correctly but they are in the wrong order in the f'(X,a) array. The parameters in the a array are k J u T A. The derivatives are dt/dJ, dt/dk, dt/du, dt/dT, and dt/dA. Note that the dJ and dk derivatives are in opposite order compared to the parameters. When I change the order, the fit looks much like the measured data and the residue is much smaller.

 

Many times it is not necessary to explictly calculate the partial derivatives. The fitting VI will perform numerical differentiation if the f'(X,a) array is left empty.

 

Most math functions are polymorphic. They can take arrays as inputs as well as scalars. The fitting model VI does not need the for loop when you take advantage of the polymorphism.

 

Lynn

 

Fitting model 2.png

0 Kudos
Message 3 of 7
(2,899 Views)

 

Thank you for the careful advice! Okay, the problem was definitely the order of the array of partial derevatives.

And also I had to transpose the array, which I couldn't even imagine. 

Why I mentioned about the iterations were just because I thought the model was right and the vi was just malfunctioning. 

After fixing the model everything is going right.

 

thanks again,

 

Sang-Yeop, Lee

 

0 Kudos
Message 4 of 7
(2,879 Views)

As the Forum's fitting expert, altenbach, often points out, it can be very useful to plot the data and the results of the model. The eye-brain combination can see patterns which may be hard to describe mathematically but clearly indicate that something is not right.  Tha visualization helped me spot some of these things.

 

Just out of curiosity I disconnected the derivatives output from the model VI. The results were the same but it took >10 times more iterations. So the numerical dfiferentiation process works well but may not be as fast.

 

Another thing I noticed: The results depend very strongly on the initial parameters estimate. I changed the constant to a control and varied the values. In particular the value of T seems to be irrelevant. T= -500 => fitted T = -583.902. T = +500 => fitted T = +422.127. T = 5, fitted T = -392.557.  For each of those values the residue remains between 10 and 13 and the number of iterations is less than 15.  This suggests to me that the model is still not well matched to the real system.

 

Lynn

 

 

0 Kudos
Message 5 of 7
(2,839 Views)

@johnsold wrote:

Just out of curiosity I disconnected the derivatives output from the model VI. The results were the same but it took >10 times more iterations. So the numerical dfiferentiation process works well but may not be as fast.


You need to make sure to differentiate between iterations and function calls.

 

The fitting VI only outputs the number of function calls which is only equal to the number of iterations if you do your own derivatives inside the model. If you don't provide derivatives, calculations of the numerical partial derivatives is done by additional function calls. If you have 5 parameters, each iteration will take 11 function calls (one for the function and two for each parameter, because the build-in partial derivatives are calculated two-sided.

 

If analytical derivatives are not possible, It typically still pays to do your own numerical derivatives inside the model. First you can ~double the speed by taking one-sided derivative (which is typically sufficient) and you can also parallelize all the function calculations needed for the derivatives.

This can significantly speed up your fitting on multicore hardware (example).

 

 

Message 6 of 7
(2,817 Views)

Thank you for the explanation.

 

Lynn

0 Kudos
Message 7 of 7
(2,810 Views)