From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

Solving a system of nonlinear equations

Solved!
Go to solution

Hey guys,
I am currently trying to solve a set of nonlinear equations in LabView with the help of MathScript and MATLAB code. I'm trying to create a working code since hours, but I haven't achieved this goal yet.

 

System of equations:

 

x^2 + y^2 + z^2 - R1^2 == 0
x^2 + (y-Y0)^2 + z^2 - R2^2 == 0
(x-X0)^2 + y^2 + z^2 - R3^2 == 0

 

R1, R2, R3, X0, Y0 are known.

 

Possible solution in MATLAB (assuming X0 = Y0 = 100, R1^2 = R3^2 = 11000, R2^2= 9000):

Create myfun.m with content:
function F = myfun(x);
F = [x(1)^2 + x(2)^2 + x(3)^2 - 11000;
x(1)^2 + (x(2)-100)^2 + x(3)^2 - 9000;
(x(1)-100)^2 + x(2)^2 + x(3)^2 - 11000];
end

 

Set intial value for iteration:
x0 = [50 50 50]

 

Solve:
fsolve(@myfun, x0)

 

=> Works fine!

 

Now I tried to just plug all of this stuff into a MathScript Node:

 

function F = myfun(x);
F = [x(1)^2 + x(2)^2 + x(3)^2 - 11000;
x(1)^2 + (x(2)-100)^2 + x(3)^2 - 9000;
(x(1)-100)^2 + x(2)^2 + x(3)^2 - 11000];

x0 = [50 50 50];

Sol = fsolve(@myfun, x0);

 

This is where the problem starts.

 

When I try to run the program, I get the follwing error message:

 

A function is defined in this script. You must save and call the
function with appropriate input values to execute the function.

 

I can't really figure out, what I did wrong and I would be very thankful, if somebody could help me.

Regards,
fuchrist

0 Kudos
Message 1 of 8
(9,228 Views)
Solution
Accepted by fuchrist

Hi,

 

MathScript does not support inline user-defined function (UDF). What you need to do is

 

  1. Define the UDF in a .m file. Give that .m file the same name as your UDF.
  2. Save the .m file in MathScript Search Path. By default, the search path is the LabVIEW Data folder.
  3. Pass the name of .m file as the first string input to function fsolve in MathScript Node.

I attach my .m file and snapshot of MathScript Node. The result I got is [50, 60, 70] which should be correct.

 

MSNode.png

Message 2 of 8
(9,219 Views)

Cheers ttrr,
I tried to use more or less the same solution, but I didn't indicate the string with ' '. -.-

Thanks a lot!
fuchrist

0 Kudos
Message 3 of 8
(9,212 Views)

Sorry, I just came up with another question.
As the function defined above is just a sample with fixed values, it serves as a proof of concept and I just tried to adapt it to my real problem:


function F = myfun(x);
F = [x(1)^2 + x(2)^2 + x(3)^2 - a^2;
x(1)^2 + (x(2)-Y0)^2 + x(3)^2 - b^2;
(x(1)-X0)^2 + x(2)^2 + x(3)^2 - c^2];

 

Do you probably also know, how to use variables, which are plugged into the MathScript node, within the defined function?

Best wishes,

fuchrist

0 Kudos
Message 4 of 8
(9,209 Views)

Hi,

 

MathScript function fsolve does not support additional variables. A workaround is to define variables to be global in both UDF and MathScript Node.

function F = myfun(x);
global a, b, c
F = [x(1)^2 + x(2)^2 + x(3)^2 - a^2;
x(1)^2 + (x(2)-Y0)^2 + x(3)^2 - b^2;
(x(1)-X0)^2 + x(2)^2 + x(3)^2 - c^2];

 

0 Kudos
Message 5 of 8
(9,205 Views)

Is it possible to use the fsolve functio on the real time target (eg. cRIO-9025 controller).

 

please help.

0 Kudos
Message 6 of 8
(8,633 Views)

Hi,

 

fsolve function is not supported on real-time target.

0 Kudos
Message 7 of 8
(8,624 Views)

ok...thanks

0 Kudos
Message 8 of 8
(8,612 Views)