MATLAB Answers

Simulate a dot moving in an ellipse

1 view (last 30 days)
Jesus De Leon
Jesus De Leon on 2 Aug 2021
Answered: darova on 3 Aug 2021
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.
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
hold off
%% 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
function Sun(s,v,color)
hold on
hold off

Accepted Answer

darova on 3 Aug 2021
Use pause()
t = linspace(0,2*pi,20);
x = 5*cos(t);
y = 3*sin(t);
h = plot(5,0,'.r');
for i = 1:20

More Answers (0)

Community Treasure Hunt

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

Start Hunting!