{"id":1653,"date":"2015-12-06T18:34:54","date_gmt":"2015-12-06T17:34:54","guid":{"rendered":"http:\/\/www.valcon.it\/python\/?p=1653"},"modified":"2024-08-10T12:11:24","modified_gmt":"2024-08-10T10:11:24","slug":"integrazione-numerica-metodo-monte-carlo","status":"publish","type":"post","link":"https:\/\/www.valcon.it\/python\/integrazione-numerica-metodo-monte-carlo\/","title":{"rendered":"Integrazione numerica con metodo Monte Carlo"},"content":{"rendered":"\n<p><a href=\"https:\/\/www.valcon.it\/pp\/integrazione-numerica-monte-carlo\/\">Vedi la discussione<\/a>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport random\n\ndef f(x): return 3*x**2+2*x+1\n\na = 1\nb = 5\nfmax = 100\nAREA = (b-a)*fmax\n\npunti = 0\nPUNTI = 5000\nfor p in range(PUNTI):\n    x = random.uniform(a, b)\n    y = random.uniform(0, fmax)\n    if(y &lt;= f(x)):\n        punti += 1\n\nrapporto = punti\/PUNTI\narea     = rapporto*AREA\n\nprint(&quot;PUNTI = %d, punti = %d, rapporto = %.4f, area = %.4f&quot; %(PUNTI, punti, rapporto, area))\n<\/pre><\/div>\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/www.valcon.it\/python\/matplotlib\/\">Matplotlib<\/a><\/div>\n<\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"https:\/\/www.valcon.it\/python\/wp-content\/uploads\/integrazione-numerica-1.png\" alt=\"\" class=\"wp-image-10846\" srcset=\"https:\/\/www.valcon.it\/python\/wp-content\/uploads\/integrazione-numerica-1.png 640w, https:\/\/www.valcon.it\/python\/wp-content\/uploads\/integrazione-numerica-1-150x113.png 150w, https:\/\/www.valcon.it\/python\/wp-content\/uploads\/integrazione-numerica-1-360x270.png 360w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport random\n\ndef f(x): return 3*x*x+2*x+1\na=1\nb=5\nfmax=100\nAREA=(b-a)*fmax\npunti=0\nPUNTI=5000\n\nxp=&#x5B;] # ASCISSE CASUALI\nyp=&#x5B;] # ORDINATE CASUALI --- SOTTO IL GRAFICO\nxP=&#x5B;] # ASCISSE CASUALI\nyP=&#x5B;] # ORDINATE CASUALI --- SOPRA IL GRAFICO\n\nfor p in range(PUNTI):\n    x=random.uniform(a,b)\n    y=random.uniform(0,fmax)\n    if(y &lt;= f(x)):\n        punti += 1\n        xp.append(x) # punto sotto\n        yp.append(y) \n    else:\n        xP.append(x) # punto sopra\n        yP.append(y)\n\nrapporto=punti\/PUNTI \narea    =rapporto*AREA \n\nprint(&quot;PUNTI=%d, punti=%d, rapporto=%.4f, area=%.4f&quot; %(PUNTI,punti,rapporto,area))\n\nX=np.linspace(a-1,b+1)\nyf=&#x5B;]\nfor x in X:\n    yf.append(f(x))\n\nplt.grid(which=&quot;major&quot;)\nplt.plot(X , yf, color=&quot;blue&quot; , linewidth=&quot;2&quot;)\nplt.scatter(xp, yp, color=&quot;green&quot;, marker =&quot;.&quot;)\nplt.scatter(xP, yP, color=&quot;red&quot;, marker =&quot;.&quot;  )\nplt.title(&quot;Integrazione numerica&quot;)\n\nplt.show()\n<\/pre><\/div>\n\n\n<p>Osserva l&#8217;andamento dell&#8217;approssimazione al crescere del numero di punti<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"https:\/\/www.valcon.it\/python\/wp-content\/uploads\/integrazione-numerica-2.png\" alt=\"\" class=\"wp-image-10849\" srcset=\"https:\/\/www.valcon.it\/python\/wp-content\/uploads\/integrazione-numerica-2.png 640w, https:\/\/www.valcon.it\/python\/wp-content\/uploads\/integrazione-numerica-2-150x113.png 150w, https:\/\/www.valcon.it\/python\/wp-content\/uploads\/integrazione-numerica-2-360x270.png 360w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/figure><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport random\n\ndef f(x): return 3*x*x+2*x+1\na=1\nb=5\nfmax=100\nAREA=(b-a)*fmax\npunti=0\nPUNTI=1000\n\nxp=&#x5B;]  # ASCISSE CASUALI\nyp=&#x5B;]  # ORDINATE CASUALI --- SOTTO IL GRAFICO\nxP=&#x5B;]  # ASCISSE CASUALI\nyP=&#x5B;]  # ORDINATE CASUALI --- SOPRA IL GRAFICO\nya=&#x5B;]  # Approssimazioni area\n\nfor p in range(1,PUNTI+1):\n    x=random.uniform(a,b)\n    y=random.uniform(0,fmax)\n    if(y &lt;= f(x)):\n        punti += 1\n        xp.append(x)  # punto sotto\n        yp.append(y)        \n    else:\n        xP.append(x)  # punto sopra\n        yP.append(y)\n    area=punti\/p*AREA # approssimazione\n    ya.append(area)\n\nrapporto=punti\/PUNTI \narea    =rapporto*AREA \n\nprint(&quot;PUNTI=%d, punti=%d, rapporto=%.4f, area=%.4f&quot; %(PUNTI,punti,rapporto,area))\n\nX=np.linspace(a-1,b+1)\nyf=&#x5B;]\nfor x in X:\n    yf.append(f(x))\n\nplt.subplot(2,1,1)\nplt.grid()\nplt.plot(ya, linewidth=&quot;2&quot;)\nplt.title(&quot;Approssimazione area&quot;)\nplt.subplot(2,1,2)\nplt.grid()\nplt.plot(X , yf, color=&quot;blue&quot;, linewidth=&quot;2&quot;)\nplt.scatter(xp, yp, color=&quot;green&quot;, marker=&quot;.&quot;)\nplt.scatter(xP, yP, color=&quot;red&quot;, marker=&quot;.&quot;)\n\nplt.show()\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>Vedi la discussione. Osserva l&#8217;andamento dell&#8217;approssimazione al crescere del numero di punti<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[32,35,10],"_links":{"self":[{"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/posts\/1653"}],"collection":[{"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/comments?post=1653"}],"version-history":[{"count":65,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/posts\/1653\/revisions"}],"predecessor-version":[{"id":25406,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/posts\/1653\/revisions\/25406"}],"wp:attachment":[{"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/media?parent=1653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/categories?post=1653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.valcon.it\/python\/wp-json\/wp\/v2\/tags?post=1653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}