Home Ask Login Register

Developers Planet

Your answer is one click away!

Mohammad Ghorbani February 2016

how i can plot a graph with inetger number as input

how i can plot cos(3 pi/7 * n ) such that n be integer number in interval [x=0,x=10] in matlab? i used this code

X = linspace(0,2*pi,10)';
Y = cos(X);

the graph that i get is this , but i want to show points of graph in integer number of x axis . how can i do that ?

enter image description here


dfri February 2016

Instead of using linspace for such a simple example as this one, you can just define your vector X as an integer vector taking steps of 1, from 0 to 10:

X = 0:1:10;
Y = cos(3*pi/7*X);

Below shows the plot generated by the code snippet above (bottom plot) as well as the same plot using step size 0.5 (X = 0:0.5:10, top plot).

Note that as Adriaan notes in the comments below, the default step size for ... = from:stepsize:to notation is 1, i.e., if omitting the stepsize and simply writing ... = from:to, a step size of 1 is used by default (stepsize=1).

enter image description here

Post Status

Asked in February 2016
Viewed 3,304 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life