LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

Solving System of Nonlinear Equations on CRIO

Solved!
Go to solution

I am trying to solve a system of nonlinear equations as an input for my real-time control loop.I have tried several methods and nothing is working. I tried using the Mathscript RT Module and the function fsolve_zeros('myfun', [4, 1]) but when I go to run it on my CRIO it says that it is not supported on the device. I don't quite understand this because it says that it is real time compatible, but that function is also listed under not supported for the Labview run time engine. Can I use this on my CRIO to solve my system of equations??

 

I also tried to use the Labview VI, nD Nonlinear System Solver but the solutions were not correct, and it is also taking about 5 seconds for it to solve on the CRIO. I would like my effective loop frequency to be 1 Hz, so I need the system of equations to be solved in less than one second.

 

I have also thought about using the CRIO to interface with my analog input/out cards, using shared variables with My Computer and running the Mathscript function on my laptop, but I cannot figure out how to get a shared variable to link between the two running VIs. I set up a simple project, my CRIO increments a Network shared variable once a second, and using the system inspector I can see that variable changing. Then when I run the VI on My Computer that just looks at that shared variable and displays it to an indicator, the code running on the CRIO stops.

 

Please send help!

0 Kudos
Message 1 of 9
(9,740 Views)

wrote:

 

I also tried to use the Labview VI, nD Nonlinear System Solver but the solutions were not correct, and it is also taking about 5 seconds for it to solve on the CRIO. I would like my effective loop frequency to be 1 Hz, so I need the system of equations to be solved in less than one second.

 

 


I am curious if you used the version with the formula model or with the VI model. Typically the VI model version should be significantly faster. (Parsing and evaluating text formulas is very expensive!)

 

(Sorry, I can't help you with the Mathscript question).

0 Kudos
Message 2 of 9
(9,719 Views)

I was using the formula model. Are there any examples to show how to use the VI model? I was having a hard time figuring out how exactly to declare my input functions. 

 

Aside from the timing it is also concerning to me that the answer was coming out incorrectly. For example, I put in guesses of [4,1], the solution is [3,0] and the output was like [-1.5, 10].

0 Kudos
Message 3 of 9
(9,717 Views)

Can you attach some simplified sample code? Are you sure the model formulas were correct? (there are some differences in syntax)

0 Kudos
Message 4 of 9
(9,713 Views)

Altenbach, I can attach sample code, but my development machine does not have a connection to the internet so it is some work.

 

I will add that when I run the Mathscript code in Labview on My Computer it calculates the answer correctly. The problem is only apparent when I try to run it on the CRIO target. It seems that Labview does not support running fsolve_zeros() on the CRIO, which to my knowledge is the only function for solving a system of nonlinear equations. 

 

My main Mathscript node is: 

fsolve_zeros('myfunc', [4,1]);

 

And myfunc is an m file that defines my nonlinear system of equations. It would not build on My Computer if the syntax was wrong, so I don't think that is the problem.

 

EDIT: I did not realize that you were the same as the comment above. Yes I checked to make sure that the strings I put together were syntactically correct. They were as follows:

 

va*x0 + vb*x1 + vd*x0*x1 + ve*x0*log(1/T) + vg*x0^2*x1 + vi*x1^2*x0 + vk*x0^2*x1^2 + vp*x1^3*x0 + vq*x1^3*log(1/T) + vr*x0^3*x1^2 + vs*x0^3*x1*log(1/T) + vv*x1^3*x0^2 + vy*x0^3*x1^3 + vz - visc;

 

ca*x0 + cb*x1 + cc*log(1/T) + cd*x0*x1 + ce*x0*log(1/T) + cf*x1*log(1/T) + cl*x0^2*x1*log(1/T) + cm*x1^2*x0*log(1/T) + co*x0^3*log(1/T) + ct*x0^2*x1^2*log(1/T) + cu*x1^3*x0*log(1/T) + cx*x1^3*x0^2*log(1/T) + cz - cond;

 

However, all the constants are defined in my code. so va, vb, ... are actually numbers. (including T, cond, visc)

 

for example it looks more like: 1.5*x0 + 2*x1 .. etc..

0 Kudos
Message 5 of 9
(9,694 Views)

My development machine is not connected to the internet, but I had an array of two strings as follows:

 

va*x0 + vb*x1 + vd*x0*x1 + ve*x0*log(1/T) + vg*x0^2*x1 + vi*x1^2*x0 + vk*x0^2*x1^2 + vp*x1^3*x0 + vq*x1^3*log(1/T) + vr*x0^3*x1^2 + vs*x0^3*x1*log(1/T) + vv*x1^3*x0^2 + vy*x0^3*x1^3 + vz - visc;

 

ca*x0 + cb*x1 + cc*log(1/T) + cd*x0*x1 + ce*x0*log(1/T) + cf*x1*log(1/T) + cl*x0^2*x1*log(1/T) + cm*x1^2*x0*log(1/T) + co*x0^3*log(1/T) + ct*x0^2*x1^2*log(1/T) + cu*x1^3*x0*log(1/T) + cx*x1^3*x0^2*log(1/T) + cz - cond;

 

All coefficients (va-vz and ca-cz) are actually doubles, and I inserted them inline. So for example it is actually "1.5*x0 + 2 * x1 ..." for the first equation. T, visc, and cond are also doubles. The only letters aside from function names in my equations are x0, x1, and the log function call.

0 Kudos
Message 6 of 9
(9,699 Views)

Josh,

 

When it comes to solving these systems of nonlinear equations on cRIO goes in general, an effective loop rate of 1 kHz is reaching the upper limit of loop rates when it comes to this sort of thing on LabVIEW Real-time. Have you considered using the FPGA on your cRIO to accomplish this task?

 

For network published shared variables, I'd check out the following white paper for an overview of their usage: https://www.ni.com/en/support/documentation/supplemental/06/using-the-labview-shared-variable.html . From there, we have a few good examples of transferring data from a RT VI to a host through the network without impacting determinism. Go to help > find examples > search > and search for 'RT FIFO Variables - Networked.lvproj'

 

Hope some of this gets you headed the right direction!

CH
Applications Engineering
National Instruments
http://www.ni.com/en-us/support.html
0 Kudos
Message 7 of 9
(9,670 Views)
Solution
Accepted by topic author joshtombs

I ended up solving the problem, and wanted to post my solution in case anyone else experiences this.

 

You cannot use the Mathscript Module to solve systems of nonlinear equations on CRIO devices, technical support confirmed that there is an error in their documentation and the fsolve function is not supported on real-time devices.

 

So I was required to use the nD Nonlinear Systems Solver VI to solve my system. The reason why I was getting an "incorrect" answer when I was using the formula instance of this VI was because LabVIEW treats the "log" function as the log base 10, while my MATLAB code treats "log" as the natural logarithm. I needed to change the formula to "ln" to get it to work properly.

 

With that said, I implemented it using the VI instance and it solve the system correctly, and much faster than the formula instance. This is the way to go if you have a similar problem.

Message 8 of 9
(9,575 Views)

@joshtombs

Hello, Is there an example for using the VI instance (the faster version). I found an example for using the formula instance, but not the fast VI option. I saw there is a template of some kind…. Thanks, Ilan

0 Kudos
Message 9 of 9
(7,496 Views)