Plots combining into one

2 次查看(过去 30 天)
Hello all,
I had an issue before in which my plots were combining due to an open hold on function. I am having this issue again but I do not know the solution. I have closed all of my open hold on functions and yet it is still happening. I have included the code below.
%Code 1
w = @(Y,K,k,n) k*Y^n/(K^n + Y^n)
v = @(Y,K,k,n) k*K^n/(K^n + Y^n);
limits = [0 5];
for i=1:5,
vt = @(Y) v(Y,1,10,2);
fplot(vt,limits,'color','r');
end
hold on
for j=1:5,
wt = @(Y) w(Y,1,10,2);
fplot(wt,limits,'color','g');
end
hold off
axis([0 5 0 10])
ylabel('dX/dt')
xlabel('Y')
legend('repressor', 'activator', 'Location', 'northeastoutside')
%Code 2
syms a b X(t) X0;
eqn = diff(X)== a - b*X;
cond = X(0) == X0;
X = dsolve(eqn,cond);
pretty(X)
syms t a b X0
mX = matlabFunction(X,'vars',[t a b X0]);
f1 = @(t) mX(t, 10, 1, 1);
f2 = @(t) mX(t, 50, 5, 1);
f3 = @(t) mX(t, 5, 1, 1);
f4 = @(t) mX(t, 1.2, 0.1, 1);
limits = [0 10];
hold on
fplot(f1, limits, 'k');
fplot(f2, limits, 'r');
fplot(f3, limits, 'b');
fplot(f4, limits, 'g');
hold off
axis([0 10 0 11])
ylabel('X')
xlabel('time')
legend('a=10, b=1', 'a=50, b=5', 'a=5, b=1', 'a=1.2, b=0.1', 'Location', 'SouthEast')

采纳的回答

Benjamin Großmann
Benjamin Großmann 2020-2-27
In line 32 of your code there is a (second) "hold on" which acts on the active axes. If you want to have two figures with one axes each, then you have to add a the "figure" command before the hold on command. You can also use one figure with two axes using subplot(2,1,1) before your first for-loop and subplot(2,1,2) before the second "hold on".
  3 个评论
Adam
Adam 2020-2-27
You should get into the habit of always keeping the handles to figure, axes, plots etc that you will be applying anything further to.
e.g.
hFig = figure; hAxes = gca;
then you can always use an explicit axes handle for instructions such as:
fplot( hAxes,... )
hold( hAxes, 'on' )
etc.
It requires a little more coding, but makes your life so much easier, knowing exactly where something will be plotted and which axes will have the 'hold' instruction applied, rather than always relying on the current axes being the one you expect it to be.
Benjamin Großmann
Benjamin Großmann 2020-2-27
You are welcome and thank you for accepting the answer. As you are new to this, I would like to add some comments to your code:
  • The comma at the end of your for statement is unneccessary and uncommon
  • Add clearvars, close all and clc at the beginning of your scripts
  • You should see the warning "Function behaves unexpectedly on array inputs" when running your script. Check your functions and add a dot before /*^ if you expect element-wise operations (I think this is the case here)
  • As Adam said, use figure and axes handles, but i do not recommend using gca, but the more explicit way:
hFig = figure('Name','YouNameIt');
hAxes = axes('Parent', hFig);
Now You can also create a figure handle array and set axes inside those figures and dont have to worry about which axes are active.
  • Everything you did to add labels, set axis limits and so on is correct, but check out the methods and properties of the axes handles, e.g. to set the xlabel you could write
hAxes.XLabel.String = 'time';
  • You can also plot explicitly inside one axes by giving the plot function your axes handle as first input argument.
plot(hAxes, x, y);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Visual Exploration 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by