??? Error using ==> events Too many input arguments.

5 views (last 30 days)
Hi I am running a file (RP3.m) to solve a differential equation. This file uses the function file (zdot3.m) to get the derivative and another events file (events.m) to identify the event to stop the program. I am getting the following error when I run the file. This has started after the introduction of the events file.
PLEASE HELP
TIA
The 3 files are given below
--ERROR MESSAGE--
??? Error using ==> events
Too many input arguments.
Error in ==> odeevents at 29
eventValue =
feval(eventFcn,t0,y0,eventArgs{:});
Error in ==> ode45 at 207
[haveEventFcn,eventFcn,eventArgs,valt,teout,yeout,ieout]
= ...
Error in ==> RP3 at 21
[t z] = ode45(@zdot3, tspan, z0,
options, p);
------------------------------
The 3 files are given below :
File 1 - RP3.m
% time span based on bubble natural frequency - for Ro = 10 mm, tspan = 9900
% microsec
% tspan = [0 105e-6]; z0 = [0.01 ; 0]
tspan = [0 500e-6]; z0 = [10e-3 ; 0] % z0(1)=Initial buuble radius, m
% z0(2) = Initial bubble velocity, m/s
options = odeset('stats', 'on', 'outputfcn', 'odeplot', 'Events', @events);
gamma = 1.33;
sig = 0.0725; % Surface tension
pvp = 2330;
patm = 1*10^5;
pa = 70000;
w = 31700;
mu = 1e-3; % Viscosity (kg/ms) of water from EXERC IN ADVANC COMP MECH
R0 = 10e-3 ; % Initial bubble radius in metres
%p = zeros(8,1);
p(1) = w; p(2) = pa; p(3) = patm; p(4)= gamma; p(5) = sig;
p(6) = pvp; p(7) = mu; p(8) = R0;
[t z] = ode45(@zdot3, tspan, z0, options, p);
x = z(:, 1); y = z(:, 2);
subplot(1,3,1); plot(t,x)
xlabel('time'); ylabel('Radius');
subplot(1,3,2); plot(t,y)
xlabel('time'); ylabel('Velocity');
subplot(1,3,3); plot(y.*t/0.01, x./0.01)
xlabel('Ut/R0'); ylabel('R/R0');
File 2 - zdot3.m
function rdot = zdot3(t, z, p)
w = p(1); pa = p(2); p0 = p(3); gamma = p(4); sig = p(5);
pvp = p(6); mu = p(7); R0 = p(8);
A = 1*10^5 + pa*sin(w*t) ; % A = p0 + pinf *sin(wt)
pg0 = p0 - pvp + 2*sig/R0; % pg0 = patm - pvp + 2*sig/R
B = pg0 * (z(1) / R0)^(3*gamma);
C = 4*mu*z(2)/z(1);
D = 2*sig/z(1); % ST of water from EXERC IN ADVANC COMP MECH
rdot = [z(2); (-3/2*z(2)^2 + pvp - A+B-C-D)/z(1)];
end
File 3 - events.m
function [eventvalue, stopthecalc eventdirection] = events(t, z)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
R = z(1);
Rdot = z(2);
eventvalue = R/10e-3 - 0.01;
stopthecalc = 1;
eventdirection = -1
end
==================================================

Accepted Answer

Geoff Hayes
Geoff Hayes on 22 Jun 2014
I took your code and was able to reproduce the same behaviour. One thing that MATLAB complained about was the naming of events.m
Warning: Function events has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
I'm using version 2014a, so you may not have this conflict. I renamed this function name (and file) to events3.m (you may want to as well, just to be safe) and its usage in RP3.m. When I re-ran the program, I got the same error but for Error using events3.
If a breakpoint is put at line 29 in odeevents,
feval(eventFcn,t0,y0,eventArgs{:});
you can see that the eventFcn is your events3 function, but three inputs are being passed in - t0 and y0 as expected, but a third eventArgs{:} is a 1x8 array of doubles. Your events3 function doesn't expect (or need) a third input and so you get the error. This third input is your p from the RP3.m file at
[t z] = ode45(@zdot3, tspan, z0, options, p);
I think what you were trying to do here is to pass p as an additional input to zdot3 which does require three inputs, with the last being p. Is that the case?
I don't think that ode45 allows for additional parameters to be passed explicitly as inputs so that they can be used in your function zdot3. However, they do discuss two methods two work around this - see ode45 and the section Parameterizing Functions .
The easiest is to just create a nested function - a function within a function. In your RP3.m file, turn this script into a function by adding a signature to the first line, and adding end to the last line
function RP3
% all code that you have already
end
So now you have changed your script into a function. Now nest your zdot3 function within this one. Just copy and paste this function code into RP3.m just before the end that you added
function RP3
% time span based on bubble natural frequency …
% all code that you have already
% etc.
xlabel('Ut/R0'); ylabel('R/R0');
% nest zdot3
function rdot = zdot3(t, z)
w = p(1); pa = p(2); p0 = p(3); gamma = p(4); sig = p(5);
pvp = p(6); mu = p(7); R0 = p(8);
A = 1*10^5 + pa*sin(w*t) ; % A = p0 + pinf *sin(wt)
pg0 = p0 - pvp + 2*sig/R0; % pg0 = patm - pvp + 2*sig/R
B = pg0 * (z(1) / R0)^(3*gamma);
C = 4*mu*z(2)/z(1);
D = 2*sig/z(1); % ST of water from EXERC IN ADVANC COMP MECH
rdot = [z(2); (-3/2*z(2)^2 + pvp - A+B-C-D)/z(1)];
end
end
Note that the above nested function no longer has the p input - it will be able to access that from the parent function workspace (same is true for w,pa,gamma,sig,pvp,mu, and R0 - you could probably eliminate the need for p altogether).
Then, in the parent function, replace
[t z] = ode45(@zdot3, tspan, z0, options, p);
with
[t z] = ode45(@zdot3, tspan, z0, options);
And you should be good to go. I was able to run the example to completion. Try the above and see what happens!
  2 Comments
Qiaoli Ji
Qiaoli Ji on 15 Nov 2017
Yes, it works. Thanks very much. You help me a lot.
Qiaoli Ji
Qiaoli Ji on 15 Nov 2017
When we add zdot3 to RP3 file, it works. But function file does not save variables in worksheet. It is not convenient. So maybe we should apart from the two files.

Sign in to comment.

More Answers (1)

Sreedhar
Sreedhar on 22 Jun 2014
Star Strider & Hayes
Many thanks for the replies.
I solved the problem (see http://www.mathworks.in/matlabcentral/newsreader/view_thread/109755) by introducing the argument 'p' (which was supplied to the function 'rdot') in the list of arguments to the events fucntion i.e. the events function is now => events (t, z, p).
I now have another problem. How can i return variables calculated in the function zdot ? For eg. I want to use the variable B calculated in zdot in the main program RP3. But when I do it like this => function [rdot, B] = zdot3(t, z) what I see is a null matrix in RP3.
THANKS IN ADVANCE
  4 Comments
Qiaoli Ji
Qiaoli Ji on 15 Nov 2017
Hi, I have met the same question to you. I used the method (nested function) of Hayes and it works. But when I try to used the above link that add p to event function, the matlab told me that the zdot3 function had too less input arguments. Could you help me? Thanks very much.

Sign in to comment.

Categories

Find more on Get Started with MATLAB 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!