Animate a line in polar coordinates

14 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Animation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by