Hello,
In order to make the two for loops update the same figure, the “hold on” command can be used in MATLAB. The code written below, and the changes made to ensure that each for loop updates the figure each iteration and clears the unnecessary points, while ensuring that both for loops affect the same figure. Also, I have made changes in the iteration conditions of the second for loop, or else it never executes.
figure;
for j=2:nt1+1
u(1)=getVarTempo(t1(j));
u(2:h-1)= p1*u(1:h-2) + (1- p1 - q1 )*u(2:h-1) + q1*u(3:h);
u(h)=u(h-1);
plot(u,x,'r*:',U,x,'k');
xlabel('C(z,t1)'); ylabel('z');
title(['t=',num2str(t1(j)),'seconds']);
axis([0 50 0 Z1+Z2]);
pause(0.01);
A(j) = trapz(x,u);
end
hold on
for j=nt1+1:2*nt2
u(h)=u(h-1);
u(h+1:end-1)= p2*u(h:end-2) + (1- p2 - q2 )*u(h+1:end-1) + q2*u(h+2:end);
u(end)=getNoemannNothing(t1);
plot(u,x,'r*:',U,x,'k');
xlabel('C(z,t2)'); ylabel('z');
title(['t=',num2str(t2(j-nt1)),'seconds']);
axis([0 50 0 Z1+Z2]);
pause(0.01);
A(j) = trapz(x,u);
hold off
end
The resulting graph is as follows.
The documentation on the hold command (https://www.mathworks.com/help/matlab/ref/hold.html) is useful in understanding the above code.