You are on page 1of 30

Computational - Physics (PH-707) Assignment

Dipankar Pokhrel
Roll-No.- 236121017
November 10, 2023

1 Python Code for the lab assignment using Chat-gpt:


1.1 Assignment-1:
Q) Write a code to convert any decimal number like 123.124 etc into binary.

Listing 1: Python Code to convert 123.123 into binary:


1 def d ecimal _to_binary ( decimal_number ) :
2 # Separate the integer and fractional parts
3 integer_part = int ( decimal_number )
4 fractional_part = decimal_number - integer_part if decimal_number != integer_part else
0
5

6 # Convert the integer part to binary


7 binary_integer = bin ( integer_part ) [2:]
8

9 # Convert the fractional part to binary


10 bina ry_fractional = " "
11 while fractional_part > 0:
12 fractional_part *= 2
13 bit = int ( fractional_part )
14 bina ry_fractional += str ( bit )
15 fractional_part -= bit
16

17 # Combine the integer and fractional parts


18 b i n a r y _ r epre sent ation = binary_integer
19 if bin ary_fractional :
20 b i n a ry_ repre sent atio n += " . " + binary_fractional
21

22 return b i n a r y _repr esen tatio n


23

24

25 decimal_number = 123.124
26 b i n a r y _ r e p r e sent ation = decimal_to_binary ( decimal_number )
27 print ( f " The binary representation of { decimal_number } is : { binar y_re prese ntat ion } " )

1
OUTPUT:
The binary representation of 123.124 is: 1111011.000111111011111001110110110010001011010000111

Q) Choose X values from 1 to 1000. Generate F(X) values. If it is even write it as H, and T if it is odd.

Listing 2: Python Code to generate 1000 random number using Pseudo-Random Number generator:
1 # Define LCG parameters
2 a = 1664525
3 b = 1013904223
4 c = 2**32
5

6 # Generate F ( X ) values using LCG


7 sequence_lcg = [( a * x + b ) % c for x in range (1 , 1001) ]
8

9 # Convert to ’H ’ for even and ’T ’ for odd


10 seque n ce _ l cg _ r es ult = [ ’H ’ if x % 2 == 0 else ’T ’ for x in sequence_lcg ]
11

12 # Print the sequences


13 print ( " F ( x ) Random Number Sequence : " , sequence_lcg )
14 print ( " Coin toss : " , sequence_lcg_result )

