Not enough input arguments M-file piecewise function

1 view (last 30 days)
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

Sign in to comment.

Answers (4)

dpb
dpb on 7 Sep 2014
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.

Geoff Hayes
Geoff Hayes on 7 Sep 2014
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
Billy Albritton
Billy Albritton on 7 Sep 2014
I just tried running this. I received the same error code. Am I not running it correctly or am I missing something?
Geoff Hayes
Geoff Hayes on 8 Sep 2014
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.


Star Strider
Star Strider on 7 Sep 2014
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:
%

Billy Albritton
Billy Albritton on 8 Sep 2014
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
Star Strider
Star Strider on 8 Sep 2014
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

Community Treasure Hunt

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

Start Hunting!