How to plot a for loop?

2 ビュー (過去 30 日間)
Mike Holbach
Mike Holbach 2015 年 3 月 3 日
回答済み: A Jenkins 2015 年 3 月 3 日
Hey guys, I'm having difficulty plotting this for loop. It keeps only giving me x values of -2 to 4 on the graph, but I need it to plot from -2 to 6
%% Problem 1.2
clc, clear all
e=2.718;
x=-2:6;
for k1=1:length(x)
if x(k1)<-1
y(k1)=e.^(x(k1)+1)
elseif x(k1)>=-1 && x(k1)<5
y(k1)=2+cos(pi.*x(k1))
elseif x>=5
y(k1)=10.*(x(k1)-5)+1
else
fprintf('\nanswer not given\n')
y(k1)=NaN
end
end
plot(x,y)

採用された回答

A Jenkins
A Jenkins 2015 年 3 月 3 日
change
elseif x>=5
to
elseif x(k1)>=5

その他の回答 (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