OUTPUT:
F(x) Random Number Sequence: [1015568748, 1017233273, 1018897798, 1020562323, 1022226848, 1023891373, 1025555898,
1027220423, 1028884948, 1030549473, 1032213998, 1033878523, 1035543048, 1037207573, 1038872098, 1040536623,
1042201148, 1043865673, 1045530198, 1047194723, 1048859248, 1050523773, 1052188298, 1053852823, 1055517348,
1057181873, 1058846398, 1060510923, 1062175448, 1063839973, 1065504498, 1067169023, 1068833548, 1070498073,
1072162598, 1073827123, 1075491648, 1077156173, 1078820698, 1080485223, 1082149748, 1083814273, 1085478798,
1087143323, 1088807848, 1090472373, 1092136898, 1093801423, 1095465948, 1097130473, 1098794998, 1100459523,
1102124048, 1103788573, 1105453098, 1107117623, 1108782148, 1110446673, 1112111198, 1113775723, 1115440248,
1117104773, 1118769298, 1120433823, 1122098348, 1123762873, 1125427398, 1127091923, 1128756448, 1130420973,
1132085498, 1133750023, 1135414548, 1137079073, 1138743598, 1140408123, 1142072648, 1143737173, 1145401698,
1147066223, 1148730748, 1150395273, 1152059798, 1153724323, 1155388848, 1157053373, 1158717898, 1160382423,
1162046948, 1163711473, 1165375998, 1167040523, 1168705048, 1170369573, 1172034098, 1173698623, 1175363148,
1177027673, 1178692198, 1180356723, 1182021248, 1183685773, 1185350298, 1187014823, 1188679348, 1190343873,
1192008398, 1193672923, 1195337448, 1197001973, 1198666498, 1200331023, 1201995548, 1203660073, 1205324598,
1206989123, 1208653648, 1210318173, 1211982698, 1213647223, 1215311748, 1216976273, 1218640798, 1220305323,
1221969848, 1223634373, 1225298898, 1226963423, 1228627948, 1230292473, 1231956998, 1233621523, 1235286048,
1236950573, 1238615098, 1240279623, 1241944148, 1243608673, 1245273198, 1246937723, 1248602248, 1250266773,
1251931298, 1253595823, 1255260348, 1256924873, 1258589398, 1260253923, 1261918448, 1263582973, 1265247498,
1266912023, 1268576548, 1270241073, 1271905598, 1273570123, 1275234648, 1276899173, 1278563698, 1280228223,
1281892748, 1283557273, 1285221798, 1286886323, 1288550848, 1290215373, 1291879898, 1293544423, 1295208948,
1296873473, 1298537998, 1300202523, 1301867048, 1303531573, 1305196098, 1306860623, 1308525148, 1310189673,
1311854198, 1313518723, 1315183248, 1316847773, 1318512298, 1320176823, 1321841348, 1323505873, 1325170398,
1326834923, 1328499448, 1330163973, 1331828498, 1333493023, 1335157548, 1336822073, 1338486598, 1340151123,
1341815648, 1343480173, 1345144698, 1346809223, 1348473748, 1350138273, 1351802798, 1353467323, 1355131848,
1356796373, 1358460898, 1360125423, 1361789948, 1363454473, 1365118998, 1366783523, 1368448048, 1370112573,

2
1371777098, 1373441623, 1375106148, 1376770673, 1378435198, 1380099723, 1381764248, 1383428773, 1385093298,
1386757823, 1388422348, 1390086873, 1391751398, 1393415923, 1395080448, 1396744973, 1398409498, 1400074023,
1401738548, 1403403073, 1405067598, 1406732123, 1408396648, 1410061173, 1411725698, 1413390223, 1415054748,
1416719273, 1418383798, 1420048323, 1421712848, 1423377373, 1425041898, 1426706423, 1428370948, 1430035473,
1431699998, 1433364523, 1435029048, 1436693573, 1438358098, 1440022623, 1441687148, 1443351673, 1445016198,
1446680723, 1448345248, 1450009773, 1451674298, 1453338823, 1455003348, 1456667873, 1458332398, 1459996923,
1461661448, 1463325973, 1464990498, 1466655023, 1468319548, 1469984073, 1471648598, 1473313123, 1474977648,
1476642173, 1478306698, 1479971223, 1481635748, 1483300273, 1484964798, 1486629323, 1488293848, 1489958373,
1491622898, 1493287423, 1494951948, 1496616473, 1498280998, 1499945523, 1501610048, 1503274573, 1504939098,
1506603623, 1508268148, 1509932673, 1511597198, 1513261723, 1514926248, 1516590773, 1518255298, 1519919823,
1521584348, 1523248873, 1524913398, 1526577923, 1528242448, 1529906973, 1531571498, 1533236023, 1534900548,
1536565073, 1538229598, 1539894123, 1541558648, 1543223173, 1544887698, 1546552223, 1548216748, 1549881273,
1551545798, 1553210323, 1554874848, 1556539373, 1558203898, 1559868423, 1561532948, 1563197473, 1564861998,
1566526523, 1568191048, 1569855573, 1571520098, 1573184623, 1574849148, 1576513673, 1578178198, 1579842723,
1581507248, 1583171773, 1584836298, 1586500823, 1588165348, 1589829873, 1591494398, 1593158923, 1594823448,
1596487973, 1598152498, 1599817023, 1601481548, 1603146073, 1604810598, 1606475123, 1608139648, 1609804173,
1611468698, 1613133223, 1614797748, 1616462273, 1618126798, 1619791323, 1621455848, 1623120373, 1624784898,
1626449423, 1628113948, 1629778473, 1631442998, 1633107523, 1634772048, 1636436573, 1638101098, 1639765623,
1641430148, 1643094673, 1644759198, 1646423723, 1648088248, 1649752773, 1651417298, 1653081823, 1654746348,
1656410873, 1658075398, 1659739923, 1661404448, 1663068973, 1664733498, 1666398023, 1668062548, 1669727073,
1671391598, 1673056123, 1674720648, 1676385173, 1678049698, 1679714223, 1681378748, 1683043273, 1684707798,
1686372323, 1688036848, 1689701373, 1691365898, 1693030423, 1694694948, 1696359473, 1698023998, 1699688523,
1701353048, 1703017573, 1704682098, 1706346623, 1708011148, 1709675673, 1711340198, 1713004723, 1714669248,
1716333773, 1717998298, 1719662823, 1721327348, 1722991873, 1724656398, 1726320923, 1727985448, 1729649973,
1731314498, 1732979023, 1734643548, 1736308073, 1737972598, 1739637123, 1741301648, 1742966173, 1744630698,
1746295223, 1747959748, 1749624273, 1751288798, 1752953323, 1754617848, 1756282373, 1757946898, 1759611423,
1761275948, 1762940473, 1764604998, 1766269523, 1767934048, 1769598573, 1771263098, 1772927623, 1774592148,
1776256673, 1777921198, 1779585723, 1781250248, 1782914773, 1784579298, 1786243823, 1787908348, 1789572873,
1791237398, 1792901923, 1794566448, 1796230973, 1797895498, 1799560023, 1801224548, 1802889073, 1804553598,
1806218123, 1807882648, 1809547173, 1811211698, 1812876223, 1814540748, 1816205273, 1817869798, 1819534323,
1821198848, 1822863373, 1824527898, 1826192423, 1827856948, 1829521473, 1831185998, 1832850523, 1834515048,
1836179573, 1837844098, 1839508623, 1841173148, 1842837673, 1844502198, 1846166723, 1847831248, 1849495773,
1851160298, 1852824823, 1854489348, 1856153873, 1857818398, 1859482923, 1861147448, 1862811973, 1864476498,
1866141023, 1867805548, 1869470073, 1871134598, 1872799123, 1874463648, 1876128173, 1877792698, 1879457223,
1881121748, 1882786273, 1884450798, 1886115323, 1887779848, 1889444373, 1891108898, 1892773423, 1894437948,
1896102473, 1897766998, 1899431523, 1901096048, 1902760573, 1904425098, 1906089623, 1907754148, 1909418673,
1911083198, 1912747723, 1914412248, 1916076773, 1917741298, 1919405823, 1921070348, 1922734873, 1924399398,
1926063923, 1927728448, 1929392973, 1931057498, 1932722023, 1934386548, 1936051073, 1937715598, 1939380123,
1941044648, 1942709173, 1944373698, 1946038223, 1947702748, 1949367273, 1951031798, 1952696323, 1954360848,
1956025373, 1957689898, 1959354423, 1961018948, 1962683473, 1964347998, 1966012523, 1967677048, 1969341573,
1971006098, 1972670623, 1974335148, 1975999673, 1977664198, 1979328723, 1980993248, 1982657773, 1984322298,
1985986823, 1987651348, 1989315873, 1990980398, 1992644923, 1994309448, 1995973973, 1997638498, 1999303023,
2000967548, 2002632073, 2004296598, 2005961123, 2007625648, 2009290173, 2010954698, 2012619223, 2014283748,
2015948273, 2017612798, 2019277323, 2020941848, 2022606373, 2024270898, 2025935423, 2027599948, 2029264473,
2030928998, 2032593523, 2034258048, 2035922573, 2037587098, 2039251623, 2040916148, 2042580673, 2044245198,
2045909723, 2047574248, 2049238773, 2050903298, 2052567823, 2054232348, 2055896873, 2057561398, 2059225923,
2060890448, 2062554973, 2064219498, 2065884023, 2067548548, 2069213073, 2070877598, 2072542123, 2074206648,
2075871173, 2077535698, 2079200223, 2080864748, 2082529273, 2084193798, 2085858323, 2087522848, 2089187373,

3
2090851898, 2092516423, 2094180948, 2095845473, 2097509998, 2099174523, 2100839048, 2102503573, 2104168098,
2105832623, 2107497148, 2109161673, 2110826198, 2112490723, 2114155248, 2115819773, 2117484298, 2119148823,
2120813348, 2122477873, 2124142398, 2125806923, 2127471448, 2129135973, 2130800498, 2132465023, 2134129548,
2135794073, 2137458598, 2139123123, 2140787648, 2142452173, 2144116698, 2145781223, 2147445748, 2149110273,
2150774798, 2152439323, 2154103848, 2155768373, 2157432898, 2159097423, 2160761948, 2162426473, 2164090998,
2165755523, 2167420048, 2169084573, 2170749098, 2172413623, 2174078148, 2175742673, 2177407198, 2179071723,
2180736248, 2182400773, 2184065298, 2185729823, 2187394348, 2189058873, 2190723398, 2192387923, 2194052448,
2195716973, 2197381498, 2199046023, 2200710548, 2202375073, 2204039598, 2205704123, 2207368648, 2209033173,
2210697698, 2212362223, 2214026748, 2215691273, 2217355798, 2219020323, 2220684848, 2222349373, 2224013898,
2225678423, 2227342948, 2229007473, 2230671998, 2232336523, 2234001048, 2235665573, 2237330098, 2238994623,
2240659148, 2242323673, 2243988198, 2245652723, 2247317248, 2248981773, 2250646298, 2252310823, 2253975348,
2255639873, 2257304398, 2258968923, 2260633448, 2262297973, 2263962498, 2265627023, 2267291548, 2268956073,
2270620598, 2272285123, 2273949648, 2275614173, 2277278698, 2278943223, 2280607748, 2282272273, 2283936798,
2285601323, 2287265848, 2288930373, 2290594898, 2292259423, 2293923948, 2295588473, 2297252998, 2298917523,
2300582048, 2302246573, 2303911098, 2305575623, 2307240148, 2308904673, 2310569198, 2312233723, 2313898248,
2315562773, 2317227298, 2318891823, 2320556348, 2322220873, 2323885398, 2325549923, 2327214448, 2328878973,
2330543498, 2332208023, 2333872548, 2335537073, 2337201598, 2338866123, 2340530648, 2342195173, 2343859698,
2345524223, 2347188748, 2348853273, 2350517798, 2352182323, 2353846848, 2355511373, 2357175898, 2358840423,
2360504948, 2362169473, 2363833998, 2365498523, 2367163048, 2368827573, 2370492098, 2372156623, 2373821148,
2375485673, 2377150198, 2378814723, 2380479248, 2382143773, 2383808298, 2385472823, 2387137348, 2388801873,
2390466398, 2392130923, 2393795448, 2395459973, 2397124498, 2398789023, 2400453548, 2402118073, 2403782598,
2405447123, 2407111648, 2408776173, 2410440698, 2412105223, 2413769748, 2415434273, 2417098798, 2418763323,
2420427848, 2422092373, 2423756898, 2425421423, 2427085948, 2428750473, 2430414998, 2432079523, 2433744048,
2435408573, 2437073098, 2438737623, 2440402148, 2442066673, 2443731198, 2445395723, 2447060248, 2448724773,
2450389298, 2452053823, 2453718348, 2455382873, 2457047398, 2458711923, 2460376448, 2462040973, 2463705498,
2465370023, 2467034548, 2468699073, 2470363598, 2472028123, 2473692648, 2475357173, 2477021698, 2478686223,
2480350748, 2482015273, 2483679798, 2485344323, 2487008848, 2488673373, 2490337898, 2492002423, 2493666948,
2495331473, 2496995998, 2498660523, 2500325048, 2501989573, 2503654098, 2505318623, 2506983148, 2508647673,
2510312198, 2511976723, 2513641248, 2515305773, 2516970298, 2518634823, 2520299348, 2521963873, 2523628398,
2525292923, 2526957448, 2528621973, 2530286498, 2531951023, 2533615548, 2535280073, 2536944598, 2538609123,
2540273648, 2541938173, 2543602698, 2545267223, 2546931748, 2548596273, 2550260798, 2551925323, 2553589848,
2555254373, 2556918898, 2558583423, 2560247948, 2561912473, 2563576998, 2565241523, 2566906048, 2568570573,
2570235098, 2571899623, 2573564148, 2575228673, 2576893198, 2578557723, 2580222248, 2581886773, 2583551298,
2585215823, 2586880348, 2588544873, 2590209398, 2591873923, 2593538448, 2595202973, 2596867498, 2598532023,
2600196548, 2601861073, 2603525598, 2605190123, 2606854648, 2608519173, 2610183698, 2611848223, 2613512748,
2615177273, 2616841798, 2618506323, 2620170848, 2621835373, 2623499898, 2625164423, 2626828948, 2628493473,
2630157998, 2631822523, 2633487048, 2635151573, 2636816098, 2638480623, 2640145148, 2641809673, 2643474198,
2645138723, 2646803248, 2648467773, 2650132298, 2651796823, 2653461348, 2655125873, 2656790398, 2658454923,
2660119448, 2661783973, 2663448498, 2665113023, 2666777548, 2668442073, 2670106598, 2671771123, 2673435648,
2675100173, 2676764698, 2678429223]
Coin toss: [’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,

4
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’,
’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’, ’H’, ’T’]

