필터 지우기
필터 지우기

Animate a line in polar coordinates

조회 수: 9 (최근 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 CenterFile Exchange에서 Animation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by