Piecewise Function Graphing Code Help
1 view (last 30 days)
Show older comments
Hello all, I am trying to graph a piecewise function (shown with code below) and keep getting error saying "Error using piecewise (line 2) Not enough input arguments". I need an explanation as I am still learning MatLab. Thank you!
____________________________________________________________________________________________________________________________________
function y = piecewise(x)
x1 = x(0<=x & x<1);
y(find(0<=x & x<1)) = 2.1592*x1+2;
x2=x(1<=x & x<2);
y(find(1<=x & x<2)) = 1.0105*x2+1.1487;
x3=x(2<=x & x<3);
y(find(2<=x & x<3)) = 2.2635*x3-1.3573;
x4=x(3<=x & x,4);
y(find(3<=x & x,4)) = 3.7079*x4-5.6905;
x5=x(4<=x & x<5);
y(find(4<=x & x<5)) = 5.2649*x5-9.1411;
x6=x(5<=x & x<6);
y(find(5<=x & x<6)) = 6.8970*x6-20.079;
plot(x, y)
axis([-1 7 -25 25])
grid on
5 Comments
dpb
on 27 Jul 2015
Yep...the line you executed says only "piecewise" but the definition of the function is that it requires a vector x. Hence, quite rightly, Matlab complains that there aren't enough input arguments since zero arguments are fewer than one. :)
It appears that the function could have been written w/o needing an argument since the ranges internally are hardcoded, but that wasn't the way it was chosen to do it. Try
piecewise([0:0.1:6])
or, of course, define a vector and pass it...
x=[0:0.1:6];
piecewise(x)
Answers (0)
See Also
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!