How to plot ellipse using contour function?

i would like to plot ellipse using contour function, since it is easier to label curves or lines than plot function , but it does not show the expected lines, the lines should be narrowed towards left and not towards right
%If_dmax is an array (1*201)
%[X,Y] = meshgrid(linspace(-1000,1000,201),linspace(-1000,1000,201));
for k1 = 1:(length(If_dmax))
[C,fContour] = contour(X,Y,sqrt((L_d.*X+L_df*If_dmax(k1)).^2+(L_q.*Y).^2),'ShowText','on', 'LineWidth', 0.5,'EdgeColor',[0 0 1]);
%fContour.LevelStep=0.05;
end

8 Comments

hi
please post a code we can test (not the case)
the code is already posted
L_d,L_q, L_df are constants you can use random numbers
If_dmax should be array which means the ellipsen are moving to the left when this is increasing
hello
why the for loop ?
at each iteration you are erasing the previous contour display
what you get ate the end is only the last iteration , like if your If_dmax was simply one single value
I am still not sure to understand what you goal is
the goal is to plot ellipsen for each value of If_dmax, therefore i used for loop
But without knowing what are the various constants, it is impossible to help you.
The fact is, the plot you show has contours of an elliptical surface. You seem to be surprised at the shape of those contours, but they are what they are, elliptical contours defined by the numbers you pass to the code.
If you want better help, you need to actually provide sufficient information for us to help you.
Abdullah
Abdullah on 13 Mar 2023
Edited: Abdullah on 13 Mar 2023
i just search a method to draw multiple ellipsen (x/a)^2 + (y/b)^2 = 1 on the same figure using contour where for each ellipse the parameter 'a' is different, which presents the mentioned 'If_dmax'
so how the code should be?
t = 0:0.01:2*pi;
a = [1;2];
b = [2;4];
x = a*cos(t);
y = b*sin(t);
plot(x.',y.')
but i wanted as in the title of post, using "contour" function

Sign in to comment.

Answers (1)

I'm a little confused. You DID draw ellipses. It seems the goal was not to draw the ellipses you drew though. Essentially, as you did it, you drew ellipses of the form:
(x/a)^2 + (y/b)^2 = R
where R varies, but a was fixed. That is how contour would work, and you were triyng to use contour to solve your prolem. I imagine you saw someone using contour to draw ellipses online somewhere. It may even have been me who showed that trick.
But from your comment, you seem to be asking how to draw ellipses where a varies in the espression
(x/a)^2 + (y/b)^2 = 1
Essentially, that will cause each ellipse to be wider or narrower in the x-direction. Possibly then, a better method is to just use polar coordinates. For example, I'll choose to vary a in that expression, where b is fixed at 1.
b = 1;
theta = linspace(0,2*pi)';
a = 0.25:0.25:2;
x = zeros(size(theta))*a;
% note that I could have done this without using a loop at all, but that
% may have been too much to understand how it works.
for i = 1:numel(a)
x(:,i) = a(i)*cos(theta);
y(:,i) = b*sin(theta);
end
plot(x,y)
legend(string(a))

4 Comments

thank you, well this way i knew already, but i wished to use Contour sinse it presents more professionally with labels and what i am looking for, i thought it might be possible with it, but it seems, it can only be done with plot
You can add labels on the lines using the text function. Nothing stops you from doing so.
Sigh. COULD you have done this using contour? Well, yes. You could probably write the entire thing with one call to contour. But possibly this is what you wanted.
a = 0.25:.25:2;
b = 1;
[X,Y] = meshgrid(linspace(-5,5,500),linspace(-2,2,500));
for ind = 1:numel(a)
[C,fContour] = contour(X,Y,sqrt((X./a(ind)).^2 + (Y/b).^2),'ShowText','on', 'LineWidth', 0.5,'EdgeColor',[0 0 1]);
hold on
fContour.LevelList=a(ind);
end
axis equal
You should see that instead of specifying the LevelStep, I told contour to plot ONLY one level for each plot.
What you had done was to overwrite the contour plot every time. Using the hold on command stops that from happening.
Thank you very much, your code helped me further
anyhow, i am still wondering why i am not able to get the expected lines.
do you find any difference between these codes of the resulted two figures?
figure(1)
a = 0.25:.25:2;
b = 0.25:.5:4;
[X,Y] = meshgrid(linspace(-5,5,500),linspace(-2,2,500));
for ind = 1:numel(a)
[C,fContour] = contour(X,Y,sqrt((X./a(ind)).^2 + (Y/b(ind)).^2),'ShowText','on', 'LineWidth', 0.5,'EdgeColor',[0 0 1]);
hold on
fContour.LevelList=a(ind);
end
axis equal
figure(2)
th = linspace(0,2*pi,201)' ;
for ind = 1:numel(a)
%xe = I_ch((k1))+a(k1).*cos(th) ;
xe = a(ind).*cos(th) ;
ye = b(ind).*sin(th) ;
h2=plot(xe, ye,'--b');
grid on;
hold on
end
i found where the bug was,
'1' was missed in the ellipse equation, so it should be like this
[C,fContour] = contour(X,Y,sqrt(((X-I_ch(ind))./a(ind)).^2 + (Y/b(ind)).^2)-1,'ShowText','on', 'LineWidth', 0.5,'EdgeColor',[0 0 1]);

Sign in to comment.

Tags

Asked:

on 7 Mar 2023

Commented:

on 15 Mar 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!