I want to plot an Ellipse. I have the verticles for the major axis: d1(0,0.8736) d2(85.8024,1.2157) (The coordinates are taken from another part of code so the ellipse must be on the first quadrant of the x-y axis) I also want to be able to change the eccentricity of the ellipse.
MATLAB: How to plot an Ellipse
ellipsegraphmathematicsplot
Related Solutions
You need to introduce a phase shift to get a rotation. See my demo code. It makes a rotated ellipse. Then it uses a second way, a rotation matrix, to rotate that ellipse by a specified angle.
% Creates a rotated ellipse, and then rotates it again by a specified angle using a second method: a rotation matrix.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
clearvars;format longg;format compact;fontSize = 20;% Parameterize the equation.
t = linspace(0, 360,1000);phaseShift = 20;xAmplitude = 2;yAmplitude = 1;x = xAmplitude * sind(t + phaseShift);y = yAmplitude * cosd(t);% Now plot the rotated ellipse.
plot(x, y, 'b-', 'LineWidth', 2);axis equalgrid on;xlabel('X', 'FontSize', fontSize);ylabel('Y', 'FontSize', fontSize);title('Rotated Ellipses', 'FontSize', fontSize);text(-1.75, 1.4, 'Parametric --', 'Color', 'b', 'FontSize', fontSize);% Now plot another ellipse and multiply it by a rotation matrix.
% https://en.wikipedia.org/wiki/Rotation_matrix
rotationAngle = 30;transformMatrix = [cosd(rotationAngle), sind(rotationAngle);... -sind(rotationAngle), cosd(rotationAngle)]xAligned = xAmplitude * sind(t);yAligned = yAmplitude * cosd(t);xyAligned = [xAligned; yAligned]';xyRotated = xyAligned * transformMatrix;xRotated = xyRotated(:, 1);yRotated = xyRotated(:, 2);hold on;plot(xRotated, yRotated, 'g-', 'LineWidth', 2);% Plot a line at 30 degrees
slope = tand(30);x1 = min(x(:));y1 = slope * x1;x2 = max(x(:));y2 = slope * x2;line([x1 x2], [y1 y2], 'Color', 'r');text(-1.75, 1.25, 'Rotation Matrix --', 'Color', 'g', 'FontSize', fontSize);text(-1.75, 1.1, '30 Degree Line --', 'Color', 'r', 'FontSize', fontSize);% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
Just after calculating x and y, you could put in these lines:
neg_y = y<0;x(neg_y) = [];y(neg_y) = [];
You might also want to use a finer time granularity, such as
time = linspace(0,10,1000); %time vector
so that you don't have a big gap when your final time point is still a ways from y==0.
Best Answer