Q) Run the decimal to binary first and test with 12.63 to binary.

Listing 3: Python Code to convert 12.63 into binary:


1 def d ecimal _to_binary ( decimal_number ) :
2 # Check if the number is negative
3 is_negative = False
4 if decimal_number < 0:
5 is_negative = True
6 decimal_number = abs ( decimal_number )
7

8 # Convert integer part to binary


9 integer_part = int ( decimal_number )
10 binary_integer = " "
11 while integer_part > 0:
12 binary_integer = str ( integer_part % 2) + binary_integer
13 integer_part //= 2
14

15 # If the number is a float , convert the fractional part to binary


16 fractional_part = decimal_number - int ( decimal_number )

5
17 bin ary_fr actional = " "
18 precision = 8 # Adjust precision as needed
19 while fractional_part > 0 and len ( binary_fractional ) < precision :
20 fractional_part *= 2
21 bit = int ( fractional_part )
22 bina ry_fractional += str ( bit )
23 fractional_part -= bit
24

25 # Combine integer and fractional parts


26 b i n a r y _ r e p r e sent ation = binary_integer
27 if bin ary_fr actional :
28 b i n a r y _ r epre sent ation += " . " + binary_fractional
29

30 # Add negative sign if applicable


31 if is_negative :
32 b i n a r y _ r epre sent ation = " -" + bin ary_r epre sent ation
33

