How to achieve a discrete differential tracker with the S function
Show older comments
1、The form of tracking the differential are as follows:

v(k) is input,h、r、α are parameters and user-defined. 2、My code is follows: function[sys,x0,str,ts,simStateCompliance] = myTry_sfun(t,x,u,flag,h,r,a) switch flag, case 0, [sys,x0,str,ts,simStateCompliance] = mdlInitializeSizes; case 1, sys = mdlDerivatives(t,s,u); case 2, sys = mdlUpdate(t,x,u,h,r,a); case 3, sys = mdlOutputs(t,x,u); case 4, sys = mdlGetTimeOfNextVarHit(t,x,u); case 9. sys = mdlTerminate(t,x,u); otherwise DAStudio.error('Simulink:block:unhandledFlag',num2str(flag)); end function [sys,x0,str,ts,simStateCompliance] = mdlInitializeSizes 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 = [0,0]; simStateCompliance = 'UnknownSimState';
function sys = mdlDerivatives(t,x,u) sys = [];
function sys = mdlUpdate(t,x,u,h,r,a) % updata state variable x_1 = x(1)+ h*x(2); x_2 = x(2)-h*((r^2)*(sign(x(1)-u))*((abs(x(1)-u))^a) + r * x(2)); sys = [x_1;x_2]; function sys = mdlOutputs(t,x,u) % update Output sys = [x(1);x(2)]; function sys = mdlGetTimeOfNextVarHit(t,x,u) % This function is not necessary sampleTime = 0.001; sys = t + sampleTime;
function sys = mdlTerminate(t,x,u) sys = [];
function sys = mdlUpdate(t,x,u,h,r) % updata state variable x_1 = x(1)+ h*x(2); x_2 = x(2)-h*((r^2)*(sign(x(1)-u))*((abs(x(1)-u))^a) + r * x(2)); sys = [x_1;x_2]; function sys = mdlOutputs(t,x,u) % update Output sys = [x(1);x(2)]; function sys = mdlGetTimeOfNextVarHit(t,x,u) % This function is not necessary sampleTime = 0.001; sys = t + sampleTime;
function sys = mdlTerminate(t,x,u) sys = []; 3、My Simulink is follows:

4、My result is follows:

I can get x_2 but can’t get x_1. I want to know where I was wrong, or maybe you can give me an example for discrete system with S function as a reference. Thank you !
Answers (0)
Categories
Find more on Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!