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

4 Ansichten (letzte 30 Tage)
Anas Din
Anas Din am 29 Sep. 2018
Bearbeitet: mohammed lubbad am 8 Okt. 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.

Antworten (2)

Stephen23
Stephen23 am 29 Sep. 2018
Bearbeitet: Stephen23 am 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 Kommentare
Stephen23
Stephen23 am 29 Sep. 2018
@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:
Anas Din
Anas Din am 29 Sep. 2018
@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.

Melden Sie sich an, um zu kommentieren.


mohammed lubbad
mohammed lubbad am 8 Okt. 2018
Bearbeitet: mohammed lubbad am 8 Okt. 2018
do you have this book anas ?? i f you need anything contact me @ lubbad1991@gmail.com i have something

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by