Error in plotting a piecewise function.
3 次查看(过去 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)')
0 个评论
采纳的回答
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.
更多回答(1 个)
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!