34 return b i n a r y _repr esen tatio n


35

36 # Test with 12.63


37 decimal_number = 12.63
38 bin a r y _ r e p r e s e nt atio n = decimal_to_binary ( decimal_number )
39 print ( f " The binary representation of { decimal_number } is : { binar y_re prese ntat ion } " )

OUTPUT :
The binary representation of 12.63 is: 1100.10100001

Q) Generate pairs of 10000 random real numbers using the Mod function inside a square of size 2 with center at the origin
corresponding to points on a plane. Count how many of these points fall inside the circle of radius 1 with center at the
same origin. From this find the value of pi.

Listing 4: Python Code to estimate pi :


1 import matplotlib . pyplot as plt
2

3 # Linear Congruential Generator ( LCG ) parameters


4 a = 1664525
5 b = 1013904223
6 m = 2**32
7 seed = 42 # Initial seed
8

9 # Function to generate random points inside a square using LCG


10 def g en e r at e _ po i nts_lcg ( num_points ) :
11 p o i n t s _ i n s i d e_circle = 0
12 p o i n t s _ o u t s i de _c i rc le _ x = []
13 p o i n t s _ o u t s i de _c i rc le _ y = []
14

15 # LCG initialization
16 X = seed

6
17

18 for _ in range ( num_points ) :


19 # Generate random numbers using LCG
20 X = (a * X + b) % m
21 x = ( X / m ) * 2 - 1 # Scale to the range [ -1 , 1]
22

23 X = (a * X + b) % m
24 y = (X / m) * 2 - 1 # Scale to the range [ -1 , 1]
25

26 # Check if the point is inside the circle


27 if x **2 + y **2 <= 1:
28 p o i n ts_inside_circle += 1
29 else :
30 p o i n t s _o u ts id e _c i rc le _ x . append ( x )
31 p o i n t s _o u ts id e _c i rc le _ y . append ( y )
32

33 return points_inside_circle , points_outside_circle_x , p o in t s_ ou t si d e_ ci r cl e _y


34

35 # Number of random points


36 num_points = 10000
37

38 # Generate points and count those inside the circle using LCG
39 points_inside_circle , outside_x , outside_y = generate_points_lcg ( num_points )
40

41 # Estimate the value of pi


42 estimated_pi = ( points_inside_circle / num_points ) * 4
43

44 print ( f " Estimated value of pi : { estimated_pi } " )


45

46 # Plotting
47 plt . figure ( figsize =(8 , 8) )
48

49 # Plot points inside the circle in blue


50 plt . scatter ( outside_x , outside_y , color = ’ blue ’ , label = ’ Outside Circle ’)
51

52 # Plot points inside the circle in red


53 plt . scatter ([ -1 , 1 , 1 , -1 , -1] , [ -1 , -1 , 1 , 1 , -1] , color = ’ black ’ , label = ’ Bounding Square ’ ,
linestyle = ’ dashed ’)
54

55 # Set aspect ratio to be equal


56 plt . gca () . set_aspect ( ’ equal ’ , adjustable = ’ box ’)
57

58 # Add circle
59 circle = plt . Circle ((0 , 0) , 1 , edgecolor = ’ red ’ , facecolor = ’ none ’ , linestyle = ’ dashed ’ ,
linewidth =2)
60 plt . gca () . add_patch ( circle )
61

62 plt . title ( f ’ Monte Carlo Simulation for Estimating pi using LCG = { estimated_pi } ’)
63 plt . legend ()
64 plt . show ()
OUTPUT:
Estimated value of pi: 3.1184

7
8
Q) Write a robust code in some language to implement LU decomposition of an invertible square matrix. Write another
program that calls this to solve a linear system of equations.

Listing 5: Python Code for LU decomposition :


1 import numpy as np
2

3 def lu_decomposition ( matrix ) :


4 n = len ( matrix )
5 L = np . eye (n , dtype = float )
6 U = matrix . copy () . astype ( float ) # Ensure U is of floating - point type
7 P = np . eye (n , dtype = float )
8

9 for k in range ( n - 1) :
10 pivot_index = np . argmax ( np . abs ( U [ k : , k ]) ) + k
11 U [[ k , pivot_index ] , :] = U [[ pivot_index , k ] , :]
12 P [[ k , pivot_index ] , :] = P [[ pivot_index , k ] , :]
13

14 for i in range ( k + 1 , n ) :
15 factor = U [i , k ] / U [k , k ]
16 L [i , k ] = factor
17 U [i , k :] -= factor * U [k , k :]
18

19 return L , U , P
20

21 def print_matrices (L , U ) :
22 print ( " Lower Triangular Matrix ( L ) : " )
23 print ( L )
24 print ( " \ nUpper Triangular Matrix ( U ) : " )
25 print ( U )
26

27 def s ol v e _l i n ea r _system (A , b ) :
28 L , U , P = lu_decomposition ( A )
29

30 print_matrices (L , U )
31

32 y = np . linalg . solve (L , np . dot (P , b ) )


33 x = np . linalg . solve (U , y )
34

35 return x
36

37 # Example usage :
38 A = np . array ([[2 , -1 , 1] ,
39 [ -3 , -1 , 4] ,
40 [ -1 , 1 , 3]])
41

42 b = np . array ([8 , -7 , -1])


43

44 solution = s o l ve _linear_system (A , b )
45 print ( " \ nSolution : " , solution )

9
OUTPUT :
Lower Triangular Matrix (L):  
1 0 0
−0.66666667 1 0
0.33333333 −0.8 1
Upper Triangular Matrix (U):  
−3 −1 4
0 −1.66666667 3.66666667
0 0 4.6
Solution: [ 3.52173913 -0.08695652 0.86956522]

Q) Write M = M0 + V where M0 is a diagonal matrix and solve Mx =b iteratively.

Listing 6: Python Code to solve linear equation iteratively :


1 import numpy as np
2

3 def jacobi_iteration (M , b , x0 = None , max_iterations =100 , tol =1 e -6) :


4 n = len ( b )
5 if x0 is None :
6 x0 = np . zeros ( n )
7

8 D = np . diag ( np . diag ( M ) )
9 R = M - D
10

11 for iteration in range ( max_iterations ) :


