MATLAB中文论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 90|回复: 2|关注: 0

[未答复] Level2 Sfunction与LV1 s-function结果不一致

[复制链接]

新手

5 麦片

财富积分


050


1

主题

13

帖子

0

最佳答案
  • 关注者: 1
发表于 2017-11-14 22:20:03 | 显示全部楼层 |阅读模式
本帖最后由 Scofield_GU 于 2017-11-17 10:30 编辑

使用状态空间描述系统+状态反馈,用了两个sfunction描述同一个系统,如图一。但是lv1的sfun可以解出结果,而lv2的结果直接跑飞了,这里没有报错,但是结果和lv1的不一致,以下为代码:

LV1:

  1. function [sys,x0,str,ts,simStateCompliance] = model_original(t,x,u,flag,A,B2,B1,C1,D12,F)


  2. switch flag,
  3.   % Initialization %
  4.   case 0,
  5.     [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;
  6.   % Derivatives %
  7.   case 1,
  8.     sys=mdlDerivatives(t,x,u,A,B2,B1);
  9.   % Update %
  10.   case 2,
  11.     sys=mdlUpdate(t,x,u);
  12.   % Outputs %
  13.   case 3,
  14.     sys=mdlOutputs(t,x,u,C1 ,D12,F);
  15.   % GetTimeOfNextVarHit %
  16.   case 4,
  17.     sys=mdlGetTimeOfNextVarHit(t,x,u,A ,B2,B1,C1 ,D12);
  18.   % Terminate %
  19.   case 9,
  20.     sys=mdlTerminate(t,x,u);
  21.   % Unexpected flags %
  22.   otherwise
  23.     DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));

  24. end



  25. %
  26. %=============================================================================
  27. % mdlInitializeSizes
  28. % Return the sizes, initial conditions, and sample times for the S-function.
  29. %=============================================================================
  30. %
  31. function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes

  32. %
  33. % call simsizes for a sizes structure, fill it in and convert it to a
  34. % sizes array.
  35. %
  36. % Note that in this example, the values are hard coded.  This is not a
  37. % recommended practice as the characteristics of the block are typically
  38. % defined by the S-function parameters.
  39. %
  40. sizes = simsizes;

  41. sizes.NumContStates  = 12;
  42. sizes.NumDiscStates  = 0;
  43. sizes.NumOutputs     = 6;%z1 z2 u1 u2 s1-u1 s2-u2
  44. sizes.NumInputs      = 4;%u1 u2 w1 w2
  45. sizes.DirFeedthrough = 1;
  46. sizes.NumSampleTimes = 1;   % at least one sample time is needed

  47. sys = simsizes(sizes);

  48. %
  49. % initialize the initial conditions
  50. %
  51. x0  = zeros(12,1);

  52. %
  53. % str is always an empty matrix
  54. %
  55. str = [];

  56. %
  57. % initialize the array of sample times
  58. %
  59. ts  = [0 0];

  60. % Specify the block simStateCompliance. The allowed values are:
  61. %    'UnknownSimState', < The default setting; warn and assume DefaultSimState
  62. %    'DefaultSimState', < Same sim state as a built-in block
  63. %    'HasNoSimState',   < No sim state
  64. %    'DisallowSimState' < Error out when saving or restoring the model sim state
  65. simStateCompliance = 'UnknownSimState';

  66. % end mdlInitializeSizes

  67. %
  68. %=============================================================================
  69. % mdlDerivatives
  70. % Return the derivatives for the continuous states.
  71. %=============================================================================
  72. %
  73. function sys=mdlDerivatives(~,x,u,A,B2,B1)
  74. ut=[u(1);u(2)];
  75. w=[u(3);u(4)];
  76. sys=A*x+B2*ut+B1*w;

  77. % end mdlDerivatives

  78. %
  79. %=============================================================================
  80. % mdlUpdate
  81. % Handle discrete state updates, sample time hits, and major time step
  82. % requirements.
  83. %=============================================================================
  84. %
  85. function sys=mdlUpdate(t,x,u)
  86. sys=[];


  87. % end mdlUpdate

  88. %
  89. %=============================================================================
  90. % mdlOutputs
  91. % Return the block outputs.
  92. %=============================================================================
  93. %
  94. function sys=mdlOutputs(~,x,u,C1 ,D12,F)
  95. ut=[u(1);u(2)];
  96. a=C1(1,:) *x+D12(1,:)*ut;
  97. b=(C1(2,:) *x+D12(2,:)*ut)/1.39;
  98. c=F(1,:) *x;%U1;
  99. d=F(2,:) *x;%U2
  100. e=x(1);%悬架动行程1
  101. f=x(2);%悬架动行程2
  102. % g=x(3);
  103. % h=x(4);
  104. % i=x(5);
  105. % j=x(6);
  106. % k=x(7);
  107. % l=x(8);
  108. % m=x(9);
  109. % n=x(10);
  110. % o=x(11);
  111. % p=x(12);
  112. sys=[a;b;c;d;e;f;];



  113. function sys=mdlTerminate(t,x,u)
  114. sys = [];
复制代码




360截图16410121709896.png

新手

5 麦片

财富积分


050


1

主题

13

帖子

0

最佳答案
  • 关注者: 1
 楼主| 发表于 2017-11-14 22:21:40 | 显示全部楼层
