How do I plot this piecewise function?

8 views (last 30 days)
Zac
Zac on 3 Jul 2022
Commented: Sam Chak on 4 Jul 2022
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!

Accepted Answer

Sam Chak
Sam Chak on 3 Jul 2022
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 Comments
Sam Chak
Sam Chak on 4 Jul 2022
You're welcome, @Zac. Hope others like @dongshan xie would benefit from the methods shared here.

Sign in to comment.

More Answers (2)

dongshan xie
dongshan xie on 3 Jul 2022
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)

Carter Sorensen
Carter Sorensen on 3 Jul 2022
Edited: Carter Sorensen on 3 Jul 2022
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)')

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by