Something wrong with a S-funciton code
Show older comments
I writed a S-funciton program as follows:
function [sys,x0,str,ts,simStateCompliance] = f(t,x,u,flag,B)
switch flag,
case 0, [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;
case 2, sys=mdlUpdate(t,x,u,B);
case 3, sys=mdlOutputs(t,x,u);
otherwise DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 20;
sizes.NumOutputs = 20;
sizes.NumInputs = 3;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [0.3 -2 0.3 -1 0.3 0 0.3 1 0.3 2 0.08 0 0.08 0.25 0.08 0.5 0.08 0.75 0.08 1];
str = [];
ts = [-1 0];
function sys=mdlUpdate(t,x,u,B)
ux(1)=(2*u(1) - 2*x(1))/(2*x(2))^2;
ux(2)=(2*u(2) - 2*x(2))/(2*x(3))^2;
ux(3)=(2*u(3) - 2*x(3))/(2*x(4))^2;
ux(4)=(2*u(4) - 2*x(4))/(2*x(5))^2;
ux(5)=(2*u(5) - 2*x(5))/(2*x(6))^2;
ux(6)=(2*u(6) - 2*x(6))/(2*x(7))^2;
ux(7)=(2*u(7) - 2*x(7))/(2*x(8))^2;
ux(8)=(2*u(8) - 2*x(8))/(2*x(9))^2;
ux(9)=(2*u(9) - 2*x(9))/(2*x(10))^2;
ux(10)=(2*u(10) - 2*x(10))/(2*x(11))^2;
ux(11)=(2*u(11) - 2*x(11))/(2*x(12))^2;
ux(12)=(2*u(12) - 2*x(2))/(2*x(13))^2;
ux(13)=(2*u(13) - 2*x(13))/(2*x(14))^2;
ux(14)=(2*u(14) - 2*x(4))/(2*x(15))^2;
ux(15)=(2*u(15) - 2*x(15))/(2*x(16))^2;
ux(16)=(2*u(16) - 2*x(16))/(2*x(17))^2;
ux(17)=(2*u(17) - 2*x(17))/(2*x(18))^2;
ux(18)=(2*u(18) - 2*x(18))/(2*x(19))^2;
ux(19)=(2*u(19) - 2*x(19))/(2*x(20))^2;
ux(20)=(2*u(20) - 2*x(20))/(2*x(1))^2;
x(1)=x(1)+B*u(3)*ux(1);
x(2)=x(2)+B*u(3)*ux(2);
x(3)=x(3)+B*u(3)*ux(3);
x(4)=x(4)+B*u(3)*ux(4);
x(5)=x(5)+B*u(3)*ux(5);
x(6)=x(6)+B*u(3)*ux(6);
x(7)=x(7)+B*u(3)*ux(7);
x(8)=x(8)+B*u(3)*ux(8);
x(9)=x(9)+B*u(3)*ux(9);
x(10)=x(10)+B*u(3)*ux(10);
x(11)=x(11)+B*u(3)*ux(11);
x(12)=x(12)+B*u(3)*ux(12);
x(13)=x(13)+B*u(3)*ux(13);
x(14)=x(14)+B*u(3)*ux(14);
x(15)=x(15)+B*u(3)*ux(15);
x(16)=x(16)+B*u(3)*ux(16);
x(17)=x(17)+B*u(3)*ux(17);
x(18)=x(18)+B*u(3)*ux(18);
x(19)=x(19)+B*u(3)*ux(19);
x(20)=x(20)+B*u(3)*ux(20);
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)];
function sys=mdlOutputs(t,x,u)
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)];
end
But when I run this code in Matlab/Simulink, there was an error: Output returned by S-function 'f' in 'test/F' during flag=3 call must be a real vector of length 20.
I want to know that the length of the output is 20, why is it not right?
3 Comments
Kaustubha Govind
on 5 Aug 2013
Are the values all real (and not complex) as well? You may to set a breakpoint in the mdlOutputs and see what value is returned in 'sys' at the time that the error occurs.
autshy
on 6 Aug 2013
febina sam
on 15 Oct 2019
can u explain further more clear ,please
Answers (0)
Categories
Find more on Code Performance in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!