MATLAB Answers

Plotting a spiral in MATLAB knowing the start and end points and number of turns?

268 views (last 30 days)
Alec Lothian
Alec Lothian on 23 Nov 2018
Commented: km meenu on 19 Jul 2020
I found a code and was able to modify it into the following code to creaet a spiral that starts at the point I want it to but I can't seem to get it to end where I want it to and have the desired numer of turns.
t = 1:5000;
u = .0265;
r0 = 10;
r = r0 +u*t;
omega = .005;
phi0 = 3*pi/2;
phi = -omega*t+phi0;
x = r .* cos(phi);
y = r .* sin(phi);
plot(x,y)
grid on;

  1 Comment

Jacob Shulman
Jacob Shulman on 23 Nov 2018
where do you want it to stop? you'll have to just figure out the value of t at that point. currently you have 5000 values of t. To shorten the spiral, shorten the array t.

Sign in to comment.

Accepted Answer

Jos (10584)
Jos (10584) on 23 Nov 2018
This does the job (for an integer number of turns)
% given values
pos = [8 4 ; % startpoint
2 7 ] ; % endpoint
nturns = 3 ; % number of turns (integer value)
% engine
dp = diff(pos,1,1) ;
R = hypot(dp(1), dp(2)) ;
phi0 = atan2(dp(2), dp(1)) ;
phi = linspace(0, nturns*2*pi, 10000) ; % 10000 = resolution
r = linspace(0, R, numel(phi)) ;
x = pos(1,1) + r .* cos(phi + phi0) ;
y = pos(1,2) + r .* sin(phi + phi0) ;
plot(x,y,'b-',pos(:,1),pos(:,2),'ro-') ; % nturns crossings, including end point

  7 Comments

Show 4 older comments
Jos (10584)
Jos (10584) on 25 Nov 2018
Subtracting the means:
x = x - mean(x)
y = y - mean(y)
will center the spiral given all points. And:
x = x - dp(1)/2
y = y - dp(2)/2
will center the spiral relative to the two points
km meenu
km meenu on 19 Jul 2020
I have a code of galaxy model and spiral line code how i draw the spiral line in the spiral arms of the galaxy

Sign in to comment.

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!