Not enough input arguments M-file piecewise function

I keep getting an error in line 3 saying not enough input arguments. Please help? I do not see what I'm missing...
function v=vpiece(t)
if t<=0
v=0;
elseif t<8
v=10*t^2 - 5*t;
elseif t<16
v=624 - 5*t;
elseif t<26
v=36*t +12*(t-16)^2;
else
v=2136*exp(-0.1*(t-26));
end
k=0;
for i=-5:0.5:50
k=k+1;
t(k)=i;
vpiece(t(k));
endplot(t,v);
end

3 Comments

Billy - please format the above code so that it is readable. Highlight the code portions and press the {} Code button.
Is the above code all part of the same vpiece function, or is the code from k=0 onwards outside of the function and is used to invoke it? If it is all part of the same function, then is the intent to make it recursive? (I'm guessing that isn't the case.)
Note the for loop
k = 0;
for i=-5:0.5:50
k=k+1;
t(k)=i;
vpiece(t(k));
end
plot(t,v);
end
The result from vpiece is not used anywhere, yet the plot function make use of a variable v. Should the code read
for i=-5:0.5:50
k = k+1;
t(k) = i;
v(k) = vpiece(t(k));
end
There is also an extra end here which I'm not sure what it refers to.
This is what I keep getting.

Sign in to comment.

Answers (4)

The function prototype is
function v=vpiece(t)
requiring an argument yet you call it as
>> vpiece
with no arguments. Of course, that's not enough input arguments as zero is definitely less than one.
Note the function signature for vpiece is expecting a single input parameter t
function v=vpiece(t)
When you call this function simply as
>> vpiece
with no input parameters, then the function call will fail and the error message makes sense.
I think that you should define your vpiece function as just
function v=vpiece(t)
if t<=0
v=0;
elseif t<8
v=10*t^2 - 5*t;
elseif t<16
v=624 - 5*t;
elseif t<26
v=36*t +12*(t-16)^2;
else
v=2136*exp(-0.1*(t-26));
end
saving all of the above (and only the above) to your vpiece.m file. Then in the Command Window (or in a separate script) execute the code
k = 0;
t = [];
v = [];
for i=-5:0.5:50
k=k+1;
t(k)=i;
v(k) = vpiece(t(k));
end
plot(t,v);
Running the above code, outside of vpiece.m, will allow the function to be called correctly and a plot will be produced.

2 Comments

I just tried running this. I received the same error code. Am I not running it correctly or am I missing something?
Billy - I've attached the vpiece.m file that you can use as your function. Check how it compares to your version. You can just copy the code from one to the other. Then copy the following code and paste it into the Command Window (pressing enter if necessary)
k = 0;
t = [];
v = [];
for i=-5:0.5:50
k=k+1;
t(k)=i;
v(k) = vpiece(t(k));
end
plot(t,v);
You should see the plot that Star Strider has shown in his solution.

Sign in to comment.

With the function defined as:
function v=vpiece(t)
if t<=0
v=0;
elseif t<8
v=10*t^2 - 5*t;
elseif t<16
v=624 - 5*t;
elseif t<26
v=36*t +12*(t-16)^2;
else
v=2136*exp(-0.1*(t-26));
end
end
and the call as:
k=0;
for i=-5:0.5:50
k=k+1;
t(k)=i;
v(k) = vpiece(t(k));
end
plot(t,v)
generates no errors and produces:
%
Thank you all for the help!! I finally got it to work. I thought I was supposed to put the loop at the end in the M-file. I don't know why I thought that, Brain lapse. Thanks again!!

5 Comments

Putting the loop at the end of m-file works — I had the function and the loop in the same m-file.
What you needed were:
  • An ‘end’ statement at the end of the function;
  • Subscripting both t and v in the loop to create the vectors to plot;
  • Putting the plot statement outside and after the loop.
Those were the only changes I made to the code you originally posted.
Putting the loop at the end of m-file works — I had the function and the loop in the same m-file.
But not in a file named vpiece.m, correct?
I have a generic function file that I use for testing multi-line functions, so the ‘vpiece’ function and the loop appeared in order in that file. I never experimented to see if the loop could be in ‘vpiece.m’ but after the final ‘end’ statement in the function. So long as the loop (or any statement calling the function) is outside the function itself (after the final ‘end’ statement in the function), it may not cause a recursion limit error or other problem. In OP’s original code, the loop was inside the final function ‘end’ statement. There were a few other problems, and because they prevented it from running, it never triggered the recursion error.
I misunderstood - thanks for the clarification.
No worries.
As I thought about it later, putting the loop inside the function file but after the actual function may not execute the loop at all.
Haven’t tested this, but I suspect the final ‘end’ statement would execute an implicit ‘return’ and anything after it would not execute.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 7 Sep 2014

Commented:

on 8 Sep 2014

Community Treasure Hunt

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

Start Hunting!