Introduction About Site Map

XML
RSS 2 Feed RSS 2 Feed
Navigation

Main Page | Blog Index

Saturday, November 19th, 2011, 12:23 am

Making Spirals in Octave/MATLAB

Occasionally, 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:

Spiral examples

Technical Notes About Comments

Comments may include corrections, additions, citations, expressions of consent or even disagreements. They should preferably remain on topic.

Moderation: All genuine comments will be added. If your comment does not appear immediately (a rarity), it awaits moderation as it contained a sensitive word or a URI.

Trackbacks: The URI to TrackBack this entry is:

http://schestowitz.com/Weblog/archives/2011/11/19/spirals-in-octave-matlab/trackback/

Syndication: RSS feed for comments on this post RSS 2

    See also: What are feeds?, Local Feeds

Comments format: Line and paragraph breaks are automatic, E-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Back to top

Retrieval statistics: 22 queries taking a total of 0.098 seconds • Please report low bandwidth using the feedback form
Original styles created by Ian Main (all acknowledgements) • PHP scripts and styles later modified by Roy Schestowitz • Help yourself to a GPL'd copy
|— Proudly powered by W o r d P r e s s — based on a heavily-hacked version 1.2.1 (Mingus) installation —|