How to plot a smoother graph or wave?

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

采纳的回答

Star Strider
Star Strider 2015-12-19
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 个评论
Ruzam
Ruzam 2015-12-20
编辑:Ruzam 2015-12-20
ahh yeah, I knew about the vectorise multiplication, however I just hadn't thought of ./ . Thank you!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by