Output returned by S-function input in sim/S-Function2' during flag=3 call must be a real vector of length 4

16 Ansichten (letzte 30 Tage)
Hello I am working in matlab using S functions and i have encountered the above mentioned problem.
I get the following two messages.
1) Source 'sim/S-Function2' specifies that its sample time (-1) is back-inherited. You should explicitly specify the sample time of sources. You can disable this diagnostic by setting the 'Source block specifies -1 sample time' diagnostic to 'none' in the Sample Time group on the Diagnostics pane of the Configuration Parameters dialog box.
2) Output returned by S-function 'input' in 'sim/S-Function2' during flag=3 call must be a real vector of length 4
thanks.
  3 Kommentare
Ameer Hamza
Ameer Hamza am 17 Dez. 2018
Bearbeitet: Walter Roberson am 10 Feb. 2022
function [sys,x0,str,ts] = spacemodel(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 = 4;
sizes.NumInputs = 0;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 0;
sys = simsizes(sizes);
x0 = [0.5,0];
str = [];
ts = [];
c=[];
function sys=mdlDerivatives(t,x,u)
t0=0:pi/2:2*pi;
t_i=0:pi/2:2*pi;
r1=cos(t0)+j*sin(t0);
r2=j*cos(t0)-sin(t0);
sys(1)=x(1);
sys(2)=x(2);
sys(2)=1/(-sin(t)+j*cos(t));
function sys=mdlOutputs(t,x,u)
t0=0:pi/2:2*pi;
r1=cos(t0)+j*sin(t0);
r2=j*cos(t0)-sin(t0);
p0=x(1);
dp0=x(2);
sys(1)=p0;
sys(2)=dp0;
The above code is linked with the error

Melden Sie sich an, um zu kommentieren.

Antworten (4)

SALAH alatai
SALAH alatai am 11 Feb. 2022
@Walter Roberson i don't get it , please more description
  7 Kommentare
SALAH alatai
SALAH alatai am 22 Feb. 2022
Bearbeitet: SALAH alatai am 22 Feb. 2022
@Walter Roberson thanks for your respones
1- then we can deduce that at any given time, gate2 = 1 - gate1 :- gate two seems a phase sfited of gate 1 to get 3 level from full bridge inverter.
2- Now, the simulation image appears to imply that the yellow pulse has a duty cycle of 1/2 -- that it is on for 1/2 of the time and off for 1/2 of the time. This does not, however, match the code: the code appears to have the pulse be active for 1/10 of the cycle time. it is 1/2 but I chnged to 0.1 and forget to correct it.
3- Okay, now you want to add two more gates. But what are the rules for the gates? When should they go high, and when should they go low? Are they a pulse at the same frequency as the existing two gates? Are they also the same duty cycle? So is the new set of gates effectively just a delayed version of the existing gates? Actually I want to add different pulses and need to change the frequency but not nesseary right now for changing frequency, for the signal that i need has different ducty cycle so for the 4 gates should have different duct cycle not 1/2. the signal in this code will give me 3 levels and I am looking for more levels. i tried to delayed it but not get what I want . for duty cycle will once 10 and once 30 not 50
Walter Roberson
Walter Roberson am 25 Feb. 2022
If you want a pulse at a different rate, or even a pulse at the same rate that is not at a fixed offset from the pulse you already have code for, then you will need to edit the model update function. You might need additional state variables.
However, for situations like you show in the plot, the state of the second output is always exactly one minus the first output, so you would not need to use two distinct state outputs: you could just have your model outputs function output [x(1); 1-x(1)] . And if the second pulse has the same structure, two parts that are inverses of each other, then you could continue to run the system with only two state variables, outputing [x(1); 1-x(1); x(2); 1-x(2)]. Of course either way you would need to edit the model updates function to adjust for the new way of using the state variables.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 10 Feb. 2022
sys=mdlOutputs(t,x,u);
That call must return the outputs of the block.
Output returned by S-function 'input' in 'sim/S-Function2' during flag=3 call must be a real vector of length 4
That tells us that your block is either configured explicitly to expect a vector of four output values, or else that the output feeds into something that MATLAB can prove expects a vector of four input values which implied to MATLAB that the output needs to be a vector of length 4 for your purposes.
function sys=mdlOutputs(t,x,u)
...
sys(1)=p0;
sys(2)=dp0;
But that only creates an output of length 2 for the function.
If you want the output to be of length 4 with the final two values being 0, then you should
sys = zeros(1,4);
sys(1)=p0;
sys(2)=dp0;
Question: why does your mdlOutputs() function bother to build t0 and calculate r1 and r2 ? They are not used in caclulation of the vector sys which is the only output of the function.
  9 Kommentare
Walter Roberson
Walter Roberson am 11 Feb. 2022
need it to 4 output 4 gate not 2.
What is the meaning associated with the first output? Which input or state is associated with this output?
What is the meaning associated with the second output? Which input or state is associated with this output?
What is the meaning associated with the third output? Which input or state is associated with this output?
What is the meaning associated with the fourth output? Which input or state is associated with this output?

Melden Sie sich an, um zu kommentieren.


琦 陈
琦 陈 am 6 Mai 2022
Bearbeitet: 琦 陈 am 6 Mai 2022
Sorry, I'd like to ask you a question. I want to realize a BP neural network to control three mutually coupled manipulators. When running, the error is reported as follows. With my program, how should I modify it,please
%基于BP网络的PID控制器S函数
function[sys,x0,str,ts]=nnbp_pid(t,x,u,flag,T,nh,xite,alfa,kFunc1,kFunc2)
%--------------------------------------------------------
T=0.01;nh=5;xite=0.25;alfa=0.05;kFunc1=1;kFunc2=2;%,可能是阈值
%--------------------------------------------------------
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes(T,nh);
case 3,
sys=mdlOutputs(t,x,u,T,nh,xite,alfa,kFunc1,kFunc2);
case {1,2,4,9},
sys=[];
otherwise,
error(['Unhandled flag=',num2str(flag)]);
end
%初始化函数
function [sys,x0,str,ts] = mdlInitializeSizes(T,nh)
sizes=simsizes;
sizes.NumContStates=0;
sizes.NumDiscStates=0;
%-----------------------
sizes.NumOutputs=6+nh*7;
sizes.NumInputs=15+14*nh;
%-----------------------
sizes.DirFeedthrough=1;
sizes.NumSampleTimes=1;
sys=simsizes(sizes);
x0=[];
str=[];
ts=[T 0];
%系统输出计算函数
function sys=mdlOutputs(t,x,u,T,nh,xite,alfa,kFunc1,kFunc2)
%--------------------------------------------------------------
%这是w(k-2)
wi_2=reshape(u(8:7+4*nh),nh,4);%把u(8)-u(27)(共20个,是输入层到隐含层的权值)
%变成一个矩阵,nh=5行4列,第一列是w11(u8),w12(u9),
%w13(u10),w14,w15,
wo_2=reshape(u(8+4*nh:7+7*nh),3,nh);%把u(28)-u(42)
%(共15个,是隐含层层到输出层的权值)
%--------------------------------------------------------------
%--------------------------------------------------------------
%这是w(k-1)
wi_1=reshape(u(8+7*nh:7+11*nh),nh,4);%同理
wo_1=reshape(u(8+11*nh:7+14*nh),3,nh);%同理
%--------------------------------------------------------------
xi=[u([6,4,1])',1];%????????????由列向量变为行向量再加一,u是列向量,最后xi=[r(k) y(k) e(k) 1]
%<<<v1.2>>>,把xi内的元素换了
xx=[u(1)-u(2);u(1);u(1)+u(3)-2*u(2)];%求Δu(k)用的,书p154下面,列向量
I=xi*wi_1';%,不是乘反了,而是忘了写转置了,这是求出了隐含层的输入,
% 隐含层的输入是一个5个元素的行向量。
% 但是感觉xi的内容有问题,应该是ek,ek-1,ek-2,ek-3(书p57)
Oh=non_transfun(I,kFunc1);
% Oh=tanh(I); %把上面的程序语句换成这个,用的是tanh函数
%求隐含层的输出,
K=non_transfun(wo_1*Oh',kFunc2);
%K=0.5*(1+tanh(wo_1*Oh') );%???????????.K是三个pid的值,是一个三个元素的列向量
uu=u(7)+K'*xx;%u(7)是u(k-1),书上写的是u(k)=u(k-1)+Δu(k),书p154下面,uu是u(k)
% ▼▼▼▼▼▼▼▼这段程序应该是写关于更新及学习wk的▼▼▼▼▼▼▼▼▼▼▼▼
dyu=sign((u(4)-u(5))/(uu-u(7)+0.0000001));
dk=non_transfun(K,3);
delta3=u(1)*dyu*xx.*dk;
wo=wo_1+xite*delta3*Oh+alfa*(wo_1-wo_2)+alfa*(wo_1-wo_2);
dO=2*non_transfun(I,3);
wi=wi_1+xite*(dO.*(delta3'*wo))'*xi+alfa*(wi_1-wi_2);
% ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲
sys=[uu;K;wi(:);wo(:)];
%激活函数
function W1=non_transfun(W,key_1)
switch key_1
case 1,W1=(exp(W)-exp(-W))./(exp(W)+exp(-W));
case 2,W1=exp(W)./(exp(W)+exp(-W));
case 3,W1=2./(exp(W)+exp(-W)).^2;
end
  1 Kommentar
Walter Roberson
Walter Roberson am 6 Mai 2022
I suggest that you debug by putting in a call to
whos
after the
sys=[uu;K;wi(:);wo(:)];
so that you can explore what sizes the variables actually are.

Melden Sie sich an, um zu kommentieren.


manfredo miao
manfredo miao am 22 Okt. 2022
问题解决了吗?我也遇到类似的问题!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by