0% found this document useful (0 votes)
15 views

Python Unit - 5

Python programming in physics

Uploaded by

premsnp2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
15 views

Python Unit - 5

Python programming in physics

Uploaded by

premsnp2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 11
Radioactivity = Apr 10, 2021 * 1 min read Q |View on GitHub launch |binder Open in Golab import numpy as np import matplotlib.pyplot as plt import random from scipy.integrate import odeint from scipy.integrate import solve_ivp| See eee Radioactive Decay by solving ODE Rate of decay of a radioactive nuclei is dN ‘dt This is a first order differential equation which can be solved by any standard method. But, we must mention initial condition of the decay, i.e. at time t = 0 , what is number of radioactive nuclei =-\N import numpy as np import matplotlib.pyplot as plt import random from scipy.integrate import odeint from scipy.integrate import solve_ivf SSS eo Radioactive Decay by solving ODE Rate of decay of a radioactive nuclei is dN a —rAN This is a first order differential equation which can be solved by any standard method. But, we must mention initial condition of the decay, i.e. at time t — ( , what is number of radioactive nuclei N(0) present in the substance? Consider, Consider, at t=0, N(0) = No ¢ NO = 1000 T_half = 4.5*60 lamda = 1/ T_half Creating time array t_init, t_final, step_size = 0, 1000, t = np.arange(t_init, t_final, step_g ee) Model of radioactivity def model( return -la By odeint method from scipy.integrate = odeint(model, NO, t) sol[:, 0] -plot(t, N, ls='- color='red', -plot(t, NO - N, ls ar 1000 800 600 Parent nucleus --- Daughter nucleus 400 200 0 200 400 600 800 1000 By solve.ivp from scipy.integrate def model(t, N): # order of the ar return -lamda*N sol = solve_ivp(model, t_span=(t[0], sol.t 1000 800 600 400 200 Simulation of Radioactive Decay by Monte Carlo method = 4,5*60 lamda = 1/ T_half N = 1000 Xo ean) time = np.arange(0, for t in time: P.append(Np) D.append(Nd) decay = 0 for n in range(Np): Parent Nucl ae TOF 4p) 18] Ee ea # Decay const. Initial numi 0, 1) P.append(N D.append(Nd) decay = 0 for n in range(Np): pe random. random() if p < lamda: decay += 1 Np -= decay Nd += decay plt.plot(P, linestyle='-', color='red plt.plot(D, linestyle='-', color='blu plt.legen plt.xlabel('t', fontsize=14) plt.ylabel('N(t)', fontsize=14) plt.sho QE 1000 800 600 Parent Nucleus — Daughter Nucleus N(t) 400 200 r T T 0 200 400 600 800 1000 Estimating the value of Pi using Monte Carlo Monte Carlo estimation Monte Carlo methods are a broad class of computational algorithms that rely on repeated random sampling to obtain numerical results. One of the basic examples of getting started with the Monte Carlo algorithm is the estimation of Pi. Estimation of Pi The idea is to simulate random (x, y) points in a 2- D plane with domain as a square of side 2r units centered on (0,0). Imagine a circle inside the same domain with same radius r and inscribed into the square. We then calculate the ratio of number points that lied inside the circle and total number of generated points. Refer to the image below: Random points are generated only few of which lie outside the imaginary circle We know that area of the square is 4p? unit sq while that of circle is are. The ratio of these two areas is as follows : area of the circle _ ar? __ Tr area of the square — 472 — > | Now for a very large number of generated points, no. of points generated inside the circle otal no. of points generated or no. of points generated inside the square t. The beauty of this algorithm is that we don’t need any graphics or simulation to display the generated points. We simply generate random (x, y) pairs and then check if x + y? <1 yes, we increment the number of points that appears inside the circle. In randomized and simulation algorithms like Monte Carlo, the more the number of iterations, the more accurate the result is. Thus, the title is “Estimating the value of Pi” and not “Calculating the value of Pi”. Below is the algorithm for the method: Pi” and not “Calculating the value of Pi”. Below is the algorithm for the method: The Algorithm 1. Initialize circle_points, square_points and interval to 0. 2. Generate random point x. . Generate random point y. . Calculate d = x*x + y*y, . If d <= 1, increment circle_points. . Increment square_points. . Increment interval. . If increment < NO_OF_ITERATIONS, repeat from Non Aas w 9. Calculate pi = 4*(circle_points/square_points). 10. Terminate. The code doesn't wait for any input via stdin as the macro INTERVAL could be changed as per the required number of iterations. Number of iterations are the square of INTERVAL. Also, I’ve paused the screen for first 10 iterations with getch() and outputs are displayed for every iteration with format given below. You can change or delete them as per requirement. x y circle_points square_points - pi Examples: INTERVAL = 5 Output : Final Estimation of Pi = 2.56 INTERVAL = 10 Output : Final Estimation of Pi = 3.24 INTERVAL = 100 Output : Final Estimation of Pi = 3.0916 C++ Java Python C# Javascript for estimation of Pi using Monte tion */ stdce++.h> ision for x and y values. More the re the number of significant digits L 10000 std; 1, oh; -x, vand_y, origin_dist, pi; points = 0, square_points = 0; zing rand() NULL) ); Examples: TEAL = 5 utout + Final estination of Pi = 2.56 rnTERUAL = 10 futout + Final estination of PL = 3.26 INTERVAL = 100 output : Final Estinetion of Pi ~ 3.0916 cr Java Python ce /* Cee program for estimation of Pi using Monte cart simuiation */ Haneluse // Dezinee precision for x and y values. More the (o Zntervals nore the sumber of sapnsficane asgste Sgetine INTERVAL 10000 using nes pace sta: sot main t Ant sntervat, i double rend_x. randy, origin-dist. pi: Ant circle points = 0, sguare_potnte = 0: 1) Initializing rand) arena( tame( NULL!) {7 Total Rendon numbers generated + possible x 17 vatues * possabie y values for (i = 0: i < (INTERVAL © INTERVAL: i+) [ 11 Randonty generated x and y values rendx = double(rend(1 % (INTERVAL + 1) / INTERVAL, pand_y + double(rand(1 % (INTERVAL + 101 / INTERVAL: // Diotence vetween (x. y) from the ortgan, origin-dist + rand.x * randoe + randy | rand. 1/ Checking af (x. y) lies ineide the define i esrcle with Ret Af lorigin diet <= 1) // Total number of pointe generated // eotinated pi after this iteration pa + doubleis * cizeteposnts) / aquareposnte: / For visual underetanding (Optéons1) cout << rand <<" * << randy <<" * k< citele points << * * << aquare_pointe ee st ee pa ce end // Bausing extimation for first 10 velues (Optional! sf 3 < 20) wotchar(): d 1) Samah Estamated Value cout << “\nFinal Estimation of PA = *

You might also like