Effacer les filtres
Effacer les filtres

How to plot a smoother graph or wave?

5 vues (au cours des 30 derniers jours)
Ruzam
Ruzam le 19 Déc 2015
Commenté : Star Strider le 20 Déc 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.

Réponse acceptée

Star Strider
Star Strider le 19 Déc 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 commentaires
Ruzam
Ruzam le 20 Déc 2015
Modifié(e) : Ruzam le 20 Déc 2015
ahh yeah, I knew about the vectorise multiplication, however I just hadn't thought of ./ . Thank you!
Star Strider
Star Strider le 20 Déc 2015
My pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by