How to plot the five ellipses in the same figure?

Here is my code, where when xi_a takes a new value from xi for each iteration, the semi-axes of my ellipse changes.
Now my question is how to plot all the 5 ellipses in the same figure, as it will be easy for me to find by how much they differ from each other.
The two important lines in the coding is made bold ie., xi_a and figure(4)plot

 Accepted Answer

Hi Priya,
‘. . . plot all the five ellipse generated in 5 iterations in one single figure.’
I don’t see where you get a and b. It must be over the horizon in a part of your code you didn’t post.
The idea in this code snippet here is to create vectors out of a and b as you go through your calculations in a loop, then create a second loop to do the plots.
This is what I would do:
for k1 = 1:5 % LOOP THAT GENERATES ‘a’, ‘b’
% . . . OTHER STATEMENTS IN YOUR CODE . . .
a(k1) = randi(5); % STATEMENT THAT CREATES ‘a’
b(k1) = randi(5); % STATEMENT THAT CREATES ‘b’
% . . . OTHER STATEMENTS IN YOUR CODE . . .
end
%===GENERATE THE ELLIPSE=======
xCenter = 0;
yCenter = 0;
xRadius = a;
yRadius = b;
theta = 0 : 0.01 : 2*pi;
for k1 = 1:length(a)
x = xRadius(k1) .* cos(theta) + xCenter;
y = yRadius(k1) .* sin(theta) + yCenter;
figure(4);
plot(x, y, 'LineWidth', 1)
axis square;
axis equal;
grid on;
xlabel('Longitudinal');ylabel('Lateral');title('Contact ellipse shape')
hold on
end
This plots five ellipses on the same axes using the vector elements of a and b.

10 Comments

Priya
Priya on 20 Jun 2014
Edited: Priya on 20 Jun 2014
Thanks very much. I'm yet to try your suggestion. But before that, I have included the other bits of my code for calculating 'a' and 'b'.
Now, do you feel like changing the code you provided or is it still the same?
My pleasure!
Your ‘m_selected’ and ‘m_selected’ already seem to be vectors, so ‘a’ and ‘b’ should be as well. My code and loop for generating the ellipses remains unchanged.
I did not run your code, so this is predicated on ‘a’ and ‘b’ being real. I note that their calculation involves taking the cube root of some value, and I don’t know what that value is. If the value is positive, you should not have problems.
It should work with the loop if all the values are real.
Yes you are. Look at what you're doing in the first loop. Change it to this:
for k1 = 1:5
a(k1) = m_selected*(3*pi*N*(K_1+K_2)/(4*K_3))^(1/3);
b(k1) = n_selected*(3*pi*N*(K_1+K_2)/(4*K_3))^(1/3);
end
but there is still a problem because the right side of the equations does not change with the index k1 so all your a's will be the same and all your b's will be the same. Time for you to think more about what is going on.
@Priya — You don’t need the loop to generate ‘a’ and ‘b’ with your code. I created the loop because I did not have vectors for ‘a’ and ‘b’, and you said they were 5-element vectors. I simply created test data.
So calculate ‘a’ and ‘b’ the same way you always have without the loop, and use the loop only in plotting the figure.
Priya
Priya on 20 Jun 2014
Edited: Priya on 20 Jun 2014
Sorry I'm afraid not. The idea is every time xi_a takes a new value, the ellipse size changes. So it's like a and b are affected by xi_a.
So has it got to do anything with xi_a? I mean do I need to create a loop somewhere around xi_a.
You probably need to include ‘xi_a’ in the loop. All ‘xi_a’ does is choose an element of ‘xi’ and then uses that value to interpolate to find ‘yi’.
So I would define the loop as:
for k1 = 1:5
xi_a=xi(randi(5));
% . . .
% ---------- OTHER CODE ----------
% . . .
a(k1,:) = m_selected*(3*pi*N*(K_1+K_2)/(4*K_3))^(1/3);
b(k1,:) = n_selected*(3*pi*N*(K_1+K_2)/(4*K_3))^(1/3);
end
%===GENERATE THE ELLIPSE SHAPE============================================
I’m lost as to what you are doing. Your m_selected and n_selected are already vectors of length(p), and they will change with xi_a as you’ve written your code.
This revision in my suggested code creates matrices out of a and b, so you may have to create a second loop around figure(4) to plot everything. I’m not certain you’re going to be able to get much out of the resulting plot, though. It will have too much information on it.
Siggestion — It might also be easier to loop through your xi vector rather than selecting random elements from it, since a second choice of the same element is going to re-create previous data, at the expense of values of xi that aren’t selected and may be interesting.
To get non-repeating values in each iteration, use the randperm function to generate the subscripts:
xi=[-0.02 -0.01 0 0.01 0.02];
xi_a = xi(randperm(length(xi), 3));
That should do what you want.
Since you’re only plotting three at a time, simply add the color indicator name to the plot statement:
plot(x, y, 'r') % Plot using red line
I’ve long since lost track of what your code does, so change the color to whatever you want for each ellipse. See the documentation on plot for details.
If you want to plot the first one in blue, the second in green and the third in red and you want to index them to the loop index (I call it k1 here), put this statement before your plot commands:
cvct = ['b' 'g' 'r'];
then in your plot loop:
for k1 = 1:3
plot(x(k1,:), y(k1,:), cvct(k1))
...
end
Could you please tell me how to give different colours for them ?
I thought I did. What doesn’t work about that code?

Sign in to comment.

More Answers (1)

Just don't call figure() all those times. In fact, don't call it at all. It will automatically create a figure with one axes, and all plots after you call "hold on" will be added onto the same axes control.

5 Comments

Priya
Priya on 19 Jun 2014
Edited: Priya on 19 Jun 2014
Thanks for your reply. Can you please tell me where do I need to call 'hold on' and where do I need to call off figure()
But I want figures 1,2,3 to remain as such. Just in fig4 I need all the five ellipse.
After figure #4 is generated, you only call plot() once. So why do you expect there to be 5 ellipses on figure #4? Change the ellipse parameters and call plot() 4 more times.
Priya
Priya on 19 Jun 2014
Edited: Priya on 19 Jun 2014
Sorry, it's still not clear to me. what do you mean by the ellipse parameters here?
Like xRadius, yCenter, etc.
Thanks for your help as well.

Sign in to comment.

Products

Tags

Asked:

on 19 Jun 2014

Edited:

on 6 Aug 2014

Community Treasure Hunt

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

Start Hunting!