How can I plot a function with a conditions in a simple way?
Info
This question is closed. Reopen it to edit or answer.
Show older comments
i want to plot this separated function:

I was able to plot the function graph using this method :
clear all
clc
w1=0:1:5000;
w12=-5000:1:0;
M1=-w1.*(w1-5000);
M12=-w12.*(w12+5000);
plot(w1,M1,'b',w12,M12,'b');
xlabel('w')
ylabel('M')
and all good...
but i tried another simple logic method:
clear all
clc
w=-5000:1:5000;
if(w>=0 && w<=5000)% here i have a problem with "w" -with orange underline
M1=-w.*(w-5000);
else
M1=-w.*(w+5000);
end
plot(w,M1,'b');
xlabel('w')
ylabel('M')
and it's not working...why? what is the problem? there is an option to plot this function with conditions?
this is the error message:
"Operands to the || and && operators must be convertible to logical scalar values.
Error in Untitled15 (line 5)
if(w>=0 && w<=5000)"
Answers (1)
madhan ravi
on 19 Jul 2019
w = -5000:5000;
M = -w.*(w-5000) .* (w >= 0 & w <= 5000) +...
-w.*(w+5000) .* (w >= -5000 & w <= 0);
plot(w,M)
1 Comment
ron tzuberi
on 19 Jul 2019
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!