[Too many input arguments] Hello, I have problem with my s-function,I guess a classical error. When I run the following code

2 views (last 30 days)
function [sys,x0,str,ts]=rbf_sensorless_test(t,x,u,flag)
switch flag
case 0
[sys,x0,str,ts]=mdlInitializeSizes;
case 1
[sys,x0,str,ts]=mdlDerivatives(t,x,u);
case 3
[sys,x0,str,ts]=mdlOutputs(t,x,u);
case {2, 4, 9 }
sys = [];
otherwise
error(['Unhandled flag = ',num2str(flag)]);
end
function [sys,x0,str,ts]=mdlInitializeSizes
global Y L c b
sizes = simsizes;
sizes.NumContStates = 6;
sizes.NumDiscStates = 0;
sizes.NumOutputs = 1;
sizes.NumInputs = 2;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 0;
sys=simsizes(sizes);
x0 = [0*ones(6,1)];
str=[];
ts=[];
c=[-0.5 -0.25 0 0.25 0.5;
-0.5 -0.25 0 0.25 0.5];
b=[0.2 0.2 0.2 0.2 0.2]';
L=8.5e-3;
Y=[2 0 0 0 0;0 2 0 0 0;0 0 2 0 0;0 0 0 2 0;0 0 0 0 2];
function sys=mdlDerivatives(u)
global Y L c b
x1=u(1);
x2=u(2);
x=[x1 x2]';
h=zeros(5,1);
for j=1:1:5
h(j)=exp(-norm(x-c(:,j))^2/(2*b(j)*b(j)));
end
dw=(1/L)*Y*x2*h;
de=(1/L)*x2;
for i=1:1:5
sys(i)=dw(i);
end
sys(6)=de;
function sys=mdlOutputs(~)
w=[x(1) x(2) x(3) x(4) x(5)]';
e=w'*h+x(6);
sys=e;

Accepted Answer

Walter Roberson
Walter Roberson on 6 Feb 2020
function sys=mdlOutputs(~)
w=[x(1) x(2) x(3) x(4) x(5)]';
e=w'*h+x(6);
sys=e;
You call this function with three arguments but your code says that it expects one argument only and that it should ignore the one argument.
Then your function proceeds to try to use variables x and h that are not defined in that context. You could have gotten x from a parameter if you had not ignored the parameters. But h is defined in another function only, and that other function might not have been called or not called for the current state.
You can use an extra function that calculates h and call that function from both places.
  9 Comments
Walter Roberson
Walter Roberson on 8 Feb 2020
w=[x(1) x(2) x(3) x(4) x(5)]';
Where is that x vector coming from? You are inside
function sys=mdlOutputs(~,~,u)
which has no x parameter. You do have
global c b
but there is no x in that.
Why do you have so many state variables? If you were doing an ode then you would have at most states for w, , ϵ and .
Your mdlDerivatives exists to calculate derivatives of your model at each time step. If you do not use the derivative anywhere (and MATLAB does not want to calculate them to resolve a transfer function or perhaps algebraic loop) then you do not need to supply that function. MATLAB can also estimate derivatives; mdlDerivatives permits more exact derivatives... when you want derivatives at all.
mdlOutputs is the output of your function, which is not the same as the derivatives.

Sign in to comment.

More Answers (0)

Categories

Find more on Acoustics, Noise and Vibration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!