How to plot using the mod function?

I'm trying to plot an energy graph, and I'm having trouble plotting both the mod function. I don't understand why I cannot plot each point when mod(t,etime) == 0 and mod(t,ptime) == 0. The mod(t,etime) == 0 function only plots at the end, and I would like it to plot continuously. Could someone please help me figure this out?
function MDSim(T,sigma,m,dt,rc,L,k,ptime,etime)
tempcontrol=input('Would you like Temperature Control? [Y/N] \n','s');
while (tempcontrol ~= 'n' && tempcontrol ~= 'y')
tempcontrol=input('Would you like Temperature Control? [Y/N] \n', 's');
end
[xpos,ypos,vx,vy]=Initialize(sigma,L,k);
h=cell(8);
for i=1:64
h{i}=rectangle('Position',[xpos(i)-0.5*sigma ypos(i)-0.5*sigma sigma sigma],'Curvature',[1 1], 'FaceColor','b');
axis equal
end
t=0;
while T > t
t=t+1;
[xpos,ypos,vx,vy,U_tot,K_avg_tot,E_tot]=Step(xpos,ypos,vx,vy,dt,m,L,sigma,rc,k,tempcontrol);
if mod(t,ptime) == 0
for j=1:64
figure(1);
set(h{j},'Position',[xpos(j)-0.5*sigma ypos(j)-0.5*sigma sigma sigma])
axis equal
end
hold on
drawnow
hold off
end
if mod(t,etime) == 0
%disp(['Total Energy is: ', num2str(E_tot)])
figure(2);
plot(t,(U_tot),'rd', t,(K_avg_tot),'b+',t,(E_tot),'m*')
title('Total Energy (E), Potential Energy (U),Kinetic Energy (K) vs. Time (s)');
xlabel('Time (ds)');
ylabel('Energy (J)');
legend('E','U', 'K', 'Location', 'EastOutside');
hold on
drawnow()
hold off
end
end
end
%

 Accepted Answer

You appear to be successfully plotting at the times you would like to. You just are not asking for the plot that you think you are.
if mod(t,etime) == 0
disp(['Total Energy is: ', num2str(E_tot)])
disp(count);
plot(t, (U_tot), '*') %notice change
axis equal
%scatter(t,E_tot);
%set(ph, 'XData', t);
%set(ph,'YData', U_tot);
%,t,K_avg_tot,'-',t,E_tot,'-')
hold on %notice change
drawnow() %notice change
end

3 Comments

Akhil Vasvani
Akhil Vasvani on 27 Apr 2017
Edited: Akhil Vasvani on 27 Apr 2017
Thank you, your answers works for one part, but it cannot work for both. How do I change my code to reflect this?
After
h{i}=rectangle('Position',[xpos(i)-0.5*sigma ypos(i)-0.5*sigma sigma sigma],'Curvature',[1 1], 'FaceColor','b');
and
hold on

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB 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!