필터 지우기
필터 지우기

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 CenterFile Exchange에서 2-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