Why do I get the error "too many output arguments"?

I call this function:
function nvec = removeByGamma(tR)
t = tR(1);
R = tR(2);
line = linespace(1, length(R), 1);
for i = line
g = findGamma(R(i));
if (g>Gammaf)
R(i) = [];
t(i) = [];
end
end
nvec = [t, R];
end
here
[t, R] = removeByGamma(ode45(@odefun, tspan, r0));
yet I get the error:
Error using solve>removeByGamma
Too many output arguments.
Error in solve (line 26)
[t, R] = removeByGamma(ode45(@odefun, tspan, r0));
I'm a bit new to this and I have no idea what's the problem
Thank you in advance!

 Accepted Answer

You have defined your function removeByGamma with only one output argument
function nvec = removeByGamma(tR)
but then you call it with two output arguments, t and R:
[t, R] = removeByGamma(ode45(@odefun, tspan, r0));
MATLAB is telling you it has no way of knowing how to assign that second argument

3 Comments

Once you have fixed this problem, you will have another.
In your code for the function removeByGamma(tR) it looks like you expect tR to be a n by 2 matrix with time and R values in the first and second column respectively. However when you supply removeByGamma the input argument ode45(@odefun, tspan, r0), you should realize that the result of calling ode45 in this way will be to return a structure, so inside of tR it receives a structure with fields tR.solver, tR.extdata,tR.x,tR.y,...
So if you really want to call your code with ode45(..) as an argument you need to modify the first two lines of removeByGamma to be
t = tR.x
R = tR.y
Alternatively, first call ode45 assigning its outputs to vectors t and R
[t,R] = ode45(@odefun, tspan, r0)
and then modify your removeByGamma function to receive the two vectors
function [t,R] = removeByGamma(t,R)
line = linspace(1, length(R), 1);
for i = line
% etc
Note that you will also have an error, because you meant ot call the function linspace, but in your code you call it linespace (I corrected this in the snippet above)
Thank you so much! have a great day :)

Sign in to comment.

More Answers (0)

Categories

Asked:

on 15 Jun 2023

Commented:

Jon
on 15 Jun 2023

Community Treasure Hunt

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

Start Hunting!