LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how mathscript node work when i have user defiend matlab function

hi iam stuck in middle

 

i want to use math script & call the matlbfunction which i wrote in matlab

 

but my vi showing this error

 

Error in function ip at line 2.  The indexes are out of bounds for the variable you specified.

i have attached the file

 

what eror iam doing in that

 thanks

 

0 Kudos
Message 1 of 14
(4,964 Views)

Hello,

Your input variable ip is a scalar.  The problem is that you are trying to index ip on line 2 (which is actually the first line of your code since line 1 is an empty line).   When you try to index the second (or later) element out of ip, an error is returned since a scalar only has one element.  Change your input variable to a vector and you should be all set.

One thing to note is that you have many statements on each line of your script.  If you split the first couple lines of your script after each semicolon, you will have only one statement per line.  This can make debugging easier.

Grant M.
Senior Software Engineer | LabVIEW MathScript | National Instruments

Message 3 of 14
(4,943 Views)

Hi,

GrantM

thanks for ur replay

 

plz check my attached file

this is function which i have wrote in matlab 7.9(r2009b) , which is matlab function whom iam calling in simulink model in matlab & it is working well

same thing i want to implement through labview(iam using labview 8.2) by using math script node

as in simulink i just write the name of function & it call it(the matlab function..

so could u se my attached file & suggest me could iam doing in right direction ..

or could u give me an example of my program

i have already read the examples on math script function how it work?

as u mentioned i could used ip as vector? is any direct way in labview to change numeric control of scalr to vector .. as in intial question

 

thanks in adnace for ur cooperation

tc

0 Kudos
Message 4 of 14
(4,929 Views)

Hello,

I only had to make a few changes to get your file to work.  First, rename your myfunction.txt to myfunction.m.  All MathScript user-defined functions must have a .m extension.  Second, place this .m file in your "My Documents\LabVIEW Data" directory.  This is the default search path where MathScript looks for user-defined functions.  Third, make sure the function has the same name as the file.  I had to change it from "myimplement" to "myfunction" since the file is myfunction.m.  Finally, remove the end statement from the bottom of the function.  Although the end statement is optional in a function definition, explicitly using it is a bug in MathScript.

There is not a direct way to change a scalar control to an array.  But all you need to do is drop an empty array control on the front panel and move the scalar control into it.

Grant M.
Senior Software Engineer | LabVIEW MathScript | National Instruments

Message 5 of 14
(4,911 Views)

Hi

GrantM

 

i have done all that u have mentioned in ur reply

rename file name as myimplement.m put it into C:\Documents and Settings\User\My Documents\LabVIEW Data

nd have remove the end statement

but again the same problem it is nt working

could u plz explain me by example

iam trying to do it

thanks a lot

tc

 function [ op ] = myimplement( ip )
 

% PARSE INPUT AND COMPUTE NECESSARY DIMENSIONS


n=ip(1);m_u=ip(2);l_y=ip(3);l_z=ip(4);p=ip(5);
mu=ip(6);mu_c=ip(7);n_c=ip(8);Bzun=ip(9);

p_c=mu+n+p-1;
q1=n_c*m_u;
q2=(n_c+mu_c-1)*l_y;
lam=mu_c+n_c+p_c-2;

lBzuv=(p*l_z)*(p_c* m_u);% Length of vectorized Bzu
Bzuv=ip(10:lBzuv+9);% Bzu vector
Bzu=reshape(Bzuv,p*l_z,p_c*m_u);% Bzu matrix
ipptr=lBzuv+9+1; % input pointer, indicates position of next data in ip

lx_u=lam*m_u;% Length of delayed u's
x_u=ip(ipptr:lx_u+ipptr-1);% delayed u's
ipptr=lx_u+ipptr;% update input pointer

lx_y=lam*l_y;% Length of delayed y's
x_y=ip(ipptr:lx_y+ipptr-1);% Delayed y's
ipptr=lx_y+ipptr;% update input pointer

lx_z=(p-1)*l_z;% Length of delayed z's
x_z=ip(ipptr:lx_z+ipptr-1);% Delayed z's
ipptr=lx_z+ipptr;% update input pointer

lthetav=m_u*(q1+q2);% Length of delayed z's
thetav=ip(ipptr:lthetav+ipptr-1);% Theta vector
theta=reshape(thetav,m_u,(q1+q2)); % Form theta matrix
ipptr=lthetav+ipptr;% update input pointer

yk=ip(ipptr:l_y+ipptr-1);% Current y
ipptr=l_y+ipptr;% update input pointer

zk=ip(ipptr:l_z+ipptr-1); % current z


% Construct Phi_uy


Phi_uy=[x_u(((mu_c-1)*m_u)+1:lam*m_u);x_y];

% Compute control signal u(k)

R1=[eye(q1) zeros(q1,(p_c-1)*m_u) zeros(q1,q2) zeros(q1,(p_c-1)*l_y);...
    zeros(q2,q1) zeros(q2,(p_c-1)*m_u) eye(q2) zeros(q2,(p_c-1)*l_y)];
uk=theta*R1*Phi_uy;

% Construct U(k) and Z(k)


U=[uk;x_u(1:(p_c-1)*m_u)]; Z=[zk;x_z];

% Compute Uhat(k) and Zhat(k)


Uhat=zeros(p_c*m_u,1);% Initialize uhat
for jj=1:p_c
        L=[zeros((jj-1)*m_u,m_u);eye(m_u,m_u);...
                zeros((p_c-jj)*m_u,m_u)];
        R=[zeros(q1,(jj-1)*m_u) eye(q1,q1)...
           zeros(q1,(p_c-jj)*m_u) zeros(q1,(jj-1)*l_y)...
           zeros(q1,q2) zeros(q1,(p_c-jj)*l_y);...
           zeros(q2,(jj-1)*m_u) zeros(q2,q1)...
           zeros(q2,(p_c-jj)*m_u) zeros(q2,(jj-1)*l_y)...
           eye(q2,q2) zeros(q2,(p_c-jj)*l_y)];
        Uhat=Uhat+L*theta*R*Phi_uy;
end
Zhat=Z-Bzu*(U-Uhat);


% Compute gradient


grndt=zeros(m_u,(q1+q2));% Initialize gradient
for jj=1:p_c
        L=[zeros((jj-1)*m_u,m_u);eye(m_u,m_u);...
                zeros((p_c-jj)*m_u,m_u)];
        R=[zeros(q1,(jj-1)*m_u) eye(q1,q1)...
           zeros(q1,(p_c-jj)*m_u) zeros(q1,(jj-1)*l_y)...
           zeros(q1,q2) zeros(q1,(p_c-jj)*l_y);...
           zeros(q2,(jj-1)*m_u) zeros(q2,q1)...
           zeros(q2,(p_c-jj)*m_u) zeros(q2,(jj-1)*l_y)...
           eye(q2,q2) zeros(q2,(p_c-jj)*l_y)];
        grndt=grndt+L'*Bzu'*Zhat*Phi_uy'*R';
end


% Compute parameter update step size eta(k)


Phi_uyn=Phi_uy'*Phi_uy;
if Phi_uyn==0
    etak=1;
else
    etak=1/(p_c*Bzun*Phi_uyn);
end


% Compute Theta(k+1)


thetakp1=theta-etak*grndt;

    

% Compute x_u(k+1) i.e


Ax_u=[zeros(m_u,lam*m_u);[eye((lam-1)*m_u) zeros((lam-1)*m_u,m_u)]];
Bx_u=[eye(m_u);zeros((lam-1)*m_u,m_u)];
x_ukp1=Ax_u*x_u+Bx_u*uk;


% Compute x_y(k+1) i.e


Ax_y=[zeros(l_y,lam*l_y);[eye((lam-1)*l_y) zeros((lam-1)*l_y,l_y)]];
Bx_y=[eye(l_y);zeros((lam-1)*l_y,l_y)];
x_ykp1=Ax_y*x_y+Bx_y*yk;

% Compute x_z(k+1) i.e

Ax_z=[zeros(l_z,(p-1)*l_z);[eye((p-2)*l_z) zeros((p-2)*l_z,l_z)]];
Bx_z=[eye(l_z);zeros((p-2)*l_z,l_z)];
x_zkp1=Ax_z*x_z+Bx_z*zk;

op=[x_ukp1;x_ykp1;x_zkp1;thetakp1(:)];


 %end

0 Kudos
Message 6 of 14
(4,896 Views)

Please post your actual VI. Do not post screenshots. Do not post Word documents that have screenshots embedded in them. Do not post scripts. Post actual LabVIEW code.

 

Also, please try to use full words instead of "Twitter-speak".

0 Kudos
Message 7 of 14
(4,891 Views)

hi

 

ok iam attaching my vi

please check& see the error

at home iam having older version of labview 8.2 it is showing this error

 

Error 1046 occurred at LabVIEW:  LabVIEW cannot initialize the script server.  Ensure the server software is installed. in implement.vi

 

but if u copy it to new version it shows the error of index out of bound?

b/c iam using input as scalar , unless my input('ip) which is vector ? so as in my previous how i can change it into Array input?/

thanks if any one can solve my issue

0 Kudos
Message 8 of 14
(4,877 Views)

You need to pay more attention to what people are telling you. You are trying to use the XMath node. This is not the same as the MathScript node which is what Grant was telling you he was using, and the instructions he gave you are for that.

 

 


b/c iam using input as scalar , unless my input('ip) which is vector ? so as in my previous how i can change it into Array input?/


You right-click on the input and using the pop-up menu you specify the data type. This is explained clearly in the LabVIEW Help.

 

0 Kudos
Message 9 of 14
(4,843 Views)

hi

how are u

 

ok iam attaching my vi

 

the error is

Error in function ip at line 7.  The indexes are out of bounds for the variable you specified.

where iam going wrong

i have kept my function file imlement.m in same library

C:\Documents and Settings\IAA\My Documents\LabVIEW Data

how i will sor out that problem

iam attaching my .m file also, which i want to implement through math script node

thanks if any one can help me out regarding my problem

take care

 

Download All
0 Kudos
Message 10 of 14
(4,804 Views)