Error in plotting a piecewise function.

2 次查看(过去 30 天)
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-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

类别

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