MATLAB Answers

S-Function does not exist

27 views (last 30 days)
furkan mola
furkan mola on 7 May 2020
Edited: Walter Roberson on 13 May 2020
I'm trying to simulate this system but I gave an error that "s-function does not exist." I'm new at Simulink and I'm trying to learn with examples.
%% chap1_2ctrl.m
function [sys,x0,str,ts] = spacemodel(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
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 = 3;
sizes.NumInputs = 3;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 0;
sys = simsizes(sizes);
x0 = [];
str = [];
ts = [];
function sys=mdlOutputs(t,x,u)
r=u(1);
dr=cos(t);
ddr=-sin(t);
th=u(2);
dth=u(3);
c=15;
e=r-th;
de=dr-dth;
s=c*e+de;
fx=25*dth;
b=133;
epc=5;k=10;
ut=1/b*(epc*sign(s)+k*s+c*de+ddr+fx);
sys(1)=ut;
sys(2)=e;
sys(3)=de;
%% chap1_2plant.m
function [sys,x0,str,ts]=s_function(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 = 0;
sys=simsizes(sizes);
x0=[-0.15 -0.15];
str=[];
ts=[];
function sys=mdlDerivatives(t,x,u)
sys(1)=x(2);
sys(2)=-25*x(2)+133*u;
function sys=mdlOutputs(t,x,u)
sys(1)=x(1);
sys(2)=x(2);
%% chap1_2plot.m
close all;
figure(1);
plot(t,y(:,1),'k',t,y(:,2),'r:','linewidth',2);
legend('Ideal position signal','Position tracking');
xlabel('time(s)');ylabel('Angle response');
figure(2);
plot(t,u(:,1),'k','linewidth',0.01);
xlabel('time(s)');ylabel('Control input');
c=15;
figure(3);
plot(e,de,'r',e,-c'.*e,'k','linewidth',2);
xlabel('e');ylabel('de');

  1 Comment

Walter Roberson
Walter Roberson on 13 May 2020
Which one is it saying cannot be found?
I notice that the names of the functions do not match the names of the files. I do not know if that matters to Simulink (for MATLAB itself it would give a warning but would use the name of the file instead of the name on the function line.)

Sign in to comment.

Answers (1)

Mark McBroom
Mark McBroom on 13 May 2020
Make sure that the .m files are in the MATLAB current working directory or on the MATLAB path.

  1 Comment

furkan mola
furkan mola on 13 May 2020
thank you but there are all .m files in current directory.

Sign in to comment.