Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. clear all;
  2. close all;
  3. clc;
  4. format long;
  5.  
  6. %%%%% METODA MONTE-CARLO
  7.  
  8. f1 =@(x) x.^7 - 2.*x.^4 + 2.*x.^2 + 1;
  9.  
  10. a=0;
  11. b=1;
  12. N=1000;
  13.  
  14. x = linspace (a,b,N);
  15.  
  16. %%%%%%% METODA PODSTAWOWA
  17.  
  18.  
  19. calka_podstawowa =0;
  20.  
  21.  
  22. for(k=1:N)
  23. calka_podstawowa = calka_podstawowa + f1(x(k))/N;
  24.  
  25.  
  26. end
  27.  
  28. calka_podstawowa
  29.  
  30. %%%%%%%%% METODA ORZEŁ RESZKA
  31. y = f1(x);
  32.  
  33. c=0;
  34. for(k=1:N)
  35. punkt = rand(1,2);
  36. punkt(2) = 2*punkt(2);
  37.  
  38. if (punkt(2) <= f1(punkt))
  39. plot(x,y,punkt(1),punkt(2),'or')
  40. hold on;
  41. else
  42. plot(x,y,punkt(1),punkt(2),'og')
  43. hold on;
  44. end
  45.  
  46.  
  47. if (punkt(2) <= f1(punkt))
  48. c=c+1;
  49.  
  50. end
  51.  
  52.  
  53. end
  54.  
  55. calka_orzel_reszka = (c/N)*2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement