필터 지우기
필터 지우기

Error in plotting a piecewise function.

조회 수: 2 (최근 30일)
Amna Habib
Amna Habib 2023년 6월 19일
댓글: Amna Habib 2023년 6월 22일
I am trying to plot the a piecewise function. But the system is showing an error.
Kindly help to find this error.
X = 0:0.01:60 ;
f = double(X<10).* (0) + ...
double(and(X>=10,X<20)).* (0.2 + ((0.2).*(x-10))./10) + ...
double(and(X>=20,X<30)).* (0.4 + ((0.2).*(x-20))./10) + ...
double(and(X>=30,X<40)).* (0.6 + ((0.2).*(x-30))./10) + ...
double(and(X>=40,X<50)).* (0.8 + ((0.2).*(x-40))./10) + ...
double(X>=50).*(1);
g = double(X<10).* (1) + ...
double(and(X>=10,X<20)).* (0.2 + ((0.2).*(20-x))./10) + ...
double(and(X>=20,X<30)).* (0.4 + ((0.2).*(30-x))./10) + ...
double(and(X>=30,X<40)).* (0.6 + ((0.2).*(40-x))./10) + ...
double(and(X>=40,X<50)).* (0.8 + ((0.2).*(50-x))./10) + ...
double(X>=50).*(0);
figure;
plot(X,f,'r','linewidth',2);
hold on ;
plot(X,g,'b','linewidth',2);
xlabel('X');
grid on;
xticks(2:1:10);
legend('f(x)','g(x)')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Command window:
Error in empirical (line 2)
f =double(X<10).* (0) + ...
X = 0:0.01:60 ;
f = double(X<10).* (0) + ...
double(and(X>=10,X<20)).* (0.2 + ((0.2).*(x-10))./10) + ...
double(and(X>=20,X<30)).* (0.4 + ((0.2).*(x-20))./10) + ...
double(and(X>=30,X<40)).* (0.6 + ((0.2).*(x-30))./10) + ...
double(and(X>=40,X<50)).* (0.8 + ((0.2).*(x-40))./10) + ...
double(X>=50).*(1);
g = double(X<10).* (1) + ...
double(and(X>=10,X<20)).* (0.2 + ((0.2).*(20-x))./10) + ...
double(and(X>=20,X<30)).* (0.4 + ((0.2).*(30-x))./10) + ...
double(and(X>=30,X<40)).* (0.6 + ((0.2).*(40-x))./10) + ...
double(and(X>=40,X<50)).* (0.8 + ((0.2).*(50-x))./10) + ...
double(X>=50).*(0);
figure;
plot(X,f,'r','linewidth',2);
hold on ;
plot(X,g,'b','linewidth',2);
xlabel('X');
grid on;
xticks(2:1:10);
legend('f(x)','g(x)')

채택된 답변

Ayush Kashyap
Ayush Kashyap 2023년 6월 19일
As per my understanding, one direct reason for error is that the variable x is not defined in the script but is used to define the piecewise functions for f and g. Instead, you should be using the variable X that is defined as a range of values in your script.
To fix the error, you simply need to replace all occurrences of x with X in the definition of f and g.
  댓글 수: 2
Amna Habib
Amna Habib 2023년 6월 19일
Thanks a lot! I have found the error.
Amna Habib
Amna Habib 2023년 6월 22일
Can you please mark the error in the following code?
I'm facing the error:
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);
>> linear
Linear Function:
Parameters: [1x1 struct]

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Poorna
Poorna 2023년 6월 19일
hi amna,
i think some of the the letter 'x' is replaced by small x instead of capital X rectify it it will work
  댓글 수: 1
Amna Habib
Amna Habib 2023년 6월 19일
Thanks a lot! I have found the error.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by