Advertisement
Guest User

Untitled

a guest
May 21st, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.20 KB | None | 0 0
  1. clear all
  2. close all
  3. %setting variables
  4. walk_num = 100;
  5. W = 0.1;
  6. N = 100000;
  7. beta = 1;
  8.  
  9. %assingment 12: plotting the potencial
  10. x = linspace(-3,3,100);
  11. figure()
  12. plot(x,V(x),'o-')
  13. set(gca,"fontname","arial","fontsize",14,"fontweight","normal");
  14. ylabel ("\it{V(x)}");
  15. xlabel ("\it{x}");
  16. grid on
  17. print("figure_4_1", "-dpng", "-r300");
  18.  
  19.  
  20. %assingment 14
  21. new_walkers = zeros(walk_num,1);
  22. old_walkers = -3 + 6*rand(walk_num,1); %initalising walkers
  23. for n = 1:N %monte carlo simulation
  24. p_old = exp(-beta.*V(old_walkers));
  25. new_walkers = old_walkers+(-W + 2.*W.*rand(walk_num,1));
  26. p_new = exp(-beta.*V(new_walkers));
  27. ratio = p_new./p_old - rand();
  28. new_walkers(ratio<0) = old_walkers(ratio<0);
  29. old_walkers = new_walkers;
  30. end
  31.  
  32.  
  33. figure()
  34. counts = histcounts(new_walkers,x);
  35. histogram('BinEdges',x,'BinCounts',counts/walk_num) %plotting numerically values
  36. hold on
  37. p = exp(-beta.*V(x))/(sum(exp(-beta.*V(x))));
  38. plot(x,p) %potting theoretical values
  39.  
  40.  
  41.  
  42. grid on
  43. set(gca,"fontname","arial","fontsize",14,"fontweight","normal");
  44. xlabel ("\it{x}");
  45. ylabel ("Probability");
  46. print("figure_4_2", "-dpng", "-r300");
  47.  
  48.  
  49.  
  50. function [v] = V(x)
  51. v=x.^2+2.*exp(-4.*(x-0.5).^2)+2.5.*exp(-4.*(x+0.5).^2);
  52. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement