I am new to MATLAB and currently working on my homework assignment. I am trying to declare the x
variable as the following:
Create a linearl
You can use linspace
as follow:
x = linspace(-pi, pi, 200);
check this out for an example: https://www.mathworks.com/help/matlab/ref/linspace.html
The other answer shows how to use linspace
, this is the correct method.
But you can also use the colon operator and some simple arithmetic to do this:
x = -pi : 2*pi/199 : pi
-- This means: go from -π to π in steps of such a size that we get exactly 200 values.
x = (0:199) * (2*pi/199) - pi
-- This means: create an array with 200 integer values, then scale them to the right range.
Note that you shouldn't use square brackets []
here. They are for concatenating arrays. The colon operator creates a single array, there is nothing to concatenate it with.