本帖最后由 Scofield_GU 于 2017-11-17 10:31 编辑
  1. function model_lv2(block)
  2. % Level-2 MATLAB file S-Function for inherited sample time demo.
  3. %   Copyright 1990-2009 The MathWorks, Inc.

  4.   setup(block);
  5.   
  6. %endfunction

  7. function setup(block)
  8.   
  9.   %% Register number of input and output ports
  10.   block.NumInputPorts  = 2;
  11.   block.NumOutputPorts = 4;

  12.   %% Setup functional port properties to dynamically
  13.   %% inherited.
  14.   block.SetPreCompInpPortInfoToDynamic;
  15.   block.SetPreCompOutPortInfoToDynamic;

  16.   % Override the input port properties.输入
  17.   %control force
  18.   block.InputPort(1).DatatypeID  = 0;  % double
  19.   block.InputPort(1).Complexity  = 'Real';
  20.   block.InputPort(1).Dimensions  = [2 1];
  21.   block.InputPort(1).DirectFeedthrough  = 0;
  22.   block.InputPort(1).SamplingMode = 'Sample';
  23.    %disturbance
  24.   block.InputPort(2).DatatypeID  = 0;  % double
  25.   block.InputPort(2).Complexity  = 'Real';
  26.   block.InputPort(2).Dimensions  = [2 1];
  27.   block.InputPort(2).DirectFeedthrough  = false;
  28.   block.InputPort(2).SamplingMode = 'Sample';
  29.   
  30.   % Override the output port properties.输出
  31.   block.OutputPort(1).DatatypeID  = 0; % double
  32.   block.OutputPort(1).Complexity  = 'Real';
  33.   block.OutputPort(1).Dimensions  = 1;
  34.   block.OutputPort(1).SamplingMode = 'Sample';
  35.   
  36.   block.OutputPort(2).Dimensions  = 1;
  37.   block.OutputPort(2).SamplingMode = 'Sample';
  38.   
  39.   block.OutputPort(3).Dimensions  = [2 1];
  40.   block.OutputPort(3).SamplingMode = 'Sample';
  41.   
  42.   block.OutputPort(4).Dimensions  = [12 1];
  43.   block.OutputPort(4).SamplingMode = 'Sample';
  44.   % Register the parameters.A,B2,B1,C1,D12,F
  45.   block.NumDialogPrms     = 6;
  46.   
  47.   
  48.   % Set up the continuous states.
  49.   block.NumContStates = 0;

  50.   % Register the sample times.
  51.   %  [0 offset]            : Continuous sample time
  52.   %  [positive_num offset] : Discrete sample time
  53.   %
  54.   %  [-1, 0]               : Inherited sample time
  55.   %  [-2, 0]               : Variable sample time
  56.   block.SampleTimes = [0.001 0];
  57.   
  58.   
  59.   %% Set the block simStateCompliance to default (i.e., same as a built-in block)
  60.   block.SimStateCompliance = 'DefaultSimState';

  61.   %% Register methods
  62.   block.RegBlockMethod('PostPropagationSetup',    @DoPostPropSetup);
  63.   block.RegBlockMethod('Start',    @Start);  
  64.   block.RegBlockMethod('Outputs',                 @Output);  
  65.   block.RegBlockMethod('Update',                  @Update);  
  66.   
  67. %endfunction

  68. function DoPostPropSetup(block)

  69.   %% Setup Dwork
  70. block.NumDworks = 2;
  71.   
  72.   block.Dwork(1).Name            = 'state';
  73.   block.Dwork(1).Dimensions      = 12;
  74.   block.Dwork(1).DatatypeID      = 0;      % double
  75.   block.Dwork(1).Complexity      = 'Real'; % real
  76.   block.Dwork(1).UsedAsDiscState = true;
  77.   
  78.   %--------------------------------------------------------------------------
  79.   block.Dwork(2).Name            = 'Input2State';
  80.   block.Dwork(2).Dimensions      = 2;
  81.   block.Dwork(2).DatatypeID      = 0;      % double
  82.   block.Dwork(2).Complexity      = 'Real'; % real
  83.   block.Dwork(2).UsedAsDiscState = 1;
  84. %endfunction

  85. function Start(block)

  86.   %% Initialize Dwork
  87.   block.Dwork(1).Data = zeros(12,1);
  88.   block.Dwork(2).Data = zeros(block.Dwork(2).Dimensions,1);
  89. %endfunction

  90. function Output(block)
  91. C1=block.DialogPrm(4).Data;%传入参数
  92. D12=block.DialogPrm(5).Data;
  93. F=block.DialogPrm(6).Data;
  94. block.OutputPort(1).Data = C1(1,:) *block.Dwork(1).Data+D12(1,:)*block.Dwork(2).Data;
  95.   block.OutputPort(2).Data =(C1(2,:) *block.Dwork(1).Data+D12(2,:)*block.Dwork(2).Data)/1.39;
  96.   block.OutputPort(3).Data = F*block.Dwork(1).Data;

  97.   block.OutputPort(4).Data = block.Dwork(1).Data;
  98.   
  99. %endfunction

  100. function Update(block)
  101. A=block.DialogPrm(1).Data;
  102. B2=block.DialogPrm(2).Data;
  103. B1=block.DialogPrm(3).Data;
  104. block.Dwork(2).Data = block.InputPort(1).Data;
  105. block.Dwork(1).Data = A*block.Dwork(1).Data+B2*block.Dwork(2).Data+B1*block.InputPort(2).Data;

  106. %endfunction

  107.   
  108.   
  109.   
  110.   
  111.   
复制代码

新手

5 麦片

财富积分


050


1

主题

13

帖子

0

最佳答案
  • 关注者: 1
 楼主| 发表于 2017-11-14 22:24:46 | 显示全部楼层
本帖最后由 Scofield_GU 于 2017-11-17 10:32 编辑

请各位老师帮我看一下可以吗,我确实是研究了 很久没有发现问题。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐上一条 /2 下一条

快速回复 返回顶部 返回列表