Animate a line in polar coordinates

18 Ansichten (letzte 30 Tage)
ロン
ロン am 6 Feb. 2023
Kommentiert: ロン am 6 Feb. 2023
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

Akzeptierte Antwort

Sarvesh Kale
Sarvesh Kale am 6 Feb. 2023
Bearbeitet: Sarvesh Kale am 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 Kommentar
ロン
ロン am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

KSSV
KSSV am 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 Kommentar
ロン
ロン am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Animation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by