Simulate a dot moving in an ellipse

4 次查看(过去 30 天)
My code asks a user to in put the coordinates of focus 1 and focus 2 of an obrital ellipse. I just need to figure out how to simulate a dot moving around the ellipse drawn.
clc
clear
x1 = input("Give me the x value of sun (focus 1): "); %check ellipse image
y1 = input("Give me the Y value of the sun(focus1): "); %check ellipse image
x2 = input("Give me the x value of focus 2: "); %check ellipse image
y2 = input("Give me the Y value of focus 2: "); %check ellipse image
hold on
hold on
drawellipse(x1,x2,y1,y2)
hold off
Sun(x1,y1,'yellow')
%% functions
function drawellipse(x1,x2,y1,y2)
% Define parameters.
%set up perihelion (closed to the sun)
Rp = sqrt ((x1)^2 + (y1)^2);
%Set up aphelion (farthest from sun)
Ra = sqrt((x2)^2 + (y2)^2);
%determine eccentricity (how flat or round the shape of the ellipse is)
%from Rp and Ra
eccentricity = (Ra-Rp)/(Ra+Rp);
numPoints = 500; % Less for a coarser ellipse, more for a finer resolution.
%the larger the number the smoother the ellipse will look
% Make equations:
% a is the distance from the center of the elipse to the (aka length of the
% semi-major axis
a = 0.5 * (Rp + Ra);
% c is the distanc from the center of the elipse to the focii (the sun's
% location is one focus)
c = eccentricity*a;
%length of the semininor axis
b = sqrt(a^2 - c^2);
%creates the empty ellipse
t = linspace(0, 2 * pi, numPoints); %from 0 radians to 2pi radians
X = a * cos(t);
Y = b * sin(t);
% Compute angles relative to (x1, y1). idk what's happening here lol
angles = atan2(y2 - y1, x2 - x1);
x = (x1 + x2) / 2 + X * cos(angles) - Y * sin(angles);
y = (y1 + y2) / 2 + X * sin(angles) + Y * cos(angles);
% Plot the ellipse as a blue curve.
subplot(1, 1, 1);
plot(x,y,'b-', 'LineWidth', 1); % Plot ellipse
grid on;
axis equal
end
function Sun(s,v,color)
hold on
color=color+".";
plot(s,v,color,'MarkerSize',75);
hold off
end

采纳的回答

darova
darova 2021-8-3
Use pause()
t = linspace(0,2*pi,20);
x = 5*cos(t);
y = 3*sin(t);
h = plot(5,0,'.r');
line(x,y)
for i = 1:20
set(h,'xdata',x(i))
set(h,'ydata',y(i))
pause(0.5)
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Earth and Planetary Science 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by