Piecewise Function Graphing Code Help

조회 수: 1 (최근 30일)
Jesse Rossman
Jesse Rossman 2015년 7월 26일
댓글: dpb 2015년 7월 27일
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
Jesse Rossman
Jesse Rossman 2015년 7월 27일
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 2015년 7월 27일
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)

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by