How do I plot this piecewise function?
87 次查看(过去 30 天)
显示 更早的评论
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1053535/image.png)
Any help is greatly appreciate!
0 个评论
采纳的回答
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')
更多回答(2 个)
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
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)')
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Number Theory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!