plot half of an ellipse in matlab

To plot half of an ellipse in MATLAB:

  1. Generate x and y coordinates for the full ellipse using the ellipse function from the MATLAB FileExchange.
main.m
[x, y] = ellipse(a,b,x0,y0,phi,linspace(0,2*pi));
50 chars
2 lines
  • a and b are the horizontal and vertical radii of the ellipse, respectively.
  • x0 and y0 are the coordinates of the center of the ellipse.
  • phi is the angle of rotation of the ellipse (in degrees).
  • linspace(0,2*pi) specifies the range of angles for which to generate points on the ellipse.
  1. Create a logical array that indicates which points on the ellipse are in the desired half. For example, to plot the lower half of the ellipse:
main.m
lower_half = y<=y0;
20 chars
2 lines
  1. Use the plot function to plot the desired half of the ellipse:
main.m
plot(x(lower_half),y(lower_half))
34 chars
2 lines

Putting it all together, we get:

main.m
[x, y] = ellipse(3, 2, 0, 0, 30, linspace(0,2*pi));
lower_half = y<=0;
plot(x(lower_half),y(lower_half))
105 chars
4 lines

This will plot the lower half of an ellipse with horizontal radius 3, vertical radius 2, centered at (0,0), and rotated 30 degrees counter-clockwise.

related categories

gistlibby LogSnag