How can I plot the graph?

9 ビュー (過去 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 Center および File ExchangeLog Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by