12 x1 = np . linalg . solve (D , b - np . dot (R , x0 ) )
13 if np . linalg . norm ( x1 - x0 ) < tol :
14 return x1
15 x0 = x1
16

17 raise ValueError ( " Jacobi method did not converge within the specified number of
iterations . " )
18

19 def g a u s s _ s e i d e l _i te rat io n (M , b , x0 = None , max_iterations =100 , tol =1 e -6) :


20 n = len ( b )
21 if x0 is None :
22 x0 = np . zeros ( n )
23

24 for iteration in range ( max_iterations ) :


25 for i in range ( n ) :
26 x0 [ i ] = ( b [ i ] - np . dot ( M [i , : i ] , x0 [: i ]) - np . dot ( M [i , i +1:] , x0 [ i +1:]) ) / M [i , i ]
27

28 if np . linalg . norm ( M @ x0 - b ) < tol :


29 return x0
30

31 raise ValueError ( " Gauss - Seidel method did not converge within the specified number of
iterations . " )
32

33 # Example usage :

10
34 M0 = np . diag ([2 , 3 , 4]) # Example diagonal matrix
35 V = np . array ([[1 , -1 , 0] , [ -1 , 2 , -1] , [0 , -1 , 3]]) # Example off - diagonal part
36 M = M0 + V # Construct M
37 b = np . array ([1 , 2 , 3]) # Example right - hand side vector
38

39 # Solve using Jacobi method


40 solution_jacobi = jacobi_iteration (M , b )
41 print ( " Solution using Jacobi method : " , solution_jacobi )
42

43 # Solve using Gauss - Seidel method


44 sol ut i o n _ g a u s s _ s eidel = g aus s_ se ide l_ it era ti on (M , b )
45 print ( " Solution using Gauss - Seidel method : " , solut ion_ gauss _sei del )

OUTPUT:
Solution using Jacobi method: [0.53684195 0.61052616 0.51578941]
Solution using Gauss-Seidel method: [0.53684207 0.61052631 0.51578947]

Q) Implement RK-4 for solving y’(x)=sin[y(x)/x], where y[0.01]=1. Find y[0.01¡x¡2].

Listing 7: Python code for solving ODE using Runge-Kutta 4th order
1

2 import matplotlib . pyplot as plt


3 import numpy as np
4 def f (x , y ) :
5 return np . sin ( y / x )
6 x0 = 0.01
7 y0 = 1
8 x = float ( input ( " Enter the value of final value of x : " ) )
9 n = 1000
10 h = (x - x0 ) / n
11 y_rk = []
12 x_rk = []
13 def rk ( x0 , y0 ) :
14 for _ in range ( n ) :
15

16 k1 = h * f ( x0 , y0 )
17 k2 = h * f ( x0 + h /2 , y0 + k1 /2)
18 k3 = h * f ( x0 + h /2 , y0 + k2 /2)
19 k4 = h * f ( x0 + h , y0 + k3 )
20

21 k = ( k1 + 2*( k2 + k3 ) + k4 ) /6
22

23 y1 = y0 + k
24 x0 = x0 + h
25 y0 = y1
26 x_rk . append ( x0 )
27 y_rk . append ( y0 )
28

11
29 return y1
30 print ( f " The value of y at x = { x } is { round ( rk ( x0 , y0 ) ,4) } " )
OUTPUT

1. Enter the value of the final value of x: 0.1


The value of y at x = 0.1 is 0.9933

2. Enter the value of final value of x: 0.5


The value of y at x = 0.5 is 1.0602
3. Enter the value of final value of x: 0.75
The value of y at x = 0.75 is 1.2961
4. Enter the value of final value of x: 1
The value of y at x = 1.0 is 1.5449
5. Enter the value of final value of x: 2
The value of y at x = 2.0 is 2.5237

Q) Solve Laplace equation inside a square of side 1 by breaking it up into n2 small squares of equal size. Find a way to
write it as a linear system. Try it for n = 3,4,5 first to get an idea how to write it as a matrix equation. The boundary
condition is the top edge of the square is at potential equal to 1 volt and other edges are grounded.

Listing 8: Python Code to Laplace Equation


1 import numpy as np
2

4 # Defining grid parameters


5 nx = int ( input ( " Enter the nunmber of grids in x - direction : "))
6 ny = int ( input ( " Enter the nunmber of grids in y - direction : "))
7 Lx = 1.0 # Length in the x - direction
8 Ly = 1.0 # Length in the y - direction
9

10

11 # Creating the grid


12 x = np . linspace (0 , Lx , nx )
13 y = np . linspace (0 , Ly , ny )
14 X , Y = np . meshgrid (x , y )
15

16

17 # Set boundary conditions


18 u = np . zeros (( ny , nx ) )
19 u [: , 0] = 0 # u (x , 0) = 0
20 u [0 , :] = 0 # u (0 , y ) = 0
21 u [: , -1] = 0 # u (a , y ) = 0
22 u [0 , :] = 1 # u (x , b ) = 1
23

24 # convergence criteria
25 max_iterations = 1000

12
26 tolerance = 1e -4
27

28 # iterative loop to solve Laplace ’s equation


29 for iteration in range ( max_iterations ) :
30 u_new = u . copy ()
31 for i in range (1 , nx - 1) :
32 for j in range (1 , ny - 1) :
33 u_new [j , i ] = 0.25 * ( u [j , i + 1] + u [j , i - 1] + u [ j + 1 , i ] + u [ j - 1 , i ])
34 max_diff = np . max ( np . abs ( u_new - u ) )
35 u = u_new
36 if max_diff < tolerance :
37 break
38

39 # printing the solution


40 print ( " Solution of Laplace ’s Equation : " )
41 print ( u )

13
OUTPUT

The output shows a matrix with those calculated unknown values in place respectively

Enter the nunmber of grids in x-direction: 3


Enter the nunmber of grids in y-direction: 3
Solution of Laplace’s Equation:

1 1 1
0 0.25 0
0 0 0

Enter the nunmber of grids in x-direction: 4


Enter the nunmber of grids in y-direction: 4
Solution of Laplace’s Equation:

1 1 1 1
0 0.37493896 0.37493896 0
0 0.12493896 0.12493896 0
0 0 0 0

Enter the nunmber of grids in x-direction: 5


Enter the nunmber of grids in y-direction: 5
Solution of Laplace’s Equation:

1. 1. 1. 1. 1.
0. 0.42847988 0.52666364 0.42847988 0.
0. 0.18737793 0.24981689 0.18737793 0.
0. 0.07133702 0.09809222 0.07133702 0.
0. 0. 0. 0. 0.

14
Figure 1: For 4×4 Figure 2: For 5×5

Figure 3: For 10×10 Figure 4: For 50×50

15
Q) In today’s lab, verify law of large numbers which says that if you add large (strictly speaking infinitely) many random
variables of the same type, the resulting random number will not be random but will take a value proportional to the mean.
Verify this for 100 fair dice. Even though x1, x2 etc. are random, x1+x2 +..... + x100 = 350 (nearly always)

Listing 9: Python Code for 100 dice roll


1

2 import random as rn
3 import matplotlib . pyplot as plt
4

5 numbe r _ o f _ e x p e r i ment = 1000


6 number_dice = 100
7

8 sample_mean = []
9 sum_of_dice = []
10 dice_rolls = [ rn . randint (1 ,6) for _ in range ( number_dice ) ]
11

12 single_roll_mean = (1 + 2 + 3 + 4 + 5 + 6) /6
13

14 sample_means = sum ( dice_rolls ) / number_dice


15

16 for _ in range ( n umber_of_experiment ) :


17 d_rolls = [ rn . randint (1 ,6) for _ in range ( number_dice ) ]
18 s_mean = sum ( d_rolls ) / number_dice
19 sample_mean . append ( s_mean )
20 sum_of_dice . append ( sum ( d_rolls ) )
21

22 standa rd _d ev ia ti on = ( sum (( x - sample_means ) **2 for x in sample_mean ) / ( number_of_e x p e r i m e n t


- 1) ) **0.5
23 print ( f " Mean of single roll { single_roll_mean } " )
24 print ( f " Mean of the 100 roll = { sample_means } " )
25 print ( f " Sum of 100 dice roll = { sum ( dice_rolls ) } " )
26 print ( f " The standard deviation is { standard_deviation } " )
27

28 fig , ax = plt . subplots ( figsize =(10 ,8) )


29 ax . hist ( sample_mean , bins = 30 , color = ’ pink ’ , alpha = 0.7)
30 ax . axvline ( x = 3.5 , color = ’ blue ’ , linestyle = ’ -- ’ , label = ’ Expected Mean (3.5) ’ , linewidth
= 2 )
31

32 inset_axes = ax . inset_axes ([0.8 , 0.8 , 0.2 , 0.2])


33

34 inset_axes . hist ( sum_of_dice , bins = 30 , color = ’ blue ’ , alpha = 0.7)


35 inset_axes . axvline ( x = 350 , color = ’ red ’ , linestyle = ’ -- ’ , label = ’ Expected Sum (350) ’ ,
linewidth = 2)
36 ax . set_xlabel ( ’ Sample Mean ’)
37 ax . set_ylabel ( ’ Frequency ’)
38 ax . set_title ( f ’ Distribution of Sample Means (100 Dice Rolls , { number_of_experiment }
Experiments ) ’)
39 inset_axes . set_xlabel ( ’ Sum ’)
40 inset_axes . set_ylabel ( ’ Frequency ’)
41 ax . legend ( loc = ’ lower right ’)
42

43 plt . show ()

16
Output
Mean of single roll 3.5
Mean of the 100 roll = 3.58
Sum of 100 dice roll = 358
The standard deviation is 0.19551067598579988

17
Q) On the computer toss a fair six sided die 100 times. Find the chi2 value. Repeat the above process 1000 times and draw
a bar chart for the possible values of chi2 .

Listing 10: Python Code to find Chi-Square Value


1 import random as rn
2

3 import matplotlib . pyplot as plt


4

5 import numpy as np
6

7 import random
8

9 def dice_roll ( num_rolls = 100) :


10 obs_counts = [0] * 6
11 exp_counts = [ num_rolls / 6] * 6
12

13 for _ in range ( num_rolls ) :


14 roll = random . randint (1 , 6)
15 obs_counts [ roll - 1] += 1
16

17 chi_squared = sum ((( observed - expected ) ** 2) / expected for observed , expected in


zip ( obs_counts , exp_counts ) )
18 return chi_squared
19

20 print ( f " The Chi - squared value : { dice_roll (100) } " )


21 chi_square = [ dice_roll () for _ in range (1000) ]
22

23 plt . hist ( chi_square , bins = 20 , edgecolor = ’r ’)


24 plt . xlabel ( ’Chi - square values ’)
25 plt . ylabel ( ’ Frequency ’)
26 plt . title ( ’ Distribution of Chi - Square values for 1000 simulation ’)

18
Output:
The Chi-squared value: 4.16

19
P100
Q) Try to simulate the 1D Ising model H = − i (si · si+1 + si ) for 100 spins on a circle. Calculate the average
magnetization at various temperatures T = 1.0, T = 0.01, T = 0.01, T = 2.0. Using Metropolis algorithm. s(j) = +1 or -1
P100
H = i (−si · si+1 - si · si−1 + si · si+2 + si · si−2 + h·si )

1.1.1 Hamiltonian of 1st kind


P100
H=− i (si · si+1 + si )

Here, J = −1 and h = −1

Listing 11: Python Code to Simulate 1D - Ising Model


1 import numpy as np
2 import matplotlib . pyplot as plt
3

4 # Function to calculate the energy of the Ising model


5 def energy_ising_1d ( configuration ) :
6 num_spins = len ( configuration )
7 energy = 0.0
8 for i in range ( num_spins ) :
9 spini = configuration [ i ]
10 ip1 = ( i + 1) % num_spins
11 spinip1 = configuration [ ip1 ]
12 energy = energy - spini * spinip1 - spini
13 return energy
14

15 # Function to calculate the magnetization


16 def magnetization ( configuration ) :
17 return np . abs ( np . sum ( configuration ) ) / len ( configuration )
18

19 def metropolis_mc ( n_steps , n_lattice_sites , beta , debug = False , save_freq =10) :


20 configuration = 2 * np . random . randint (2 , size = n_lattice_sites ) - 1
21 beta = 1 / temperature
22 average_spins = []
23

24 if debug :
25 print ( " Starting configuration : " , configuration )
26

