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

7 ビュー (過去 30 日間)
Anas Din
Anas Din 2018 年 9 月 29 日
編集済み: mohammed lubbad 2018 年 10 月 8 日
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.

回答 (2 件)

Stephen23
Stephen23 2018 年 9 月 29 日
編集済み: Stephen23 2018 年 9 月 29 日
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 件のコメント
Stephen23
Stephen23 2018 年 9 月 29 日
@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 2018 年 9 月 29 日
@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.

サインインしてコメントする。


mohammed lubbad
mohammed lubbad 2018 年 10 月 8 日
編集済み: mohammed lubbad 2018 年 10 月 8 日
do you have this book anas ?? i f you need anything contact me @ lubbad1991@gmail.com i have something

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by