Clear Filters
Clear Filters

How do I make just one plot stay in a figure, while other plots are removed/updated as I iterate through my loop

4 views (last 30 days)
I'm trying to make the plot of a potential visible in my figure while I plot the time evolution of the wave function of a particle in the potential.
V=diag(xvec.^2); %V is potential and xvec is a vector
figure(1)
hold on
plot(xvec,V);
And after this point I want "hold on" command to be turned off for the animation to make sense, but the plot of the potential V to still appear in my figure, while the loop below iterates through the time evolution.
for t=0:dt:100*dt
v=exp(-i*E(1:N)*t/hbar);
Psi=v'*ev(:,1:N)'/sqrt(N);
plot(xvec,abs(Psi).^2/dx)
axis([-0.1*a 1.1*a 0 4])
text(2.5*a,0.45,num2str(t))
pause(.2)
end

Accepted Answer

Walter Roberson
Walter Roberson on 2 Mar 2017
V=diag(xvec.^2); %V is potential and xvec is a vector
fig = figure(1);
ax = axes('Parent', fig);
hold(ax, 'on')
plot(ax, xvec, V);
for t=0:dt:100*dt
v = exp(-i*E(1:N)*t/hbar);
Psi = v'*ev(:,1:N)'/sqrt(N);
y = abs(Psi).^2/dx;
if t == 0
ph = plot(ax, xvec, y)
axis(ax, [-0.1*a 1.1*a 0 4])
th = text(ax, 2.5*a, 0.45, num2str(t));
else
set(ph, 'YData', y);
set(th, 'String, num2str(t));
end
pause(.2)
end
hold(ax, 'off')

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!