Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
Pranjal Pathak
Pranjal Pathak am 6 Jul. 2013
Kommentiert: Anders Kipp am 15 Feb. 2016
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!

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 6 Jul. 2013
Bearbeitet: Azzi Abdelmalek am 6 Jul. 2013
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 Kommentar
Pranjal Pathak
Pranjal Pathak am 6 Jul. 2013
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by