How to plot a 2D graph using all the for loop values

1 回表示 (過去 30 日間)
Pranjal Pathak
Pranjal Pathak 2013 年 7 月 6 日
コメント済み: Anders Kipp 2016 年 2 月 15 日
Hi, I have the following equation to be plotted in Matlab:
***************
alp=1;
K=1;
E0=1;
for w=-20:1:20;
a=K*(cos(pi*alp/2)*w^alp+w^(2*alp));
b=1+2*(cos(pi*alp/2)*w^alp+w^(2*alp));
c=a/b;
E(w)=E0*(1+c);
end
figure(1)
plot(E,w)
xlabel('w');
ylabel('E');
title('Plot of E vs w')
*********
Can anyone tell me what is wrong in the above code, as I am unable to get the plot.
Thanking You!

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 6 日
編集済み: Azzi Abdelmalek 2013 年 7 月 6 日
Your errors:
  1. you can not use a negative index
  2. when you write for w=-20:20, at the end of the loop w=20, is just one value
  3. you should use plot (w,E) instead of plot(E,w)
alp=1;
K=1;
E0=1;
E=[];
for w=-20:1:20;
a=K*(cos(pi*alp/2)*w^alp+w^(2*alp));
b=1+2*(cos(pi*alp/2)*w^alp+w^(2*alp));
c=a/b;
E(end+1)=E0*(1+c);
end
figure(1)
w=-20:20
plot(w,E)
xlabel('w');
ylabel('E');
title('Plot of E vs w')t(E,w)
  1 件のコメント
Pranjal Pathak
Pranjal Pathak 2013 年 7 月 6 日
Thanks a lot Azzi! This was what I was looking for. I have a similar problem which I have posted yesterday. Can you help me here: http://www.mathworks.com/matlabcentral/answers/81191-how-to-plot-using-all-the-for-loop-values

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGeometry and Mesh についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by