Continue to Site

# [SOLVED]ellipsis draw by matlab

Status
Not open for further replies.

#### ayham87

##### Newbie level 6
Dear friends,

I am trying to draw three ellipsis separated by 120 degree as in the below picture by matlab in the same graph
but i stuck how to complete my code, i started with one ellipse as below

Code:
xCenter = 12.5;
yCenter = 10;
theta = 0 : 0.01 : 2*pi;

x = xRadius * cos(theta) + xCenter;
y = yRadius * sin(theta) + yCenter;
plot(x, y, 'LineWidth', 3);
axis square;
xlim([0 20]);
ylim([0 20]);
grid on;

In order to generate an ellipse orientated along an axis that forms an angle φ with respect to the abscissa you have to apply the rotation of coordinates to the equation of the ellipse itself.
The equation of the ellipse is (as correctly implemented in you code):

xe=R•cos(θ) + xCenter
ye=R•sin(θ) + yCenter

the rotation is given by:

xr=xe•cos(φ)-ye•sin(φ)
yr=xe•sin(φ)+ye•cos(φ)

In order to have three ellipses 120° spaced, simply apply φ1=0°, φ2=120°, φ3=240°.

Since the axis rotation will rotate (of course) also the center, if xCenter=0 and yCenter=0 the centers af the three ellipses will be the same and equal to (0,0), so to have also a rotation of the center you will need to place it at a certain distance from the origin. To have the ellispses touching in a single point simply set to 0 the center corresponding to the minor axis of the ellipse and set the other at the same value of the radius of the major axis.

I wrote a code for Scilab. It is very similar to Matlab then with minor changes you can run it of that platform (for instance "isoview" stays for "axis square" and "%pi" stays for "pi").

xCenter = 0;
yCenter = 8;
theta = 0 : 0.01 : 2*%pi;

isoview(-20,20,-20,20);

phi=0*%pi/180;

xr = x*cos(phi)-y*sin(phi);
yr = x*sin(phi)+y*cos(phi);

plot(xr, yr, 'r');

phi=120*%pi/180;

xr = x*cos(phi)-y*sin(phi);
yr = x*sin(phi)+y*cos(phi);

plot(xr, yr, 'b');

phi=240*%pi/180;

xr = x*cos(phi)-y*sin(phi);
yr = x*sin(phi)+y*cos(phi);

plot(xr, yr, 'g');

If you need the ellipses to be intersecated, simply decrease the value of yCenter.

ayham87

### ayham87

Points: 2
Thanks Albbg

??? Error using ==> axis at 183
Unknown command option square(-20,20,-20,20)

I think because you cannot directly translate isoview(-20,20,-20,20) into axis square(-20,20,-20,20).
I'm not sure about Matlab synopsis, but it seems you have to use a similar code to your, that is:

axis square
xlim([-20 20]);
ylim([-20 20]);
grid on;

ayham87

### ayham87

Points: 2
Thanks a lot Albbg

It works with me now

last thing, i tried to shift the ellipsis plot coordinates out of (0,0) to another coordinate but i couldn't . could you tell me how can i plot on any specific coordinate?

I shifted the ellipsis by defining shifts for the plot as:

X_shift=5;
Y_shift=8;
plot(xr+X_shift, yr+X_shift, 'r');

It's correct, then you solved the problem.

Perhaps it's a little bit short using complex numbers.

Code:
xRadius = 2.5;
theta = [0 : 0.01 : 2*pi]';

R120 = cos(2*pi/3) + i*sin(2*pi/3);

plot(z*[1,R120,R120'], 'LineWidth', 3);
axis square;
xlim([-15 15]);
ylim([-15 15]);
grid on;

Thanks a lot

Do you know how can i insert numbering for the ellipses as below figure

i need only how to add the values inside each ellipse

Status
Not open for further replies.