필터 지우기
필터 지우기

How can I plot the graph?

조회 수: 2 (최근 30일)
Sauce
Sauce 2021년 9월 16일
댓글: Chunru 2021년 9월 17일
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

채택된 답변

Chunru
Chunru 2021년 9월 16일
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
Sauce
Sauce 2021년 9월 16일
What does output=zeros(size(x)); do?
Chunru
Chunru 2021년 9월 17일
"output=zeros(size(x));" allocates memory for the variable output and initializes it with 0s. This may make the code more efficient.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Behzad Eydiyoon
Behzad Eydiyoon 2021년 9월 16일
function output = carFunc(x)
x=0:0.1:75;
if (x >= 75)
output = 25;
else
output = x/3;
end
plot(x,output)
end

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by