1

I'm trying to build a tracker differentiator using Matlab S-Function. However, it's giving me an error saying "Output returned by S-function 'han_td' in 'ex_han/S-Function' during flag=3 call must be a real vector of length 2".

I can't see what I'm doing wrong. Below is the code:

function [sys,x0,str,ts]=han_td(t,x,u,flag,r,h,T)           
switch flag,
    case 0, [sys,x0,str,ts]=mdlInitializeSizes(T);          
    case 2, sys = mdlUpdates(x,u,r,h,T);                    
    case 3, sys = x;                          
    case {1, 4, 9}, sys = [];                               
    otherwise, error(['Unhandled flag = ',num2str(flag)]);  
end;

%initialization function mdlInitializeSizes
function [sys,x0,str,ts] = mdlInitializeSizes(T)    
sizes = simsizes;                       
sizes.NumContStates = 0;                
sizes.NumDiscStates = 2;                
sizes.NumOutputs = 2;                   
sizes.NumInputs = 1;                    
sizes.DirFeedthrough = 0;               
sizes.NumSampleTimes = 1;               
sys = simsizes(sizes);                  
x0 = [0;0];                            
str = [];                             
ts = [-1 0]; 


function sys = mdlUpdates(x,u,r,h,T)    
sys=[x(1)+T*x(2); x(2)+T*fst2(x,u,r,h)];

function f=fst2(x,u,r,h)
delta =r*h;
delta0=delta*h;
y0=x(1)-u+h*x(2);
a0=sqrt(delta*delta+8*r*abs(y0));
if abs(y0)<=delta0, a=x(2)+y0/h;
else, a=x(2)+0.5*(a0-delta)*sign(y0);  end
if abs(a)<=delta, f=-r*a/delta;
else, f=-r*sign(a);end
D.Bombu
  • 11
  • 1
  • There are several errors, the model updates outputs a 1 array, but 2 are expected, the sampling time is bad defined perhaps, should be promptly fixed as per my preference, and the flag 3 should be 2 array and maybe this is only 1 array. Check the dimensions of every flag at the startup and come back if you have problems...... – Brethlosze May 11 '17 at 04:02

0 Answers0