How can I plot the graph?

1 Ansicht (letzte 30 Tage)
Sauce
Sauce am 16 Sep. 2021
Kommentiert: Chunru am 17 Sep. 2021
I want to plot the function where y=x/3 until y=25 where it is constant y=25
function output = carFunc(x)
if (x >= 75)
output = 25;
else
output = x/3;
end
plot(x,carFunc(x))
end

Akzeptierte Antwort

Chunru
Chunru am 16 Sep. 2021
x = 0:100;
plot(x,carFunc(x));
ylim([0 30])
function output = carFunc(x)
output = zeros(size(x));
idx = (x >= 75);
output(idx) = 25;
idx = (x < 75);
output(idx) = x(idx)/3;
end
  2 Kommentare
Sauce
Sauce am 16 Sep. 2021
What does output=zeros(size(x)); do?
Chunru
Chunru am 17 Sep. 2021
"output=zeros(size(x));" allocates memory for the variable output and initializes it with 0s. This may make the code more efficient.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Behzad Eydiyoon
Behzad Eydiyoon am 16 Sep. 2021
function output = carFunc(x)
x=0:0.1:75;
if (x >= 75)
output = 25;
else
output = x/3;
end
plot(x,output)
end

Kategorien

Mehr zu Graph and Network Algorithms finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by