How to write a code in generate a random number within an ellipse for Mathlab?
30 views (last 30 days)
Show older comments
Hi all,
Do you have any ideas how to write the Mathlab function ellipse(n) to generate and display n random vectors that are uniformly distributed within an ellipse function where 5x^2+21xy+25y^2=9.
This is my first time using the Mathlab and I have no idea how should I start to write the code for this.
0 Comments
Accepted Answer
the cyclist
on 2 Jun 2013
You can use the "rejection method" to do this very easily in MATLAB.
I assume this a school assignment, so I don't want to show you exactly how to do it. Here is the general approach:
1. Generate random x and y variables over a rectangular region that is bigger than the ellipse. You could use the MATLAB function rand() to do this.
2. Check to see whether the condition 5*x.^2+21*x.*y+25*y.^2 <= 9 is satisfied.
3. If it is satisfied, plot that point. [You could use the plot() command.] If not, "reject" that point and do not plot it.
4. Repeat until you have the desired number of points.
4 Comments
Image Analyst
on 2 Jun 2013
Another excellent source of training: Doug Hull's video tutorials.
saba as
on 22 Dec 2016
Please can you give me the matlab code for this method concrnant the same equation
More Answers (3)
Roger Stafford
on 2 Jun 2013
There is a method which avoids having to reject points. The idea is to rotate your coordinate system about the center of the ellipse (in this case the origin) to where the expression for the ellipse assumes its standard form (without an x*y term.) There is a commonly used technique of filling a unit circle uniformly with random points involving the square root of 'rand' numbers for the varying radius and another set of 'rand' numbers multiplied by 2*pi for the varying angle in radians. The ellipse can be filled uniformly by expanding that used for this unit circle by scale factors of the ellipse's respective major and minor semi-axes lengths. I'll just give the matlab code here. It will be a good mathematical exercise for you to see if you can fill in the details to justify it.
% Fill ellipse 5*x^2 + 21*x*y + 25*y^2 = 9 uniformly with n points
r = sqrt(rand(n,1)); % Start out as if filling a unit circle
t = 2*pi*rand(n,1);
X = 3*sqrt(2/59)*r.*cos(t); % Rescale by major & minor semi-axis lengths
Y = 3*sqrt(2)*r.*sin(t);
x = (3*X-7*Y)/sqrt(58); % Rotate back to original coordinates
y = (7*X+3*Y)/sqrt(58);
The (x,y) points will fill the ellipse in a statistically uniform manner.
Image Analyst
on 2 Jun 2013
I thought of the cyclist's method first too. Another method that you can use, if you want, is to just use rand to get points in a rectangle. Then use a mask to eliminate the ones outside the ellipse. See the FAQ for how to generate and use an elliptical mask: http://matlab.wikia.com/wiki/FAQ#How_do_I_create_an_ellipse.3F. For bonus points and maximum learning, do it both ways!
A masking demo for digital images is also in the FAQ, but basically you just create the binary image of the ellipse and set outside that to zero.
yourMatrix(~ellipseImage) = 0;
Image Analyst
on 22 Dec 2016
Since it's an old question, I'll post the solution:
% Script to draw random points inside or outside an ellipse.
% Initialization / clean-up code.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 30;
width = 9;
numberOfPoints = 50000;
% Use rand() to get points randomly located within a square.
x = width * rand(1, numberOfPoints) - width/2;
y = width * rand(1, numberOfPoints) - width/2;
% Determine which of all the points are actually inside the ellipse defined by the equation.
% e = 5 * x .^ 2 + 21*x.*y + 25*y .^ 2;
% Use >= if you want points outside the ellipse.
% Use <= if you want points inside the ellipse.
inEllipse = (5 * x .^ 2 + 21 * x .* y + 25 * y .^ 2) <= 9;
% inEllipse is a list on indexes of x and y that are inside the ellipse.
% Extract only those points that are inside.
xInEllipse = x(inEllipse);
yInEllipse = y(inEllipse);
fprintf('Min x = %f, max x = %f\n', min(xInEllipse), max(xInEllipse));
fprintf('Min y = %f, max y = %f\n', min(yInEllipse), max(yInEllipse));
% Now display our random set of points in a figure.
plot(xInEllipse, yInEllipse, '.', 'MarkerSize', 5)
% Put the axes origin at the center.
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
axis square;
grid on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
title('Random Locations Within an Ellipse', 'FontSize', fontSize);
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!