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
%

 채택된 답변

Walter Roberson
Walter Roberson 2017년 4월 26일

0 개 추천

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

Akhil Vasvani
Akhil Vasvani 2017년 4월 27일
편집: Akhil Vasvani 2017년 4월 27일
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
Akhil Vasvani
Akhil Vasvani 2017년 4월 27일
Thank you

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

태그

질문:

2017년 4월 26일

댓글:

2017년 4월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by