MATLAB Answers

0

I keep getting 'Undefined function or variable 't' ' after running the code

Asked by Anas Din on 29 Sep 2018
Latest activity Edited by mohammed lubbad on 8 Oct 2018
I am following an example in a MATLAB sliding mode control book, however the book does not do a good job of explaining how to follow the examples. the code from the book is:
function [sys,x0,str,ts]=spacemodel(t,x,u,flag)
switch flag
case 0
[sys,x0,str,ts]=mdlInitializeSizes;
case 1
sys=mdlDerivatives(t,x,u);
case 3
sys=mdlOutputs(t,x,u);
case {2,4,9}
sys=[];
otherwise
error(['Unhandled Flag =',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes
sizes=simsizes;
sizes.NumContStates=0;
sizes.NumDiscStates=0;
sizes.NumOutputs=1;
sizes.NumInputs=3;
sizes.DirFeedthrough=1;
sizes.NumSampleTimes=0;
sys=simsizes(sizes);
x0=[];
str=[];
ts=[];
function sys=mdlOutputs(t,x,u)
J=2;
thd=u(1);
th=u(2);
dth=u(3);
e=th-thd;
de=dth;
c=10;
s=c*e + de;
xite=1.1;
k=0;
%k=10;
ut=J*(-c*dth-1/J*(k*s+xite*sign(s)));
sys(1)=ut;
This part 1 of 3 .m scripts for this example. When I run the .m file I get the 'Undefined function or variable 't' ' message.
Also there is a SIMULINK model in the book that I am also trying to get working.

  0 Comments

Sign in to comment.

2 Answers

Answer by Stephen Cobeldick on 29 Sep 2018
Edited by Stephen Cobeldick on 29 Sep 2018

You do not explain how you are calling the function, but that is likely to be the problem. Perhaps you are trying to call it by copy-and-pasting the function definition and trying to run that?:
[sys,x0,str,ts] = spacemodel(t,x,u,flag)
The function requires four input arguments: you cannot just "run" it (e.g. by clicking the big green "Run" button) or "run" the file, you have to call it with all four input arguments, which have to be variables that exist in the workspace, e.g.:
A = 2;
B = 2;
C = 3;
function [sys,x0,str,ts] = spacemodel(A,B,C,1)
All well-written code also includes a help section at the beginning which explains what the code does, and specifies the input and output arguments. Note that this code will throw errors for some different flag input values when not all of the output arguments are defined... this is not very well written code.

  5 Comments

chap1_1ctrl.m (part 1) I have added the end argument to the end of all functions for this part of the code. In addition I have added the other 2 remaining parts of the code for you viewing and I have included a SIMULINK model in case it clarifies what the book is trying to achieve. The example in the book is trying to show me the difference in 3 different plots when changing the 'k'.
I am sorry for my lack of knowledge in explaining things.
t=1;
x=1;
u=1;
function [sys,x0,str,ts]=spacemodel(t,x,u,flag)
switch flag
case 0
[sys,x0,str,ts]=mdlInitializeSizes;
case 1
sys=mdlDerivatives(t,x,u);
case 3
sys=mdlOutputs(t,x,u);
case {2,4,9}
sys=[];
otherwise
error(['Unhandled Flag =',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes
sizes=simsizes;
sizes.NumContStates=0;
sizes.NumDiscStates=0;
sizes.NumOutputs=1;
sizes.NumInputs=3;
sizes.DirFeedthrough=1;
sizes.NumSampleTimes=0;
sys=simsizes(sizes);
x0=[];
str=[];
ts=[];
end
function sys=mdlOutputs(t,x,u)
J=2;
thd=u(1);
th=u(2);
dth=u(3);
e=th-thd;
de=dth;
c=10;
s=c*e + de;
xite=1.1;
k=0;
%k=10;
ut=J*(-c*dth-1/J*(k*s+xite*sign(s)));
sys(1)=ut;
end
Done, and still the same message.
chap1_1plant.m (part 2)
function [sys,x0,str,ts]=spacemodel(t,x,u,flag)
switch flag
case 0
[sys,x0,str,ts]=mdlInitializeSizes;
case 1
sys=mdlDerivatives(t,x,u);
case 3
sys=mdlOutputs(t,x,u);
case {2,4,9}
sys=[];
otherwise
error(['Unhandled Flag =',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes
sizes=simsizes;
sizes.NumContStates=2;
sizes.NumDiscStates=0;
sizes.NumOutputs=2;
sizes.NumInputs=1;
sizes.DirFeedthrough=0;
sizes.NumSampleTimes=1;
sys=simsizes(sizes);
x0=[0,0];
str=[];
ts=[0 0];
function sys=mdlDerivatives(t,x,u)
J=2;
dt=sin(t);
ut=u(1);
sys(1)=x(2);
sys(2)=1/J*(ut+dt);
function sys=mdlOutputs(t,x,u)
sys(1)=x(1);
sys(2)=x(2);
chap1_1plot.m (part 3)
close all;
figure(1)
plot(t,y(:,1),'r',t,y(:,2),'k:','linewidth',2);
xlabel('time(s)');ylabel('Step response');
figure(2)
plot(t,ut(:,1),'r','linewidth',2);
xlabel('time(s)');ylabel('Control input');
c=10;
figure(3)
e=y(:,2)-y(:,1);
de=y(:,3);
plot(e,de,'r',e,-c'.*e,'k','linewidth',2)
xlabel('e');ylabel('de');
@Anas Din: Read my answer again. You call the function from the command window like this:
But where have you defined t, x, u and flag ? What values do you expect them to have, if you have not defined them?
If you are expecting to use this function with Simulink, then you should read the documentation about how to call function from Simulink. While trying things and experimenting is a great idea, it is no replacement for actually reading the documentation:
@Stephen Cobeldick, thank you for your help, I will read the documentation and get back to you ASAP.
P.S Thank you for your help so far.

Sign in to comment.


Answer by mohammed lubbad on 8 Oct 2018
Edited by mohammed lubbad on 8 Oct 2018

do you have this book anas ?? i f you need anything contact me @ lubbad1991@gmail.com i have something

  0 Comments

Sign in to comment.