I need help using for and if for plotting a function.

1 回表示 (過去 30 日間)
Tasos Novas
Tasos Novas 2019 年 12 月 9 日
回答済み: Walter Roberson 2019 年 12 月 9 日
clc
clear
for e= 0:0.02/1071:0.02
if (e<0.01)
s(e)=100*e
elseif (e>0.01)
s(e)=75.3*e
end
end
plot(e,s)

採用された回答

Walter Roberson
Walter Roberson 2019 年 12 月 9 日
e_vals = 0:0.02/1071:0.02;
num_e = length(e_vals);
s = zeros(1, num_e);
for e_idx = 1 : num_e
e = e_vals(e_idx);
if (e<0.01)
s(e_idx)=100*e;
elseif (e>0.01)
s(e_idx)=75.3*e;
else
s(e_idx) = nan;
end
end
plot(e_vals,s)
If you do not want the nan in the output, then you should reconsider whether you really want to use elseif

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by