Filter löschen
Filter löschen

How to plot a smoother graph or wave?

5 Ansichten (letzte 30 Tage)
Ruzam
Ruzam am 19 Dez. 2015
Kommentiert: Star Strider am 20 Dez. 2015
Ok, so I'm fairly new to matlab still. So given the code below I would like to plot a given equation within the loop. Unfortunatly It is only plotting in steps of 1 for w up to 100. Thats why it doesn't look smooth. However if I put w=1:0.1:N for the for loop, it then gives an error and says must be an integer, this makes sense because y(w) says we are accessing an element in an array. So then I just put y=2*sin(2*w)/w; and there is no graph displayed, just a white empty space.
Could I create an array like w=0:0.01:100; Maybe, and then...... arrghh, I can't think right now. The code is below.
N=100;
y=2*sin(2*w)/w;
for w=1:N
y(w)=2*sin(2*w)/w;
end;
%semilogx([1:N],y);
plot([1:N],y);
Any help would be greatly appreciated. Thank you.

Akzeptierte Antwort

Star Strider
Star Strider am 19 Dez. 2015
You first approach is best, although you need to vectorise the division (use ./ instead of /). You do not need the loop:
N=100;
w=1:0.1:N;
y=2*sin(2*w)./w;
plot(w,y);
See the documentation for Array vs. Matrix Operations for details on matrix and element-wise calculations.
  2 Kommentare
Ruzam
Ruzam am 20 Dez. 2015
Bearbeitet: Ruzam am 20 Dez. 2015
ahh yeah, I knew about the vectorise multiplication, however I just hadn't thought of ./ . Thank you!
Star Strider
Star Strider am 20 Dez. 2015
My pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots 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