Ploting a function in a for loop

2 ビュー (過去 30 日間)
Ahmet Oguz
Ahmet Oguz 2016 年 11 月 22 日
回答済み: Walter Roberson 2016 年 11 月 23 日
d starts from zero and end at 0.99. For each d, i want to calculate y function. After that i want to plot y versus d. However my code does not generate plot. What am i doing wrong?
clc
k=0;
for d=0:0.01:0.99
k=k+1;
y(k)=1/(1+0.018*(d/(1-d)+d));
plot (y(k),d)
end

採用された回答

Walter Roberson
Walter Roberson 2016 年 11 月 23 日
By default, when you plot() a single point, no marker is used. Also, no line is drawn unless you plot at least two points at the same time. Furthermore, you have not used "hold on" so your later plots erase the first.
You should use a different strategy:
dvals = 0 : 0.01 : 0.99;
for k = 1 : length(dvals)
d = dvals(k);
y(k)=1/(1+0.018*(d/(1-d)+d));
end
plot(dvals, y)
Or, more simply,
d = 0 : 0.01 : 0.99;
y = 1 ./ (1 + 0.018 .* (d ./ (1-d) + d));
plot(dvals, y)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by