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

2 次查看(过去 30 天)
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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by