LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

lsqcurvefit

Hello eveybody,

I'm using the mathscript node to fit a matrix (xdata) of reference data to my measurement (ydata). I first used the GLS from labview; but since I cannot define constraints to this function, I wrote a .m file using lsqcurvefit to do my fitting. This file works in matlab, but not in my mathscript. The error I get is that the '@' sign is an unexpected character. Can someone tell me whow to fix this?

my codes:

eerstefit.m
    function [x] = eerstefit(xdata,ydata)

    x0=[0.01 0.01 0.01 0.01 0.01]
    LB=[0 0 0 0 0]
    UB=inf

    k = lsqcurvefit(@tweedefit, x0, xdata, ydata, LB, UB)
    x = transpose (k)

tweedefit.m
    function [F] = tweedefit(x, xdata)

    a=(xdata(:,1))/1000;
    b=(xdata(:,2))/1000;
    c=xdata(:,3);
    d=(xdata(:,4))/1000;
    e=xdata(:,5);
    f=(xdata(:,6))/1000;

    F = x(1)*a+x(2)*b+x(3)*d+x(4)*e+x(5)*f
0 Kudos
Message 1 of 5
(9,766 Views)
Hello,

Unfortunately, there are two problems with your files.  The first is, as you noticed, that the '@' sign is not understood by LabVIEW MathScript.  This is a known issue and we will consider adding support for it in a future release.  Most functions that work by taking such a parameter will work in MathScript by simply passing the name of the function as a string parameter.  For example, see the help for ode45.

The second problem is that the lsqcurvefit function is not currently supported by MathScript.  We will also consider adding this in a future release.  At this time, the best we can do is recommend a LabVIEW graphical approach to constrained fitting.  See the attached project below for an example of how to do this (requires LabVIEW 8.5).

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
0 Kudos
Message 2 of 5
(9,756 Views)
Thank you very much, it works well now 😄
0 Kudos
Message 3 of 5
(9,748 Views)

Hi there,

I have about the same problem using a fitting procedure in MathScript in LabVIEW. The error I get is:

Error code: 1050

LabVIEW:  Error occurred while executing script. Error message from server: ??? Error using ==> optim\private\lsqncommon at 98
LSQNONLIN cannot continue because user supplied objective function failed with the following error:
Error using ==> feval
Undefined function or method 'fitfunctiepsidelta' for input arguments of type 'double'.

Error in ==> lsqnonlin at 181
[x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...

.
 in psidelta.vi->ellipsfront.vi

 

Using the script

 

for i=1:length(En)

    a=agem(i);
    b=bgem(i);
    gam=gamma(i);
    P=Pp(i)*pi/180;
    Pp(i)=P;
    if i==1
        c=[(45*pi/180) 0];
    else
        c=cc;
    end

cl=[0 0];
cu=[(180*pi/180) (180*pi/180)];

options =optimset('TolX' ,0.000001);
fitnaam='@fitfunctiepsidelta';
[cc,resnorm,residual,exitflag,output,lambda,jacobian]=lsqnonlin(@fitfunctiepsidelta,c,cl,cu,options);
%varcovar=inv(jacobian'*jacobian)*resnorm

psideltavalues(i,2)=cc(1);
psideltavalues(i,3)=cc(2);


end

 

where i now use fitnaam as an input string, as '@fitfunctiepsidelta' didn't work as an input directly as well. Could you tell me what i am doing wrong here?

Thanks in advance,

Pepijn

0 Kudos
Message 4 of 5
(9,677 Views)
Hello Pepijn,

Error 1050 is an error reported from the MATLAB® script node, not the MathScript node.  The error details it returns seem to indicate that it could not find the function you wish to use for fitting (fitfunctiepsidelta).  Most likely this has to do with your path setting.  Try calling the path command to add to the search path the location of that function on disk.  For example,
path(path, 'c:\myfuncs');

MATLAB® is a registered trademark of The MathWorks, Inc.

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
0 Kudos
Message 5 of 5
(9,619 Views)