LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

global variables in lsqfit function

Hi all

 

My question is two pronged.

I am working on fitting a function to a curve using lsqfit function in the mathscript node.

The function to be fitted is defined as:

F = k*(1+erf((x-a)/(sqrt(2)*m))/2;

 

the values of k and m are varied in the labview VI using For loops and each value is to be passed to the mathscript function a (saved as a.m).I used global function to pass the values between the mathscript node and the mathscript window as shown in the attached VI.

 

My script file for the function reads something like this

 

function [F,df] = a(a,x);

global k

global m

F = k*(1+erf((x-a)/(sqrt(2)*m))/2;

df = zeros(0,0);

 

But I get an error that k and m are undefined and doesnt run the VI.It would be great if you could help me with the reasons why it is doing so.

 

Also, in an attempt to make sure that the function is being called by mathscript node, I removed the variables k and m from the script file and ran the VI. It works fine except that it is excruciatingly slow which is not ideal for my case. I was wondering if there is a way of short circuiting this whole thing or defining the function in the VI itself.

 

Any help would be great.

 

Thanks

 

0 Kudos
Message 1 of 2
(2,414 Views)

Hi ankit12,

 

MathScript RT workspaces are not persistent between calls to the MathScript RT node, which is why k and m are showing up as undefined when you declare them in another node.

 

I'm assuming (from looking at your auto-indexing) that k and m are intended to be single values (not arrays)?

 

If so, you do not need to have any declarations of k and m in your second MathScript RT node.  The input terminals on the left (blue boxes with k and m) serve as those declarations in the scope of that MathScript RT node.

 

If you delete those declarations (lines 1-5 in your code), it should run just fine.

 

Have a great day,

 

Chris V

 

 

Applications Engineer
National Instruments
0 Kudos
Message 2 of 2
(2,330 Views)