Piecewise Function Graphing Code Help

2 vues (au cours des 30 derniers jours)
Jesse Rossman
Jesse Rossman le 26 Juil 2015
Commenté : dpb le 27 Juil 2015
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 commentaires
Jesse Rossman
Jesse Rossman le 27 Juil 2015
Ok. I may just not know what you need exactly because I'm new to Matlab. Here is a screen shot, does this help? Thanks for the help! I appreciate it.
dpb
dpb le 27 Juil 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)

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by