MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Yuen Shin
on 2 Jun 2013

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.

*No products are associated with this question.*

Answer by the cyclist
on 2 Jun 2013

Accepted answer

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.

Yuen Shin
on 2 Jun 2013

Yup,but do you have any sites or materials which teach us in writing the Mathlab code for either the accept-rejection method or the inverse theorem. My problem is I know the algorithm but I have no ideas how to start in writing code for the Mathlab when this is the first time I am being introduced of using Mathlab to write the code.

But thanks a lots for the reply.

the cyclist
on 2 Jun 2013

There are many paths to learning MATLAB. Here is one possible starting point: http://www.mathworks.com/academia/student_center/tutorials/launchpad.html

Image Analyst
on 2 Jun 2013

Another excellent source of training: Doug Hull's video tutorials.

Answer by 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.

Answer by 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;

## 0 Comments