Borrar filtros
Borrar filtros

How to plot a smoother graph or wave?

5 visualizaciones (últimos 30 días)
Ruzam
Ruzam el 19 de Dic. de 2015
Comentada: Star Strider el 20 de Dic. de 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.

Respuesta aceptada

Star Strider
Star Strider el 19 de Dic. de 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 comentarios
Ruzam
Ruzam el 20 de Dic. de 2015
Editada: Ruzam el 20 de Dic. de 2015
ahh yeah, I knew about the vectorise multiplication, however I just hadn't thought of ./ . Thank you!
Star Strider
Star Strider el 20 de Dic. de 2015
My pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Line Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by