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 件のコメント
Geoff Hayes
2014 年 9 月 7 日
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.
Billy Albritton
2014 年 9 月 7 日
Billy Albritton
2014 年 9 月 7 日
回答 (4 件)
dpb
2014 年 9 月 7 日
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
2014 年 9 月 7 日
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 件のコメント
Billy Albritton
2014 年 9 月 7 日
Geoff Hayes
2014 年 9 月 8 日
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.
Star Strider
2014 年 9 月 7 日
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
2014 年 9 月 8 日
0 投票
5 件のコメント
Star Strider
2014 年 9 月 8 日
編集済み: Star Strider
2014 年 9 月 8 日
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.
Geoff Hayes
2014 年 9 月 8 日
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?
Star Strider
2014 年 9 月 8 日
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.
Geoff Hayes
2014 年 9 月 8 日
I misunderstood - thanks for the clarification.
Star Strider
2014 年 9 月 8 日
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.
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
