MATLAB: About S -function errors ,How to solve the problems

s-functionsimulink

when I writed codes in S-function and runed in matlab, I got two errors: (1)Error in 'hy_test_onlinelearning/S-Function' while executing MATLAB S-function 'hy_onlinelearning', flag = 2 (update), at time 0.0. (2)Too many input arguments. How can I solve these problems ? Thank you very much.
The codes are below: function [sys,x0,str,ts,simStateCompliance] =hy_onlinelearning(t,x,u,flag,G,I) switch flag,
%%%%%%%%%%%%%%%%%%

% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;
%%%%%%%%%%%%%%%

% Derivatives %
%%%%%%%%%%%%%%%
case 1,
sys=mdlDerivatives(t,x,u);
%%%%%%%%%%

% Update %
%%%%%%%%%%
case 2,
sys=mdlUpdate(t,x,u,G,I);
%%%%%%%%%%%

% Outputs %
%%%%%%%%%%%
case 3,
sys=mdlOutputs(x,u);
%%%%%%%%%%%%%%%%%%%%%%%

% GetTimeOfNextVarHit %
%%%%%%%%%%%%%%%%%%%%%%%
case 4,
sys=mdlGetTimeOfNextVarHit(t,x,u);
%%%%%%%%%%%%%

% Terminate %
%%%%%%%%%%%%%
case 9,
sys=mdlTerminate(t,x,u);
%%%%%%%%%%%%%%%%%%%%

% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
% end sfuntmpl
% %============================================================================= % mdlInitializeSizes % Return the sizes, initial conditions, and sample times for the S-function. %============================================================================= % function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes
% % call simsizes for a sizes structure, fill it in and convert it to a % sizes array. % % Note that in this example, the values are hard coded. This is not a % recommended practice as the characteristics of the block are typically % defined by the S-function parameters. % sizes = simsizes;
sizes.NumContStates = 0; sizes.NumDiscStates = 70; sizes.NumOutputs = 1; sizes.NumInputs = 7; sizes.DirFeedthrough = 1; sizes.NumSampleTimes = 1; % at least one sample time is needed
sys = simsizes(sizes);
% % initialize the initial conditions % x0 = zeros(sizes.NumDiscStates,1);
% % str is always an empty matrix % str = [];
% % initialize the array of sample times % ts = [-1 0];
% Specify the block simStateCompliance. The allowed values are: % 'UnknownSimState', < The default setting; warn and assume DefaultSimState % 'DefaultSimState', < Same sim state as a built-in block % 'HasNoSimState', < No sim state % 'DisallowSimState' < Error out when saving or restoring the model sim state simStateCompliance = 'UnknownSimState';
% end mdlInitializeSizes
% %============================================================================= % mdlDerivatives % Return the derivatives for the continuous states. %============================================================================= % function sys=mdlDerivatives(t,x,u)
sys = [];
% end mdlDerivatives
% %============================================================================= % mdlUpdate % Handle discrete state updates, sample time hits, and major time step % requirements. %============================================================================= % function sys=mdlUpdate(x,u) z1=0; err_goal=0.001; max_epoch=1000; for epoch=1:max_epoch w1=x(1)*u(1)+x(11)*u(2)+x(21)*u(3)+x(31)*u(4)+x(41)*u(5)+x(51)*u(6); w2=x(2)*u(1)+x(12)*u(2)+x(22)*u(3)+x(32)*u(4)+x(42)*u(5)+x(52)*u(6); w3=x(3)*u(1)+x(13)*u(2)+x(23)*u(3)+x(33)*u(4)+x(43)*u(5)+x(53)*u(6); w4=x(4)*u(1)+x(14)*u(2)+x(24)*u(3)+x(34)*u(4)+x(44)*u(5)+x(54)*u(6); w5=x(5)*u(1)+x(15)*u(2)+x(25)*u(3)+x(35)*u(4)+x(45)*u(5)+x(55)*u(6); w6=x(6)*u(1)+x(16)*u(2)+x(26)*u(3)+x(36)*u(4)+x(46)*u(5)+x(56)*u(6); w7=x(7)*u(1)+x(17)*u(2)+x(27)*u(3)+x(37)*u(4)+x(47)*u(5)+x(57)*u(6); w8=x(8)*u(1)+x(18)*u(2)+x(28)*u(3)+x(38)*u(4)+x(48)*u(5)+x(58)*u(6); w9=x(9)*u(1)+x(19)*u(2)+x(29)*u(3)+x(39)*u(4)+x(49)*u(5)+x(59)*u(6); w10=x(10)*u(1)+x(20)*u(2)+x(30)*u(3)+x(40)*u(4)+x(50)*u(5)+x(60)*u(6); y1=1./(1+exp(-w1)); y2=1./(1+exp(-w2)); y3=1./(1+exp(-w3)); y4=1./(1+exp(-w4)); y5=1./(1+exp(-w5)); y6=1./(1+exp(-w6)); y7=1./(1+exp(-w7)); y8=1./(1+exp(-w8)); y9=1./(1+exp(-w9)); y10=1./(1+exp(-w10)); v=x(61)*y1+x(62)*y2+x(63)*y3+x(64)*y4+x(65)*y5+x(66)*y6+x(67)*y7+x(68)*y8+x(69)*y9+x(70)*y10; z2=1./(1+exp(-v)); e=u(7)+G*z2-z1; total_error=total_error+e^2/2; delta_x(61)=I*e*y1*z2*(1-z2); x(61)=x(61)+delta_x(61); delta_x(62)=I*e*y2*z2*(1-z2); x(62)=x(62)+delta_x(62); delta_x(63)=I*e*y3*z2*(1-z2); x(63)=x(63)+delta_x(63); delta_x(64)=I*e*y4*z2*(1-z2); x(64)=x(64)+delta_x(64); delta_x(65)=I*e*y5*z2*(1-z2); x(65)=x(65)+delta_x(65); delta_x(66)=I*e*y6*z2*(1-z2); x(66)=x(66)+delta_x(66); delta_x(67)=I*e*y7*z2*(1-z2); x(67)=x(67)+delta_x(67); delta_x(68)=I*e*y8*z2*(1-z2); x(68)=x(68)+delta_x(68); delta_x(69)=I*e*y9*z2*(1-z2); x(69)=x(69)+delta_x(69); delta_x(70)=I*e*y10*z2*(1-z2); x(70)=x(70)+delta_x(70); delta_x(1)=I*e*x(61)*u(1)*z2*(1-z2)*y1*(1-y1); x(1)=x(1)+delta_x(1); delta_x(2)=I*e*x(62)*u(1)*z2*(1-z2)*y2*(1-y2); x(2)=x(2)+delta_x(2); delta_x(3)=I*e*x(63)*u(1)*z2*(1-z2)*y3*(1-y3); x(3)=x(3)+delta_x(3); delta_x(4)=I*e*x(64)*u(1)*z2*(1-z2)*y4*(1-y4); x(4)=x(4)+delta_x(4); delta_x(5)=I*e*x(65)*u(1)*z2*(1-z2)*y5*(1-y5); x(5)=x(5)+delta_x(5); delta_x(6)=I*e*x(66)*u(1)*z2*(1-z2)*y6*(1-y6); x(6)=x(6)+delta_x(6); delta_x(7)=I*e*x(67)*u(1)*z2*(1-z2)*y7*(1-y7); x(7)=x(7)+delta_x(7); delta_x(8)=I*e*x(68)*u(1)*z2*(1-z2)*y8*(1-y8); x(8)=x(8)+delta_x(8); delta_x(9)=I*e*x(69)*u(1)*z2*(1-z2)*y9*(1-y9); x(9)=x(9)+delta_x(9); delta_x(10)=I*e*x(70)*u(1)*z2*(1-z2)*y10*(1-y10); x(10)=x(10)+delta_x(10); delta_x(11)=I*e*x(61)*u(2)*z2*(1-z2)*y1*(1-y1); x(11)=x(11)+delta_x(11); delta_x(12)=I*e*x(62)*u(2)*z2*(1-z2)*y2*(1-y2); x(12)=x(12)+delta_x(12); delta_x(13)=I*e*x(63)*u(2)*z2*(1-z2)*y3*(1-y3); x(13)=x(13)+delta_x(13); delta_x(14)=I*e*x(64)*u(2)*z2*(1-z2)*y4*(1-y4); x(14)=x(14)+delta_x(14); delta_x(15)=I*e*x(65)*u(2)*z2*(1-z2)*y5*(1-y5); x(15)=x(15)+delta_x(15); delta_x(16)=I*e*x(66)*u(2)*z2*(1-z2)*y6*(1-y6); x(16)=x(16)+delta_x(16); delta_x(17)=I*e*x(67)*u(2)*z2*(1-z2)*y7*(1-y7); x(17)=x(17)+delta_x(17); delta_x(18)=I*e*x(68)*u(2)*z2*(1-z2)*y8*(1-y8); x(18)=x(18)+delta_x(18); delta_x(19)=I*e*x(69)*u(2)*z2*(1-z2)*y9*(1-y9); x(19)=x(19)+delta_x(19); delta_x(20)=I*e*x(70)*u(2)*z2*(1-z2)*y10*(1-y10); x(20)=x(20)+delta_x(20); delta_x(21)=I*e*x(61)*u(3)*z2*(1-z2)*y1*(1-y1); x(21)=x(21)+delta_x(21); delta_x(22)=I*e*x(62)*u(3)*z2*(1-z2)*y2*(1-y2); x(22)=x(22)+delta_x(22); delta_x(23)=I*e*x(63)*u(3)*z2*(1-z2)*y3*(1-y3); x(23)=x(23)+delta_x(23); delta_x(24)=I*e*x(64)*u(3)*z2*(1-z2)*y4*(1-y4); x(24)=x(24)+delta_x(24); delta_x(25)=I*e*x(65)*u(3)*z2*(1-z2)*y5*(1-y5); x(25)=x(25)+delta_x(25); delta_x(6)=I*e*x(66)*u(3)*z2*(1-z2)*y6*(1-y6); x(26)=x(26)+delta_x(26); delta_x(27)=I*e*x(67)*u(3)*z2*(1-z2)*y7*(1-y7); x(27)=x(27)+delta_x(27); delta_x(28)=I*e*x(68)*u(3)*z2*(1-z2)*y8*(1-y8); x(28)=x(28)+delta_x(28); delta_x(29)=I*e*x(69)*u(3)*z2*(1-z2)*y9*(1-y9); x(29)=x(29)+delta_x(29); delta_x(30)=I*e*x(70)*u(3)*z2*(1-z2)*y10*(1-y10); x(30)=x(30)+delta_x(30); delta_x(31)=I*e*x(61)*u(4)*z2*(1-z2)*y1*(1-y1); x(31)=x(31)+delta_x(31); delta_x(32)=I*e*x(62)*u(4)*z2*(1-z2)*y2*(1-y2); x(32)=x(32)+delta_x(32); delta_x(33)=I*e*x(63)*u(4)*z2*(1-z2)*y3*(1-y3); x(33)=x(33)+delta_x(33); delta_x(34)=I*e*x(64)*u(4)*z2*(1-z2)*y4*(1-y4); x(34)=x(34)+delta_x(34); delta_x(35)=I*e*x(65)*u(4)*z2*(1-z2)*y5*(1-y5); x(35)=x(35)+delta_x(35); delta_x(36)=I*e*x(66)*u(4)*z2*(1-z2)*y6*(1-y6); x(36)=x(36)+delta_x(36); delta_x(37)=I*e*x(67)*u(4)*z2*(1-z2)*y7*(1-y7); x(37)=x(37)+delta_x(37); delta_x(38)=I*e*x(68)*u(4)*z2*(1-z2)*y8*(1-y8); x(38)=x(38)+delta_x(38); delta_x(39)=I*e*x(69)*u(4)*z2*(1-z2)*y9*(1-y9); x(39)=x(39)+delta_x(39); delta_x(40)=I*e*x(70)*u(4)*z2*(1-z2)*y10*(1-y10); x(40)=x(40)+delta_x(40); delta_x(41)=I*e*x(61)*u(5)*z2*(1-z2)*y1*(1-y1); x(41)=x(41)+delta_x(41); delta_x(2)=I*e*x(62)*u(5)*z2*(1-z2)*y2*(1-y2); x(42)=x(42)+delta_x(42); delta_x(43)=I*e*x(63)*u(5)*z2*(1-z2)*y3*(1-y3); x(43)=x(43)+delta_x(43); delta_x(44)=I*e*x(64)*u(5)*z2*(1-z2)*y4*(1-y4); x(44)=x(44)+delta_x(44); delta_x(45)=I*e*x(65)*u(5)*z2*(1-z2)*y5*(1-y5); x(45)=x(45)+delta_x(45); delta_x(46)=I*e*x(66)*u(5)*z2*(1-z2)*y6*(1-y6); x(46)=x(46)+delta_x(46); delta_x(47)=I*e*x(67)*u(5)*z2*(1-z2)*y7*(1-y7); x(47)=x(47)+delta_x(47); delta_x(48)=I*e*x(68)*u(5)*z2*(1-z2)*y8*(1-y8); x(48)=x(48)+delta_x(48); delta_x(49)=I*e*x(69)*u(5)*z2*(1-z2)*y9*(1-y9); x(49)=x(49)+delta_x(49); delta_x(50)=I*e*x(70)*u(5)*z2*(1-z2)*y10*(1-y10); x(50)=x(50)+delta_x(50); delta_x(51)=I*e*x(61)*u(6)*z2*(1-z2)*y1*(1-y1); x(51)=x(51)+delta_x(51); delta_x(52)=I*e*x(62)*u(6)*z2*(1-z2)*y2*(1-y2); x(52)=x(52)+delta_x(52); delta_x(53)=I*e*x(63)*u(6)*z2*(1-z2)*y3*(1-y3); x(53)=x(53)+delta_x(53); delta_x(54)=I*e*x(64)*u(6)*z2*(1-z2)*y4*(1-y4); x(54)=x(54)+delta_x(54); delta_x(55)=I*e*x(65)*u(6)*z2*(1-z2)*y5*(1-y5); x(55)=x(55)+delta_x(55); delta_x(56)=I*e*x(66)*u(6)*z2*(1-z2)*y6*(1-y6); x(56)=x(56)+delta_x(56); delta_x(57)=I*e*x(67)*u(6)*z2*(1-z2)*y7*(1-y7); x(57)=x(57)+delta_x(57); delta_x(58)=I*e*x(68)*u(6)*z2*(1-z2)*y8*(1-y8); x(58)=x(58)+delta_x(58); delta_x(59)=I*e*x(69)*u(6)*z2*(1-z2)*y9*(1-y9); x(59)=x(59)+delta_x(59); delta_x(60)=I*e*x(70)*u(6)*z2*(1-z2)*y10*(1-y10); x(60)=x(60)+delta_x(60); z1=z2; if total_error<err_goal break; else total_error=0; end end
sys = [x(1);x(2);x(3);x(4);x(5);x(6);x(7);x(8);x(9);x(10);x(11);x(12);x(13);x(14);x(15);x(16);x(17);x(18);x(19);x(20); x(21);x(22);x(23);x(24);x(25);x(26);x(27);x(28);x(29);x(30);x(31);x(32);x(33);x(34);x(35);x(36);x(37);x(38);x(39);x(40); x(41);x(42);x(43);x(44);x(45);x(46);x(47);x(48);x(49);x(50);x(51);x(52);x(53);x(54);x(55);x(56);x(57);x(58);x(59);x(60); x(61);x(62);x(63);x(64);x(65);x(66);x(67);x(68);x(69);x(70) ];
% end mdlUpdate
% %============================================================================= % mdlOutputs % Return the block outputs. %============================================================================= % function sys=mdlOutputs(x,u) w1=x(1)*u(1)+x(11)*u(2)+x(21)*u(3)+x(31)*u(4)+x(41)*u(5)+x(51)*u(6); w2=x(2)*u(1)+x(12)*u(2)+x(22)*u(3)+x(32)*u(4)+x(42)*u(5)+x(52)*u(6); w3=x(3)*u(1)+x(13)*u(2)+x(23)*u(3)+x(33)*u(4)+x(43)*u(5)+x(53)*u(6); w4=x(4)*u(1)+x(14)*u(2)+x(24)*u(3)+x(34)*u(4)+x(44)*u(5)+x(54)*u(6); w5=x(5)*u(1)+x(15)*u(2)+x(25)*u(3)+x(35)*u(4)+x(45)*u(5)+x(55)*u(6); w6=x(6)*u(1)+x(16)*u(2)+x(26)*u(3)+x(36)*u(4)+x(46)*u(5)+x(56)*u(6); w7=x(7)*u(1)+x(17)*u(2)+x(27)*u(3)+x(37)*u(4)+x(47)*u(5)+x(57)*u(6); w8=x(8)*u(1)+x(18)*u(2)+x(28)*u(3)+x(38)*u(4)+x(48)*u(5)+x(58)*u(6); w9=x(9)*u(1)+x(19)*u(2)+x(29)*u(3)+x(39)*u(4)+x(49)*u(5)+x(59)*u(6); w10=x(10)*u(1)+x(20)*u(2)+x(30)*u(3)+x(40)*u(4)+x(50)*u(5)+x(60)*u(6); y1=1./(1+exp(-w1)); y2=1./(1+exp(-w2)); y3=1./(1+exp(-w3)); y4=1./(1+exp(-w4)); y5=1./(1+exp(-w5)); y6=1./(1+exp(-w6)); y7=1./(1+exp(-w7)); y8=1./(1+exp(-w8)); y9=1./(1+exp(-w9)); y10=1./(1+exp(-w10)); v=x(61)*y1+x(62)*y2+x(63)*y3+x(64)*y4+x(65)*y5+x(66)*y6+x(67)*y7+x(68)*y8+x(69)*y9+x(70)*y10; sys =1./(1+exp(-v));
% end mdlOutputs
% %============================================================================= % mdlGetTimeOfNextVarHit % Return the time of the next hit for this block. Note that the result is % absolute time. Note that this function is only used when you specify a % variable discrete-time sample time [-2 0] in the sample time array in % mdlInitializeSizes. %============================================================================= % function sys=mdlGetTimeOfNextVarHit(t,x,u)
sampleTime = 1; % Example, set the next hit to be one second later. sys = t + sampleTime;
% end mdlGetTimeOfNextVarHit
% %============================================================================= % mdlTerminate % Perform any end of simulation tasks. %============================================================================= % function sys=mdlTerminate(t,x,u)
sys = [];
% end mdlTerminate

Best Answer

Only 3 arguments are passed to the mdlUpdate stage, so you need to define your function prototype as:
sys=mdlUpdate(t,x,u);
Please also look at the example models if you are getting started with S-functions. Run "sfundemos" and navigate to 'MATLAB file S-functions>Level-1 MATLAB file S-functions'. Note that Level-1 MATLAB S-functions have been deprecated for several releases now, so if you're writing a new S-function, we highly recommend writing it as a Level-2 MATLAB S-function.