For a beginning Matlab plotting project instructions are to use several commands. I used everything except for "subplot" and "hold". Can anyone tell me how I could work it into the following code?:

%Define parameters of r and functions
r=0:.1:1;
Ea=-1.436./r;
Er=(5.86.*10.^-6)./(r.^9);
En=Ea+Er;
%plot functions with given r values
plot(r,Ea,'--',r,Er,'-',r,En,':')
%set axis according to the best fit for pertinent information
axis ([0 1 -15 15]);
%Display equations on graph, modified according to in-class example
text(.20,-10,'Ea=$\displaystyle\frac{-1.436}{r}$','interpreter','LaTeX') text(.7,3,'Er=$\displaystyle\frac{5.86*10.^-6}{r.^9}$','interpreter','latex') text(.05,0,'En=$En+Ea$,','interpreter','latex')
% Create xlabel
xlabel({'Interatomic Separation,r, nm'});
% Create ylabel
ylabel({'Potential Energy, E'});
% Create title
title({'K.^+ -C.^- Ion Pair, Attractive, Repulsive, and Net Energies'});
% Create textbox
annotation('textbox',... [0.73 0.78 0.17 0.13],... 'String',{'Name','Net id:00000','Feb. 4, 2013'},... 'FitBoxToText','off');
%Create legend, specify location
hleg1=legend('Attractive Force (Ea)','Repulsive Force (Er)','Net Energy (En)'); set(hleg1,'location','SouthEast');

5 件のコメント

Amit
Amit 2014 年 2 月 5 日
what you're trying to plot with subplot? I mean which variables with what and how many subplots?
I guess that's why I'm confused. I can plot all three of my formulas just using the plot command. However as part of the project I'm supposed to use the subplot and hold commands as well. The code the way I have it gives me exactly the graph I want so could I change it to include a subplot and hold and get the same end result?
Amit
Amit 2014 年 2 月 5 日
I plotted the code you have written here. You have done a very nice job in doing that, Kudos. If I was you, i'd use this plot as it is.
Subplot will shrink the axes. However, if you really need to use the subplot, that I (or anyone else) can explain on how to use it. Is that your question?
cassie
cassie 2014 年 2 月 5 日
編集済み: cassie 2014 年 2 月 5 日
For example, could I use a subplot command and superimpose that onto the graph I did using the plot command? Or is that question only illustrating how clueless I am? I'm perfectly happy with the way it looks... but I'll lose points for not using the additional commands.
Then split it into two calls to plot() like Amit showed you. It will plot in two separate graphs/charts, instead of both on one graph.

サインインしてコメントする。

 採用された回答

Amit
Amit 2014 年 2 月 5 日
編集済み: Amit 2014 年 2 月 5 日
subplot sections a figure windows in sections For example,
subplot(3,2,1)
Suggest that the figure space is going to be distributed into 3 row and 2 columns (total 3X2 = 6) spaces. The last index (here, 1) suggests which of these spaces you're using right now.
Just to understand, try something like this:
x = 1:10; % Random functions
y1 = 5*x;
y2 = exp(x);
subplot(2,2,1);
plot(x,y1);
subplot(2,2,2);
plot(x,y2);
And see where each of the plots are plotted.
Everytime you call subplot, it is very similar to calling figure. However, figure creates a new figure window and makes that your current figure. Any thing you will plot will be plotted on that figure. Similar to that, when you call subplot, the axes of that section becomes your current axes.

4 件のコメント

I definitely have a better idea about subplot and what it does. I added subplot(2,1,1) to see what it looks like and it's pretty awful. I see what you mean about shrinking the axis. That said, where does hold come in exactly?
To demonstrate hold, Try these examples:
x = 1:10; % Random functions
y1 = 5*x;
y2 = exp(x);
Now try on:
plot(x,y1);
plot(x,y2);
And separately;
plot(x,y1);
hold on;
plot(x,y2)
Without hold, the new plot command wipes out previous plot and plots the new one. With the hold on, the new plot is plotted on the previous plot. In other words, hold on is similar to
plot(x,y1,x,y2);
It's becoming clearer. I had to turn it in but I "followed" the directions by adding
subplot(1,1,1)
hold on
above my plot command... which does nothing but doesn't throw an error. Maybe I'll get full credit for being creative. Thanks again for your help.
Amit
Amit 2014 年 2 月 5 日
編集済み: Amit 2014 年 2 月 5 日
If you really have to use hold on, you can do this:
plot(r,Ea,'--');
hold on;
plot(r,Er,'-');
plot(r,En,':');
This will plot it exactly the same.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 2 月 5 日
subplot() just tells it WHERE to plot. plot() does the actual plotting. Since you have only a single plot, I don't see any reason to use subplot().
hold on tells it not to blow away any existing plot when you plot another one - it will keep both. Since you are not calling plot() more than once, you will not need to use hold.

1 件のコメント

Thanks for the link, I was trying to figure out how to incorporate subplot and hold without destroying the look of what I already created. Not because I want to, just trying to follow general instructions for the project.

サインインしてコメントする。

質問済み:

2014 年 2 月 5 日

編集済み:

2014 年 2 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by