Skip to main content

Program to determine the pi value by Monte carlo method:

#Program to determine the pi value by Monte carlo method:
from math import*
import random as rn
nt=input("Enter the total no. darts:")
nt=int(nt)
ns=0.0
for i in range (nt):
 x=rn.random()
 y=rn.random()
 x1=-1+2*x
 y1=-1+2*y
 if sqrt(x1**2+y1**2)<=1:
  ns=ns+1
A=4*ns/nt
print("The value of pi is:",A)

Comments

Top 3 Popular Posts of All Time

How to Install CERN ROOT on Ubuntu: A Step-by-Step Guide

If you're involved in scientific research or data analysis, you've likely heard of CERN ROOT. Developed by CERN (the European Organization for Nuclear Research), ROOT is an essential toolkit for high-energy physics data analysis. In this guide, we'll walk you through the process of installing CERN ROOT on your Ubuntu machine.  Prerequisites Before we dive into the installation process, make sure you have the following prerequisites in place: A working Ubuntu machine (this guide is tested on Ubuntu 23.04, but it should work on other versions as well). Sudo privileges to install packages. Downloading CERN ROOT Start by downloading the latest version of CERN ROOT from the official website  or open your terminal and use the following command to download the source code: wget https://root.cern/download/root_vX.YY.Z.source.tar.gz Replace X.YY.Z with the version number you want to install (e.g., 6.22.08). Installing Dependencies CERN ROOT relies on several libraries and tools. T...

Use of TClonesArray Class

void write { TClonesArray * arr = new TClonesArray ( " TVector3 " ); TClonesArray & ar = * arr ; TFile * file = new TFile ( " file.root " , " recreate " ); TTree * tree = new TTree ( " tcl " , " tcl " ); tree -> Branch ( " array " , & arr ); TRandom2 * rand = new TRandom2 ( 1 ); for ( int i = 0 ; i < 100 ; i ++) { arr -> Clear (); for ( int j = 0 ; j < 1000 ; j ++) { double x = rand -> Rndm (); double y = rand -> Rndm (); double z = rand -> Rndm (); new ( ar [ j ]) TVector3 ( x , y , z ); } tree -> Fill (); } file -> Write (); file -> Close (); } void read () { TFile * file = new TFile ( " file.root " ); TTree * tree = ( TTree *) file -> Get ( " tcl " ); TClonesArray * arr = new ...

5a

 import numpy as np from scipy.integrate import quad import matplotlib.pyplot as plt f = lambda x: np.sin(x**2) A = [] x = np.arange(0.01, 10, 0.01) for i in x:          res = quad(f, 0, i)     A.append(res[0]) print(sum(A)) plt.plot(x[:600], A[:600],label="Fresnel Integral Si(x)") plt.grid() plt.legend() plt.show() print("From the plot, we find that the solution slowly converges to 0.5")