27 current_energy = energy_ising_1d ( configuration )


28 for i in range ( n_steps ) :
29 spin_to_change = np . random . randint ( n_lattice_sites )
30 configuration [ spin_to_change ] *= -1
31 energy_flip = energy_ising_1d ( configuration )
32 r = np . random . random ()
33

34 if r < min (1 , np . exp ( - beta * ( energy_flip - current_energy ) ) ) :


35 current_energy = energy_flip
36 else :
37 configuration [ spin_to_change ] *= -1
38

20
39 average_spin = configuration . mean ()
40 if i % save_freq == 0:
41 average_spins . append ( average_spin )
42

43 if debug and i % 10 == 0:
44 print ( " % i : " % i , configuration , " Energy : " , current_energy , " Spin : " , average_spin )
45

46 return average_spins , configuration


47

48 # Parameters
49 num_spins = 100
50 num_steps = 1000
51 temperatures = [1.0 , 0.1 , 0.01 , 2.0]
52

53 # Create subplots for each temperature


54 fig , axs = plt . subplots ( len ( temperatures ) , 2 , figsize =(8 , 12) )
55

56 for i , temperature in enumerate ( temperatures ) :


57 average_spins , final_configuration = metropolis_mc ( n_steps = num_steps ,
n_lattice_sites = num_spins , beta =1.0 / temperature )
58 axs [i , 0]. plot ( range (0 , num_steps , 10) , average_spins )
59 axs [i , 0]. set_title ( f ’ Temperature : { temperature } ’)
60 axs [i , 0]. set_xlabel ( ’ Time Steps ’)
61 axs [i , 0]. set_ylabel ( ’ Average Spin ’)
62

63 axs [i , 1]. imshow ( np . expand_dims ( final_configuration , axis =0) , cmap = ’ binary ’)


64 axs [i , 1]. set_title ( f ’ Final Configuration : { temperature } ’)
65

66 final_mag = magnetization ( final_configuration )


67 print ( f " Temperature : { temperature } " )
68 print ( f " Final Average Spin : { average_spins [ -1]} " )
69 print ( f " Final Magnetization : { final_mag }\ n " )
70

71 plt . tight_layout ()
72 plt . show ()

OUTPUT
Temperature: 1.0
Final Average Spin: 1.0
Final Magnetization: 1.0
Temperature: 0.1
Final Average Spin: 1.0
Final Magnetization: 1.0
Temperature: 0.01
Final Average Spin: 1.0
Final Magnetization: 1.0
Temperature: 2.0
Final Average Spin: 0.76
Final Magnetization: 0.74

21
22
1.1.2 Hamiltonian of second kind
P100
H = i (−si · si+1 - si · si−1 + si · si+2 + si · si−2 + h·si )

Here, we take h = 1

Listing 12: Python Code to Simulate 1D - Ising Model


1 import numpy as np
2 import matplotlib . pyplot as plt
3

4 # Function to calculate the energy of the Ising model


5 def energy_ising_1d ( configuration ) :
6 num_spins = len ( configuration )
7 energy = 0.0
8 for i in range ( num_spins ) :
9 spini = configuration [ i ]
10 ip_1 = ( i - 1) % num_spins
11 ip1 = ( i + 1) % num_spins
12 ip2 = ( i + 2) % num_spins
13 ip_2 = ( i - 2) % num_spins
14 spinip2 = configuration [ ip2 ]
15 spinip_2 = configuration [ ip_2 ]
16 spinip1 = configuration [ ip1 ]
17 spinip_1 = configuration [ ip_1 ]
18 energy = energy - spini * spinip1 - spinip_1 * spini + spinip2 * spini + spini *
spinip_2 - spini
19 return energy
20

21 # Function to calculate the magnetization


22 def magnetization ( configuration ) :
23 return np . abs ( np . sum ( configuration ) ) / len ( configuration )
24

25 def metropolis_mc ( n_steps , n_lattice_sites , beta , debug = False , save_freq =10) :


26 configuration = 2 * np . random . randint (2 , size = n_lattice_sites ) - 1
27 beta = 1 / temperature
28 average_spins = []
29

30 if debug :
31 print ( " Starting configuration : " , configuration )
32

33 current_energy = energy_ising_1d ( configuration )


34 for i in range ( n_steps ) :
35 spin_to_change = np . random . randint ( n_lattice_sites )
36 configuration [ spin_to_change ] *= -1
37 energy_flip = energy_ising_1d ( configuration )
38 r = np . random . random ()
39

40 if r < min (1 , np . exp ( - beta * ( energy_flip - current_energy ) ) ) :


41 current_energy = energy_flip
42 else :

23
43 configuration [ spin_to_change ] *= -1
44

45 average_spin = configuration . mean ()


46 if i % save_freq == 0:
47 average_spins . append ( average_spin )
48

49 if debug and i % 10 == 0:
50 print ( " % i : " % i , configuration , " Energy : " , current_energy , " Spin : " , average_spin )
51

52 return average_spins , configuration


53

54 # Parameters
55 num_spins = 100
56 num_steps = 1000
57 temperatures = [1.0 , 0.1 , 0.01 , 2.0]
58

59 # Create subplots for each temperature


60 fig , axs = plt . subplots ( len ( temperatures ) , 2 , figsize =(8 , 12) )
61

62 for i , temperature in enumerate ( temperatures ) :


63 average_spins , final_configuration = metropolis_mc ( n_steps = num_steps ,
n_lattice_sites = num_spins , beta =1.0 / temperature )
64 axs [i , 0]. plot ( range (0 , num_steps , 10) , average_spins )
65 axs [i , 0]. set_title ( f ’ Temperature : { temperature } ’)
66 axs [i , 0]. set_xlabel ( ’ Time Steps ’)
67 axs [i , 0]. set_ylabel ( ’ Average Spin ’)
68

69 axs [i , 1]. imshow ( np . expand_dims ( final_configuration , axis =0) , cmap = ’ binary ’)


70 axs [i , 1]. set_title ( f ’ Final Configuration : { temperature } ’)
71

72 final_mag = magnetization ( final_configuration )


73 print ( f " Temperature : { temperature } " )
74 print ( f " Final Average Spin : { average_spins [ -1]} " )
75 print ( f " Final Magnetization : { final_mag }\ n " )
76

