フィルターのクリア

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 Center および File ExchangeGrid 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