LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Using lsqfit (MathScript RT Module Function)

Dear LabVIEW team,

 

I tried to run the example given on the lsqfit page:

http://zone.ni.com/reference/en-XX/help/371361G-01/lvtextmath/msfunc_lsqfit/

 

I made a file called myfit.m which contains: 

 

% The myfun function is defined by:
function [F, df] = myfun(a, xdata)
F = a(1)*xdata.^2 + a(2)*cos(xdata);
df = zeros(0, 0);

 

and a script named testfit.m (both in the same folder):

 

a = [2, 1.5];
x = 0:1:19;
noise = randnormal(size(x)) / 5;
y = a(1)*x.^2 + a(2)*cos(x) + noise;
[x, normres, resid] = lsqfit('myfun', [10, 10], x, y);

 

Then the output in MathScript is:

 

>>
Error in function lsqfit at line 5: The function you specified as an argument is invalid.

 

Line 5: [x, normres, resid] = lsqfit('myfun', [10, 10], x, y);

 

I am not familiar with mathscript / matlab (yet) so I don't see what is going wrong here. But as this is an example taken from your homepage I assume it should work.

 

Thanks,

Philipp Oertle


0 Kudos
Message 1 of 4
(2,838 Views)

Hi Philipp,

 

Try renaming your "myfit.m" file to "myfun.m". The compiler is expecting the function to be defined in a similarly named file.

 

Cheers!

 

K Scott

0 Kudos
Message 2 of 4
(2,833 Views)

Oops, that was a tipo in the post! Sorry for that. The file is already named 'myfun.m'

 

Philipp

0 Kudos
Message 3 of 4
(2,829 Views)

Hmmm. I was sure that was the issue. Could you try to simplify it a little? For instance, placing the testfit.m contents into the MathScript Node directly and running the VI?

 

Additionally, what versions of LabVIEW/MathScript are you using and on what operating system?

 

Thanks,

 

-K Scott

0 Kudos
Message 4 of 4
(2,823 Views)