How can i fix that? And are there any error in my code? thank. "X0 returned by MATLAB S-function 'mppt1' in 'solar/S-Function' must be a vector of length 4"

5 views (last 30 days)
function [sys,x0,str,ts,simStateCompliance] = mppt1(t,x,u,flag)
%SFUNDSC2 Example unit delay MATLAB File S-function
% The MATLAB file S-function is an example of how to implement a unit
% delay.
%
% See sfuntmpl.m for a general S-function template.
%
% See also SFUNTMPL.
% Copyright 1990-2009 The MathWorks, Inc.
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %V
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts,simStateCompliance] = mdlInitializeSizes;
%%%%%%%%%%
% Update %
%%%%%%%%%%
case 2,
sys = mdlUpdate(t,x,u);
%%%%%%%%%%
% Output %
%%%%%%%%%%
case 3,
sys = mdlOutputs(t,x,u);
%%%%%%%%%%%%%
% Terminate %
%%%%%%%%%%%%%
case 9,
sys = [];
otherwise
DAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag));
end
%end sfundsc2
%
%=============================================================================
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the S-function.
%=============================================================================
%
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = 4;
sizes.NumOutputs = 1;
sizes.NumInputs = 1;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
%initial conditions
x0=[0;0;0;0];
str = [];
ts = [0.01 0]; % Sample period of 0.1 seconds (10Hz)
% speicfy that the simState for this s-function is same as the default
simStateCompliance = 'DefaultSimState';
% end mdlInitializeSizes
%
%=======================================================================
% mdlUpdate
% Handle discrete state updates, sample time hits, and major time step
% requirements.
%=======================================================================
%
function sys = mdlUpdate(t,x,u)
x(1)=u;
if x(1)>x(2)
x(2)=x(1);
x(3)=x(4);
x(4)=t(3)+0.01;
else
x(3)=x(4);
x(4)=x(3)-0.01;
end
sys = x;
%end mdlUpdate
%
%=======================================================================
% mdlOutputs
% Return the output vector for the S-function
%=======================================================================
%
function sys=mdlOutputs(t,x,u)
sys = x(4);
%end mdlOutputs

Answers (1)

Shubham
Shubham on 12 Oct 2024
The error message indicates that the initial condition vector x0 must have a length of 4. Your code seems to be correct in that regard, as you have defined "x0" as a 4-element vector:
x0 = [0; 0; 0; 0];
However, the issue may arise from the "mdlUpdate" function. Ensure that you're updating the state vector "x" properly. As far as I understand, accessing "t(3)" doesn't make sense because "t" is a scalar representing the current time.
Try modifying the update function as:
function sys = mdlUpdate(t,x,u)
x(1) = u; % Assuming u is a scalar input
if x(1) > x(2)
x(2) = x(1);
x(3) = x(4);
x(4) = t + 0.01; % Changed from t(3) to t
else
x(3) = x(4);
x(4) = x(3) - 0.01;
end
sys = x;
If you are still facing issues, then try sharing the model for complete reproduction of the issue.
I hope this helps!

Categories

Find more on Dynamic System Models in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!