Animate a line in polar coordinates

13 ビュー (過去 30 日間)
ロン
ロン 2023 年 2 月 6 日
コメント済み: ロン 2023 年 2 月 6 日
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

採用された回答

Sarvesh Kale
Sarvesh Kale 2023 年 2 月 6 日
編集済み: Sarvesh Kale 2023 年 2 月 6 日
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 件のコメント
ロン
ロン 2023 年 2 月 6 日
Thank you very much. It works! The negative part in square root makes trouble a lot. But your code solve it well.

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2023 年 2 月 6 日
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 件のコメント
ロン
ロン 2023 年 2 月 6 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAnimation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by