What data type is valid for piecewise functions?
이전 댓글 표시
I am trying to graph a simple piecewise function.
Basically, I am trying to graph:
for x < 0, y = -x,
for x>0, y = x.
My code looked like this:
x = -10:1/100:10;
y = piecewise(x<0, -x, x>0, x);
plot(x,y,'r')
axis([-10 10 -10 10])
But when I attempt to do it using the piecewise command, I get this message:
Undefined function 'piecewise' for input arguments of type 'double'.
So after that I tried using the cast command to change the data type.
Then, my code looked like this:
x = -10:1/100:10;
b = cast(x, 'single')
y = piecewise(b<0, -b, b>0, b);
plot(b,y,'r')
axis([-10 10 -10 10])
and once again, I got
Undefined function 'piecewise' for input arguments of type 'single'.
And I tried this for all data types and it didn't work. What am I missing?
Here is my code:
% This plots the x-axis
x = -100:1/100:100;
y = 0*x;
plot(x,y,'k')
hold on
%This plots the y-axis
x = -100:1/100:100;
y = 9999*x;
plot(x,y,'k')
hold on
%This is the graph
x = -10:1/100:10;
y = piecewise(x<0, -x, x>0, x);
plot(x,y,'r')
axis([-10 10 -10 10])
댓글 수: 7
dpb
2020년 8월 6일
piecewise is part of Symbolic Toolbox -- looks like you don't have it; the message is saying it can't find the function piecewise; in this case the input argument type part of the message is a distraction.
What does
which -all piecewise
return?
Chase Reiter
2020년 8월 6일
Chase Reiter
2020년 8월 6일
Alan Stevens
2020년 8월 6일
Why not just:
x = -10:1/100:10;
y = abs(x);
plot(x,y,'r')
axis([-10 10 -10 10])
Chase Reiter
2020년 8월 6일
dpb
2020년 8월 6일
Type it in at the command line to see what MATLAB returns for the piecewise function resolution.
Chase Reiter
2020년 8월 6일
채택된 답변
추가 답변 (1개)
카테고리
도움말 센터 및 File Exchange에서 Assumptions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
