フィルターのクリア

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 Center および File ExchangeNumber Theory についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by