[Too many input arguments] Hello, I have problem with my s-function,I guess a classical error. When I run the following code
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
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
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
thinh le duc
on 7 Feb 2020
Edited: thinh le duc
on 7 Feb 2020
Thanks For the help, I have identified a part of the error, but still have the same error, please help me to continue.
function sys=mdlOutputs(t,x,u)
global 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
w=[x(1) x(2) x(3) x(4) x(5)]';
e=w'*h+x(6);
sys=e;
- while executing MATLAB S-function 'rbf_sensorless_test', flag = 3 (output), at time 0.0.
- Too many output arguments.
Walter Roberson
on 7 Feb 2020
Edited: Walter Roberson
on 7 Feb 2020
[sys,x0,str,ts]=mdlOutputs(t,x,u);
That needs four outputs but the function is only defined with one output.
function [sys,x0,str,ts]=mdlOutputs(t,x,u)
global 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
w=[x(1) x(2) x(3) x(4) x(5)]';
e=w'*h+x(6);
sys=e;
i tried but it did not work!! :((
- while executing MATLAB S-function 'rbf_sensorless_test', flag = 3 (output), at time 0.0.
- Index exceeds array bounds.
x1=u(1);
x2=u(2);
x=[x1 x2]';
So your x will be a 2 x 1 vector, overwriting the x that was passed in to the function.
w=[x(1) x(2) x(3) x(4) x(5)]';
There you need the 5th element of the vector that is now only 2 elements.
e=w'*h+x(6);
and there you need the 6th element of the vector that is now only 2 elements.
Sorry I just started learning this, so there are many mistakes. I tried again but it still didn't work. Thank you so much, please help me!!
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 = 1;
sizes.NumSampleTimes = 0;
sys=simsizes(sizes);
x0 = [0 0 0 0 0 0]';
str=[];
ts=[];
c=[-0.5 -0.25 0 0.25 0.5;-0.5 -0.25 0 0.25 0.5];
b=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
m=u(1);
n=u(2);
m_n=[m n]';
h=zeros(5,1);
for j=1:1:5
h(j)=exp(-norm(m_n-c(:,j))^2/(2*b^2));
end
dw=(1/L)*n*Y*h;
de=(1/L)*n;
sys(1)=dw(1);
sys(2)=dw(2);
sys(3)=dw(3);
sys(4)=dw(4);
sys(5)=dw(5);
sys(6)=de;
function sys=mdlOutputs(~,~,u)
global c b
m=u(1);
n=u(2);
w=[x(1) x(2) x(3) x(4) x(5)]';
m_n=[m n]';
h=zeros(5,1);
for j=1:1:5
h(j)=exp(-norm(m_n-c(:,j))^2/(2*b^2));
end
e=w'*h+x(6);
sys=e;
This is my problem:

You are not building m=sin(t), n=sin(t-pi/3) ?
Your e output uses ϵ but you have not defined that; you have defined
. But you are not calculating
from n ?
At the moment it is not obvious that you need any output other than e ?
I have provided missing information. I used simulink

The ultimate goal is to calculate "e".
I already calculated the ε
function sys=mdlDerivatives(~,~,u)
global Y L c b
m=u(1);
n=u(2);
m_n=[m n]';
h=zeros(5,1);
for j=1:1:5
h(j)=exp(-norm(m_n-c(:,j))^2/(2*b^2));
end
dw=(1/L)*n*Y*h; %.............
de=(1/L)*n; %.............
sys(1)=dw(1);
sys(2)=dw(2);
sys(3)=dw(3);
sys(4)=dw(4);
sys(5)=dw(5);
sys(6)=de;
function sys=mdlOutputs(~,~,u)
global c b
m=u(1);
n=u(2);
w=[x(1) x(2) x(3) x(4) x(5)]';
m_n=[m n]';
h=zeros(5,1);
for j=1:1:5
h(j)=exp(-norm(m_n-c(:,j))^2/(2*b^2));
end
ex=x(6);
e=w'*h+ex;
sys=e;
Hope you continue to help me :D
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 should not be the same as your mdlOutputs . https://www.mathworks.com/matlabcentral/answers/366520-how-can-explain-the-relation-between-mdlderivatives-output-and-mdloutputs-in-s-function-simulink#answer_291095
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.
Thanks so much, I will learn more through what you have instructed. :D
More Answers (0)
Categories
Find more on MATLAB in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)