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.
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.
02-19-2014 12:14 PM
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
Solved! Go to Solution.
02-19-2014 08:29 PM
Hi,
MathScript does not support inline user-defined function (UDF). What you need to do is
I attach my .m file and snapshot of MathScript Node. The result I got is [50, 60, 70] which should be correct.
02-20-2014 02:52 AM
Cheers ttrr,
I tried to use more or less the same solution, but I didn't indicate the string with ' '. -.-
Thanks a lot!
fuchrist
02-20-2014 03:04 AM - edited 02-20-2014 03:06 AM
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
02-20-2014 03:09 AM
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];
08-14-2014 09:13 AM
Is it possible to use the fsolve functio on the real time target (eg. cRIO-9025 controller).
please help.
08-14-2014 08:39 PM
Hi,
fsolve function is not supported on real-time target.
08-15-2014 03:53 AM
ok...thanks