77 plt . tight_layout ()
78 plt . show ()

OUTPUT
Temperature: 1.0
Final Average Spin: 0.1
Final Magnetization: 0.1
Temperature: 0.1
Final Average Spin: 0.36
Final Magnetization: 0.36
Temperature: 0.01
Final Average Spin: 0.48
Final Magnetization: 0.48
Temperature: 2.0
Final Average Spin: 0.2
Final Magnetization: 0.18

24
25
Q) Make chatGPT write a program to draw a RC circuit diagram with R and C in series with a battery of 1 volt.

LaTeX Code
\{ d o c u m e n t c l a s s { s t a n d a l o n e }
\ usepackage { c i r c u i t i k z }
\ b e g i n { document }

\ begin { c i r c u i t i k z }
\draw ( 0 , 0 ) t o [ V, v=1V] ( 0 , 2 )
t o [ R, l =$R$ ] ( 2 , 2 )
t o [ C, l =$C$ ] ( 2 , 0 )
−− ( 0 ,0);
\end{ c i r c u i t i k z }

\end{ document }

RC Circuit Diagram
R

1V C

26
Q) Ask chatGPT to give you seismic yield of some underground nuclear explosion as a function of time. Then ask it to
write a program to do FFT on it. Then run it.

Python Code

Listing 13: Python Code for FFT of seismic yield of underground nuclear explosion:
1 import numpy as np
2 import matplotlib . pyplot as plt
3

4 def g e n e r a t e _ c o m p l e x _ s e i s m i c _ d a t a ( duration , dt , magnitudes , frequencies , event_time ) :


5 times = np . arange (0 , duration , dt )
6 seismic_data = np . sum ([ amp * np . sin (2 * np . pi * freq * times ) for amp , freq in
zip ( magnitudes , frequencies ) ] , axis =0)
7 return times , seismic_data
8

9 def perform_fft ( signal , sampling_rate ) :


10 n = len ( signal )
11 frequencies = np . fft . fftfreq (n , d =1/ sampling_rate )
12 fft_result = np . fft . fft ( signal )
13 ma gn it ud e_ sp ectrum = np . abs ( fft_result ) / n
14 return frequencies , magnitude_spectrum
15

16 # Parameters for synthetic seismic data


17 duration = 60 # seconds
18 dt = 0.01 # seconds
19 event_time = 10.0 # seconds ( time of the event )
20

21 # Multiple frequency components for the synthetic seismic data


22 magnitudes = [1.0 , 0.5 , 0.2] # amplitudes of the sine waves
23 frequencies = [1.0 , 5.0 , 10.0] # frequencies of the sine waves
24

25 # Generate synthetic seismic data


26 times , synthetic_data = g e n e r a t e _ c o m p l e x _ s e i s m i c _ d a t a ( duration , dt , magnitudes , frequencies ,
event_time )
27

28 # Perform FFT
29 frequencies , mag nitude_spectrum = perform_fft ( synthetic_data , 1 / dt )
30

31 # Plot the results


32 plt . figure ( figsize =(12 , 6) )
33

34 plt . subplot (2 , 1 , 1)
35 plt . plot ( times , synthetic_data )
36 plt . title ( ’ Complex Synthetic Seismic Data ’)
37 plt . xlabel ( ’ Time ( s ) ’)
38 plt . ylabel ( ’ Amplitude ’)
39

40 plt . subplot (2 , 1 , 2)
41 plt . plot ( frequencies , magnitude_spectrum )
42 plt . title ( ’ FFT Result ’)

27
43 plt . xlabel ( ’ Frequency ( Hz ) ’)
44 plt . ylabel ( ’ Magnitude ’)
45

46 plt . tight_layout ()
47 plt . show ()

FFT Result Image

28
Q) Ask ChatGPT to solve some JEE Advanced Maths question. I think it will not succeed. Then ask it to write code to
answer that question. Run the code and see if the answer is correct.

Question 1
If θ is an acute angle such that tan θ = 34 , and ϕ is an acute angle such that cos ϕ = 5
13 , calculate the value of sin(θ + ϕ).
Python Code:

Listing 14: Python Code for question 1 :


1 import math
2

3 # Given values
4 tan_theta = 3/4
5 cos_phi = 5/13
6

7 # Calculate sin_theta using the Pythagorean identity


8 sin_theta = math . sqrt (1 - tan_theta **2)
9

10 # Calculate sin_phi using the Pythagorean identity


11 sin_phi = math . sqrt (1 - cos_phi **2)
12

13 # Calculate sin ( theta + phi ) using the sum - to - product formula


14 sin_th et a_ pl us _p hi = sin_theta * cos_phi + cos_phi * sin_theta
15

16 # Display the result


17 print ( f " sin ( theta + phi ) : { sin_theta_pl us_phi :.4 f } " )

Output:
sin(θ + ϕ) : 0.5088

Question 2
Find the roots of z 4 + 1 = 0.
Python Code:

Listing 15: Python Code for question 2:


1 import numpy as np
2

3 # Coefficients of the equation z ^4 + 1 = 0


4 coefficients = [1 , 0 , 0 , 0 , 1]
5

6 # Find the roots of the equation


7 roots = np . roots ( coefficients )
8

9 # Display the roots


10 for root in roots :
11 print ( f " Root : { root . real :.4 f } + { root . imag :.4 f } i " )
Output:

29
Root: -0.7071 + 0.7071i
Root: -0.7071 - 0.7071i
Root: 0.7071 + 0.7071i
Root: 0.7071 - 0.7071i

Question 3
If sin θ = 53 and cos ϕ = 45 , find the value of tan(θ − ϕ).
Python Code:

Listing 16: Python Code for question 3:


1 import math
2

3 # Given values
4 sin_theta = 3/5
5 cos_phi = 4/5
6

7 # Calculate cos ( theta ) using the Pythagorean identity


8 cos_theta = math . sqrt (1 - sin_theta **2)
9

10 # Calculate tan ( theta - phi )


11 tan_t h et a _ mi n u s_ phi = ( sin_theta - ( - cos_phi ) ) / ( cos_theta + cos_phi )
12

13 # Display the result


14 print ( f " tan ( theta - phi ) : { tan_theta_minus_phi :.4 f } " )

Output:
tan(θ − ϕ) : 0.8750

30

You might also like