필터 지우기
필터 지우기

How do I plot this piecewise function?

조회 수: 79 (최근 30일)
Zac
Zac 2022년 7월 3일
댓글: Sam Chak 2022년 7월 4일
I am trying to plot a piecewsie function. I followed the instructions on the following page:
However, I keep getting this error message:
Error using fplot
Input must be a function handle or symbolic function.
Error in piecwise (line 9)
fplot(x)
This is my code:
clear all;
clc;
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms t
x = piecwise(t <= -2, 0, -2 <= t <= 0, ((2 + t) / 2)^2, 0 <= t <= 2, 1, 2 <= t <= 4, ((4 - t) / 2)^2, t >= 4);
fplot(x)
This is the piecewise function I am trying to plot:
Any help is greatly appreciate!

채택된 답변

Sam Chak
Sam Chak 2022년 7월 3일
Hi @Zac
Because you mispelled 'piecwise'?
Anyhow, you can try using one of these methods:
t = -3:1e-3:5;
%% Method 1
x = @(t) 0*(t<-2) + (((2 + t)/2).^2).*((-2 <= t) & (t < 0)) + 1*((0 <= t) & (t < 2)) + (((4 - t)/2).^2).*((2 <= t) & (t < 4)) + 0*(4 <= t);
subplot(3,1,1)
plot(t, x(t), 'linewidth', 1.5, 'Color', [0.8500, 0.3250, 0.0980])
ylim([-0.5 1.5]), grid on, xlabel('t'), ylabel('x(t)'), title('Method 1')
%% Method 2
x1 = 0*(t<-2); % segment 1
x2 = (((2 + t)/2).^2).*((-2 <= t) & (t < 0)); % segment 2
x3 = 1*((0 <= t) & (t < 2)); % segment 3
x4 = (((4 - t)/2).^2).*((2 <= t) & (t < 4)); % segment 4
x5 = 0*(4 <= t); % segment 5
x = x1 + x2 + x3 + x4 + x5; % combine all segments
subplot(3,1,2)
plot(t, x, 'linewidth', 1.5, 'Color', [0.9290, 0.6940, 0.1250])
ylim([-0.5 1.5]), grid on, xlabel('t'), ylabel('x(t)'), title('Method 2')
%% Method 3
syms t
x = piecewise(t<-2,0, -2<t<0,((2 + t)/2)^2, 0<t<2,1, 2<t<4,((4 - t) / 2)^2, 4<t,0);
subplot(3,1,3)
fplot(x, [-3 5], 'linewidth', 1.5, 'Color', [0.4660, 0.6740, 0.1880])
ylim([-0.5 1.5]), grid on, xlabel('t'), ylabel('x(t)'), title('Method 3')
  댓글 수: 3
dongshan xie
dongshan xie 2022년 7월 4일
The second method is pretty cool!
Sam Chak
Sam Chak 2022년 7월 4일
You're welcome, @Zac. Hope others like @dongshan xie would benefit from the methods shared here.

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

추가 답변 (2개)

dongshan xie
dongshan xie 2022년 7월 3일
First, I think it's "piecewise" instead of "piecwise"
Second, you should write "fplot(t,x)".
So, the code should be
syms t
x = piecewise(t <= -2, 0, -2 <= t <= 0, ((2 + t) / 2)^2, 0 <= t <= 2, 1, 2 <= t <= 4, ((4 - t) / 2)^2, t >= 4);
fplot(t,x)
  댓글 수: 2
Carter Sorensen
Carter Sorensen 2022년 7월 3일
Good catch with the fplot(). I completely missed that...
dongshan xie
dongshan xie 2022년 7월 4일
Thank you!

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


Carter Sorensen
Carter Sorensen 2022년 7월 3일
편집: Carter Sorensen 2022년 7월 3일
Hello,
Couldn't quite get fplot() to work with the piecewise() function. One workaround I found is to store the values you want to test in a matrix. Afterwards, pass each value in the matrix to the piecewise() function. From here, plot the outputs against the inputs using the plot() command. The described process may not be the most efficient, but it should work.
Sample code (inputs and outputs) has been included. Are you required to use fplot()?
Let me know if anything is unclear. Thanks!
clear all;
clc;
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
num = [-10:1:10]; % Sample numbers in matrix
syms x(t)
x(t) = piecewise(t <= -2, 0, -2 <= t <= 0, ((2 + t) / 2)^2, 0 <= t <= 2, 1, 2 <= t <= 4, ((4 - t) / 2)^2, t >= 4); % Your piecewise functions
results = x(num); % Plug numbers in matrix 'num' into x(t)
grid on
plot(num, results, 'linewidth', 3) % Plot all num values against corresponding x(t) values
xlabel('num')
ylabel('x(t)')

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by