Plotting sine functions using linspace command
How do I create a regularly-spaced array of values in MATLAB? - Stack Overflow
When to use Linspace?
How to use linspace on a matrix
Videos
There are a couple of ways you can do this:
Using the colon operator:
startValue = 1; endValue = 10; nElements = 20; stepSize = (endValue-startValue)/(nElements-1); A = startValue:stepSize:endValue;Using the
linspacefunction (as suggested by Amro):startValue = 1; endValue = 10; nElements = 20; A = linspace(startValue,endValue,nElements);
Keep in mind that the number of elements in the resulting arrays includes the end points. In the above examples, the difference between array element values will be 9/19, or a little less than 0.5 (unlike the sample array in the question).
linspace generates linearly spaced vectors:
>> A = linspace(1, 10, 20-1)
ans =
1 1.5 2 2.5 3 3.5 ... 9.5 10
I'm working with continuous-time (CT) and discrete-time (DT) signal and want to plot them in Matlab.
For CT, when I used the function linspace to create a vector of values and then use that to have a function (t1, x1 and t2, x2). I then plot the value of x() against the created t(). The graphs I got were different with different length of my vector generated by linspace and none of them seems correct. (The correct version is using the normal method* of creating vector (t3, x3)).
Code:
f=100; %Frequency t1=linspace(0,1,100); %From 0 to 1 in 100 steps t2=linspace(0,1,300); %From 0 to 1 in 300 steps t3=0:0.001:1; %From 0 to 1 and increment by 0,001 x1=cos(2*pi*f*t1); x2=cos(2*pi*f*t2); x3=cos(2*pi*f*t3); subplot(3,1,1); plot(t1,x1); subplot(3,1,2); plot(t2,x2); subplot(3,1,3); plot(t3,x3);
The same problem does not apply whether or not I use linspace or normal method* to create an array to plot magnitude and phase of a function.
Code:
w=linspace(-10*pi,10*pi,1000); xa=(i*w)./(1+i*w); figure(1); subplot(2,1,1);plot(w,abs(xa));grid on; subplot(2,1,2);plot(w,angle(xa));grid on;
If I generate w by normal method*, the graph still looks the same.
Can someone please explain!
Thanks.
Graphs of (t1, x1) (t2, x2) (t3, x3)I am trying to use linspace to create a time vector that is equal to length of the number of samples in an array. For example, I have a test with 30000 samples and each was taken 5ms in between As a result, first samples starts at 0, second at 5, third at 10th, and so on. I am trying to use linspace to accomplish this but running into problems. Here is what I am trying to do *test is the array with all the samples*
time = linspace(0, length(test), 5)
I am trying to create a vector that starts at zero and increments by 5 for each sample. All this does is create 5 times that are crazily spaced. I realize that the last variable actually just generate 5 number of points but thats not what I am trying to do as I want the step size to be 5. Any ideas? Thanks so much in advance!