State derivatives returned by S-function 'BUCKchap3_6plant' in 'BUCKchap3_6/S-Function' during flag=1 call must be a real vector of length 2

17 ビュー (過去 30 日間)
error:State derivatives returned by S-function 'BUCKchap3_6plant' in 'BUCKchap3_6/S-Function' during flag=1 call must be a real vector of length 2。
function [sys,x0,str,ts]=BUCKchap3_6plant(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 = 2; sizes.NumInputs = 1; sizes.DirFeedthrough = 1; sizes.NumSampleTimes = 0; sys=simsizes(sizes); x0=[0 0]; str=[]; ts=[];
function sys=mdlDerivatives(t,x,u) L=1e-3; R=20; C=1000e-6; Vin=50;
sys(1)=(-x(2)+Vin*u(1))/L; sys(2)=x(1)/C-x(2)/C/R;
function sys=mdlOutputs(t,x,u) sys(1)=x(1); sys(2)=x(2);
  1 件のコメント
Walter Roberson
Walter Roberson 2018 年 11 月 5 日
Just before
sys(1)=(-x(2)+Vin*u(1))/L; sys(2)=x(1)/C-x(2)/C/R;
Add
assert(all(imag(u)==0), 'u is imaginary or nan')

サインインしてコメントする。

回答 (2 件)

FUN S
FUN S 2018 年 11 月 5 日
Thank you very much

FUN S
FUN S 2018 年 11 月 5 日
編集済み: Walter Roberson 2020 年 6 月 16 日
By adding assert(all(imag(u)==0), 'u is imaginary or nan'); before sys(1)=(-x(2)+Vin*u(1))/L; sys(2)=x(1)/C-x(2)/C/R;, but the problem remains the same. The following I gives a control model (BUCKchap3_6ctrl) and control object(BUCKchap3_6plant) code.
function [sys,x0,str,ts]=BUCKchap3_6plant(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 = 2;
sizes.NumInputs = 1;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 0;
sys=simsizes(sizes);
x0=[0 0];
str=[];
ts=[];
function sys=mdlDerivatives(t,x,u)
L=1e-3;
R=20;
C=1000e-6;
Vin=50;
% ut = u(1);
assert(all(imag(u)==0), 'u is imaginary or nan');
sys(1) = -x(2)/L + Vin / L * u(1);
sys(2) = x(1)/C - x(2) /C / R;
% sys(1)=(-x(2)+Vin*u(1))/L; % sys(2)=x(1)/C-x(2)/C/R;
function sys=mdlOutputs(t,x,u)
sys(1)=x(1);
sys(2)=x(2);
function [sys,x0,str,ts]=BUCKchap3_6ctrl(t,x,u,flag)
switch flag,
case 0,
[sys,x0,str,ts]=mdlInitializeSizes;
case 3,
sys=mdlOutputs(t,x,u);
case {1, 2, 4, 9 }
sys = [];
otherwise error(['Unhandled flag = ',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes
sizes = simsizes;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 1;
sizes.NumInputs = 5;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 0;
sys=simsizes(sizes);
x0=[];
str=[];
ts=[];
function sys=mdlOutputs(t,x,u)
L=1e-3;
R=20;
C=1000e-6;
Vin=50;
wn=210;
k21=wn;
k22=wn;
Vref=u(1);
e=u(2);
de=u(3);
x1=u(4);
x2=u(5);
Q=-k22*e-k21*de;
f=L*x1/C/R;
ut=(f+(1-L/C/R/R)*x2+C*L*Q)/Vin;
sys(1)=ut;
  2 件のコメント
Aliff Ashraff
Aliff Ashraff 2020 年 6 月 16 日
hello can u solve the problem?
Walter Roberson
Walter Roberson 2020 年 6 月 16 日
"Fun S": I reformatted your code to make it readable. Please check it to ensure that I correctly interpreted the comment positions.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAudio I/O and Waveform Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by