Animate a line in polar coordinates

19 views (last 30 days)
I want to animate a Lemniscate of Bernoulli in polar system. When I addpoints, system display:
Error using animatedline
Argument Y cannot be complex.
My code is under here:
clc, clear, close all;
theta = 0:0.001:2*pi;
r = sqrt(cos(2*theta));
figure;
polarplot(theta, r, 'r', 'LineWidth',1.5);
figure;
h = animatedline(polaraxes,theta,r);
figure;
for k = 1:length(theta)
addpoints(h,theta(k),r1(k));
drawnow
end

Accepted Answer

Sarvesh Kale
Sarvesh Kale on 6 Feb 2023
Edited: Sarvesh Kale on 6 Feb 2023
Hi ロン,
You are trying to animate the Lemniscate of Bernoulli, here is an example in rectangular co-ordinate system
clear
clc;
h=animatedline;
xlim([-1.5 1.5]);
ylim([-1.5 1.5]);
h.LineWidth=1.5;
% parametric representation of Lemniscate of Bernoulli
t = linspace(-4,4,10000);
x = cos(t)./(1+(sin(t).^2));
y = (sin(t).*cos(t))./(1+(sin(t).^2));
for k = 1:10000
addpoints(h,x(k),y(k))
drawnow
end
the above is simple cartesian representation however while doing the polar representation you have to take care of angles as negative quantities cannot be present inside the square root sign as they would lead to complex number so your angles should be constrained, the representation in polar co-ordinates you require is given below
clear;
figure;
h = animatedline(polaraxes);
rlim([0 1]);
h.LineWidth = 1.5;
h.Color=[1 0 0] ;
theta = -pi/4:0.001:pi/4; % cosine of 2*theta will be positive for this range
n= length(theta);
% first half animation
r = -sqrt(cos(2*theta));
for k = 1:length(theta)
addpoints(h,theta(k),r(k))
drawnow
end
% second half animation
r = sqrt(cos(2*theta)); % the negative sign covers second half
theta = pi/4:-0.001:-pi/4;
for k = 1:length(theta)
addpoints(h,theta(k),r(k))
drawnow
end
I hope the above two code snippets achieve your end goal, please accept the answer if the query is answered. Thank you
  1 Comment
ロン
ロン on 6 Feb 2023
Thank you very much. It works! The negative part in square root makes trouble a lot. But your code solve it well.

Sign in to comment.

More Answers (1)

KSSV
KSSV on 6 Feb 2023
In your case r is comlex number which is not allowed. You need to get r as shown below.
theta = 0:0.001:2*pi;
R = 1 ;
x = R*cos(theta) ; y = R*sin(theta) ;
r = sqrt(x.^2+y.^2) ; ;
figure;
polarplot(theta, r, 'r', 'LineWidth',1.5);
figure;
h = animatedline(polaraxes,theta,r);
figure;
for k = 1:length(theta)
addpoints(h,theta(k),r(k));
drawnow
end
  1 Comment
ロン
ロン on 6 Feb 2023
Thanks for your help. It seems like that your code is a circle and the part in the sqrt() is always positive. So there isn't complex number. But the Lemniscate of Bernoulli have some negative quantities inside the square root sign.

Sign in to comment.

Categories

Find more on Animation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!