フィルターのクリア

How to plot a linear piecewise function on matlab?

3 ビュー (過去 30 日間)
Amna Habib
Amna Habib 2023 年 6 月 26 日
コメント済み: Amna Habib 2023 年 6 月 26 日
Can you please mark the error in the following code?
I'm facing the error:
>> linear
Linear Function:
Parameters: [1x1 struct]
X = 0:0.001:50 ;
f = double(X<20).* (0) + ...
double(and(X>=20,X<35)).* ((X-20)./15) + ...
double(X>=35).*(1);
g = double(X<20).* (1) + ...
double(and(X>=20,X<35)).* ((35-(X))./(15)) + ...
double(X>=35).*(0);
figure;
plot(X,f,'r','linewidth',2);
hold on ;
plot(X,g,'b','linewidth',2);
xlabel('X');
xticks(2:1:10);
  3 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 6 月 26 日
The code expect for the first line you mentioned runs without any error. I am not sure what the command linear supposed to do, nor do we have the data of linear to run to reproduce the error.
If you are getting an error, copy and paste the full error, which means all of the red text.
Also, there's no need of using double().
X = 0:0.001:50 ;
f = (X<20).* (0) + (and(X>=20,X<35)).* ((X-20)./15) + (X>=35).*(1);
g = (X<20).* (1) + (and(X>=20,X<35)).* ((35-(X))./(15)) + (X>=35).*(0);
figure;
plot(X,f,'r','linewidth',2);
hold on ;
plot(X,g,'b','linewidth',2);
xlabel('X');
%xticks(2:1:10);
DGM
DGM 2023 年 6 月 26 日
... or just
% all you need to define a polyline are the vertices
x = [0 20 35 50];
f = [0 0 1 1];
g = [1 1 0 0];
plot(x,f,'r','linewidth',2);
hold on ;
plot(x,g,'b','linewidth',2);
xlabel('X');

サインインしてコメントする。

回答 (1 件)

Sam Chak
Sam Chak 2023 年 6 月 26 日
Might as well...
but this approach requires the fuzzy logic toolbox.
x = 0:0.1:50;
f = linsmf(x, [20 35]); % s-shaped function
g = linzmf(x, [20 35]); % z-shaped function
plot(x, [g; f]', 'linewidth', 2), grid on
xlabel('X');

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品


リリース

R2014b

Community Treasure Hunt

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

Start Hunting!

Translated by