Saturday, November 19th, 2011, 12:23 am
Making Spirals in Octave/MATLAB
ccasionally, in numerical programming, one may wish to plot or instantiate a grid of a circular (or spiral) nature. Here are some code samples for those trying to achieve it. The following sets some values of interest:
lines=10
degrees=180
Now, let us generate some linear, equally-spaced points:
t_map = linspace(0,lines*pi*2,lines*degrees*2);
What we have here is the 2 for 360 degrees, which may depend on what one tries to achieve. For the more complex case shown later, this will be essential. Now, let’s do the sine/cosine magic:
x_map = t_map.*cos(t_map);
y_map = t_map.*sin(t_map);
This can be plotted with plot(x_map, y_map)
, but let’s do something more interesting by assigning colour to the sample points and then plotting them as dense dots. The following would work:
flat=zeros(1000,1000); for ll=(size(x_map,2)/2):-1:1 flat(floor(x_map(ll*2))'+500,floor(y_map(ll*2))'+500)=1 end imshow(flat)
The values here are rather arbitrary and can be used for demonstrative purposes. The problem itself seems to be commonly recurring, thus the need for a blog post. Different colours can be assigned to sample points, which achieves something like the following: