Error in plotting a piecewise function.
1 view (last 30 days)
Show older comments
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)')
0 Comments
Accepted Answer
Ayush Kashyap
on 19 Jun 2023
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.
More Answers (1)
Poorna
on 19 Jun 2023
hi amna,
i think some of the the letter 'x' is replaced by small x instead of capital X rectify it it will work
See Also
Categories
Find more on Filter Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!