import numpy as np
import scipy as sp
import sympy as smp
import matplotlib.pyplot as plt
from scipy.integrate import quad
from scipy.integrate import cumulative_trapezoid
from scipy.integrate import simpson
from scipy.integrate import trapezoid
You know the formula explicitly
$$f(x) = ... $$In this case there are two options
We can try sympy at first to see if the integral can be solved analytically
Example: Find $\int \sin^3(x) e^{-5x} dx$
x = smp.symbols('x', real=True)
f = smp.sin(x)**3 * smp.exp(-5*x)
smp.integrate(f, x)
smp.integrate(f, x).simplify()
Example: Find $\int \cos(bx)e^{-ax} dx$
a, b = smp.symbols('a b', real=True, positive=True)
f = smp.cos(b*x)* smp.exp(-a*x)
smp.integrate(f, x)
smp.integrate(f, x).simplify()
Example: Find $\int \frac{(1+\sqrt{x})^{1/3}}{\sqrt{x}} dx$
f = (1+smp.sqrt(x))**smp.Rational(1,3) / smp.sqrt(x)
smp.integrate(f, x)
smp.integrate(f, x).simplify()
Example (Definite) Find $\int_{0}^{\ln(4)}\frac{e^x}{\sqrt{e^{2x}+9}} dx$
f = smp.exp(x) / smp.sqrt(smp.exp(2*x) + 9)
smp.integrate(f, (x, 0, smp.log(4)))
Example (Improper) Find $ \int_{0}^{\infty} \frac{16 \tan^{-1}(x)}{1+x^2} dx $
f = 16*smp.atan(x) / (1+x**2)
smp.integrate(f, (x, 0, smp.oo))
Example: Find $\int_{1}^{2} e^{-\sin(x)} dx$
Won't run
#f = smp.exp(-smp.sin(x))
#smp.integrate(f, (x, 1, 2)).simplify()
So we use scipy's quad function to integrate numerically
f = lambda x: np.exp(-np.sin(x))
f(3)
0.8683850922340686
f(4)
2.1314499915144016
f = lambda x: np.exp(-np.sin(x))
quad(f, 1, 2)
(0.3845918142796868, 4.2698268729567035e-15)
quad(f, 1, 2)[0]
0.3845918142796868
quad(f, 1, 2)[1]
4.2698268729567035e-15
Example: Find $\int_{0}^{2\pi} \frac{1}{(a-\cos(x))^2 + (b-\sin(x))^2} dx$
#f = 1/((a-smp.cos(x))**2 + (b-smp.sin(x))**2)
#smp.integrate(f, (x, 0, 2*smp.pi)).simplify()
This won't run as this does not have an analytical solution. We must solve it numerically, and thus we need choose values of $a$ and $b$ each time
f = lambda x: 1/((a-np.cos(x))**2 + (b-np.sin(x))**2)
a, b = 2, 3
quad(f, 0, 2*np.pi)
(0.5235987755982989, 1.9168823883823662e-13)
quad(f, 0, 2*np.pi)[0]
0.5235987755982989
quad(f, 0, 2*np.pi)[1]
1.9168823883823662e-13
We can solve this for many different values of $a$ and $b$ quickly as follows:
def f(x, a, b):
return 1/((a-np.cos(x))**2 + (b-np.sin(x))**2)
np.arange(2,10,1)
array([2, 3, 4, 5, 6, 7, 8, 9])
np.arange(2,10,1)
array([2, 3, 4, 5, 6, 7, 8, 9])
a_array = np.arange(2,10,1)
b_array = np.arange(2,10,1)
integrals = [[a, b, quad(f, 0, 2*np.pi, args=(a,b))[0]] for a in a_array for b in b_array]
integrals
[[2, 2, 0.8975979010256552], [2, 3, 0.5235987755982989], [2, 4, 0.3306939635357684], [2, 5, 0.22439947525641385], [2, 6, 0.16110731556870733], [2, 7, 0.1208304866765305], [2, 8, 0.09377888518178487], [2, 9, 0.07479982508547126], [3, 2, 0.5235987755982988], [3, 3, 0.36959913571644665], [3, 4, 0.26179938779914935], [3, 5, 0.19039955476301776], [3, 6, 0.14279966607226333], [3, 7, 0.11023132117858925], [3, 8, 0.08726646259971647], [3, 9, 0.07059758772111896], [4, 2, 0.3306939635357676], [4, 3, 0.26179938779914946], [4, 4, 0.2026833970057931], [4, 5, 0.15707963267948966], [4, 6, 0.1231997119054821], [4, 7, 0.09817477042468103], [4, 8, 0.07953399123010797], [4, 9, 0.06544984694977965], [5, 2, 0.2243994752564138], [5, 3, 0.19039955476301776], [5, 4, 0.15707963267948966], [5, 5, 0.12822827157509362], [5, 6, 0.10471975511965978], [5, 7, 0.08607103160519981], [5, 8, 0.07139983303613168], [5, 9, 0.05983986006837441], [6, 2, 0.16110731556870733], [6, 3, 0.1427996660722633], [6, 4, 0.1231997119054821], [6, 5, 0.10471975511965978], [6, 6, 0.08849556770675474], [6, 7, 0.07479982508547126], [6, 8, 0.06346651825433926], [6, 9, 0.05416539057913403], [7, 2, 0.12083048667653049], [7, 3, 0.11023132117858922], [7, 4, 0.09817477042467314], [7, 5, 0.08607103160519707], [7, 6, 0.07479982508547127], [7, 7, 0.06477510625958338], [7, 8, 0.05609986881410472], [7, 9, 0.04870686284635363], [8, 2, 0.09377888518178562], [8, 3, 0.0872664625997153], [8, 4, 0.07953399123011913], [8, 5, 0.07139983303613058], [8, 6, 0.06346651825433966], [8, 7, 0.05609986881410436], [8, 8, 0.04947390005653283], [8, 9, 0.04363323129985851], [9, 2, 0.07479982508547135], [9, 3, 0.07059758772111849], [9, 4, 0.06544984694978664], [9, 5, 0.059839860068376605], [9, 6, 0.05416539057913443], [9, 7, 0.0487068628463537], [9, 8, 0.04363323129985854], [9, 9, 0.03902599569676779]]
np.array(integrals)
array([[2. , 2. , 0.8975979 ],
[2. , 3. , 0.52359878],
[2. , 4. , 0.33069396],
[2. , 5. , 0.22439948],
[2. , 6. , 0.16110732],
[2. , 7. , 0.12083049],
[2. , 8. , 0.09377889],
[2. , 9. , 0.07479983],
[3. , 2. , 0.52359878],
[3. , 3. , 0.36959914],
[3. , 4. , 0.26179939],
[3. , 5. , 0.19039955],
[3. , 6. , 0.14279967],
[3. , 7. , 0.11023132],
[3. , 8. , 0.08726646],
[3. , 9. , 0.07059759],
[4. , 2. , 0.33069396],
[4. , 3. , 0.26179939],
[4. , 4. , 0.2026834 ],
[4. , 5. , 0.15707963],
[4. , 6. , 0.12319971],
[4. , 7. , 0.09817477],
[4. , 8. , 0.07953399],
[4. , 9. , 0.06544985],
[5. , 2. , 0.22439948],
[5. , 3. , 0.19039955],
[5. , 4. , 0.15707963],
[5. , 5. , 0.12822827],
[5. , 6. , 0.10471976],
[5. , 7. , 0.08607103],
[5. , 8. , 0.07139983],
[5. , 9. , 0.05983986],
[6. , 2. , 0.16110732],
[6. , 3. , 0.14279967],
[6. , 4. , 0.12319971],
[6. , 5. , 0.10471976],
[6. , 6. , 0.08849557],
[6. , 7. , 0.07479983],
[6. , 8. , 0.06346652],
[6. , 9. , 0.05416539],
[7. , 2. , 0.12083049],
[7. , 3. , 0.11023132],
[7. , 4. , 0.09817477],
[7. , 5. , 0.08607103],
[7. , 6. , 0.07479983],
[7. , 7. , 0.06477511],
[7. , 8. , 0.05609987],
[7. , 9. , 0.04870686],
[8. , 2. , 0.09377889],
[8. , 3. , 0.08726646],
[8. , 4. , 0.07953399],
[8. , 5. , 0.07139983],
[8. , 6. , 0.06346652],
[8. , 7. , 0.05609987],
[8. , 8. , 0.0494739 ],
[8. , 9. , 0.04363323],
[9. , 2. , 0.07479983],
[9. , 3. , 0.07059759],
[9. , 4. , 0.06544985],
[9. , 5. , 0.05983986],
[9. , 6. , 0.05416539],
[9. , 7. , 0.04870686],
[9. , 8. , 0.04363323],
[9. , 9. , 0.039026 ]])
np.array(integrals).T
array([[2. , 2. , 2. , 2. , 2. ,
2. , 2. , 2. , 3. , 3. ,
3. , 3. , 3. , 3. , 3. ,
3. , 4. , 4. , 4. , 4. ,
4. , 4. , 4. , 4. , 5. ,
5. , 5. , 5. , 5. , 5. ,
5. , 5. , 6. , 6. , 6. ,
6. , 6. , 6. , 6. , 6. ,
7. , 7. , 7. , 7. , 7. ,
7. , 7. , 7. , 8. , 8. ,
8. , 8. , 8. , 8. , 8. ,
8. , 9. , 9. , 9. , 9. ,
9. , 9. , 9. , 9. ],
[2. , 3. , 4. , 5. , 6. ,
7. , 8. , 9. , 2. , 3. ,
4. , 5. , 6. , 7. , 8. ,
9. , 2. , 3. , 4. , 5. ,
6. , 7. , 8. , 9. , 2. ,
3. , 4. , 5. , 6. , 7. ,
8. , 9. , 2. , 3. , 4. ,
5. , 6. , 7. , 8. , 9. ,
2. , 3. , 4. , 5. , 6. ,
7. , 8. , 9. , 2. , 3. ,
4. , 5. , 6. , 7. , 8. ,
9. , 2. , 3. , 4. , 5. ,
6. , 7. , 8. , 9. ],
[0.8975979 , 0.52359878, 0.33069396, 0.22439948, 0.16110732,
0.12083049, 0.09377889, 0.07479983, 0.52359878, 0.36959914,
0.26179939, 0.19039955, 0.14279967, 0.11023132, 0.08726646,
0.07059759, 0.33069396, 0.26179939, 0.2026834 , 0.15707963,
0.12319971, 0.09817477, 0.07953399, 0.06544985, 0.22439948,
0.19039955, 0.15707963, 0.12822827, 0.10471976, 0.08607103,
0.07139983, 0.05983986, 0.16110732, 0.14279967, 0.12319971,
0.10471976, 0.08849557, 0.07479983, 0.06346652, 0.05416539,
0.12083049, 0.11023132, 0.09817477, 0.08607103, 0.07479983,
0.06477511, 0.05609987, 0.04870686, 0.09377889, 0.08726646,
0.07953399, 0.07139983, 0.06346652, 0.05609987, 0.0494739 ,
0.04363323, 0.07479983, 0.07059759, 0.06544985, 0.05983986,
0.05416539, 0.04870686, 0.04363323, 0.039026 ]])
np.array(integrals).T[2]
array([0.8975979 , 0.52359878, 0.33069396, 0.22439948, 0.16110732,
0.12083049, 0.09377889, 0.07479983, 0.52359878, 0.36959914,
0.26179939, 0.19039955, 0.14279967, 0.11023132, 0.08726646,
0.07059759, 0.33069396, 0.26179939, 0.2026834 , 0.15707963,
0.12319971, 0.09817477, 0.07953399, 0.06544985, 0.22439948,
0.19039955, 0.15707963, 0.12822827, 0.10471976, 0.08607103,
0.07139983, 0.05983986, 0.16110732, 0.14279967, 0.12319971,
0.10471976, 0.08849557, 0.07479983, 0.06346652, 0.05416539,
0.12083049, 0.11023132, 0.09817477, 0.08607103, 0.07479983,
0.06477511, 0.05609987, 0.04870686, 0.09377889, 0.08726646,
0.07953399, 0.07139983, 0.06346652, 0.05609987, 0.0494739 ,
0.04363323, 0.07479983, 0.07059759, 0.06544985, 0.05983986,
0.05416539, 0.04870686, 0.04363323, 0.039026 ])
This is the case where one collects actual data and wants to find the integral
Example: Electric Pulses in particle physics experiments
x, y = np.loadtxt('data/sample_data3.txt')
Suppose this is current measured $I(t)$
plt.plot(x,y)
plt.xlabel('Time [ns]')
plt.ylabel('Current [mA]')
plt.savefig('example.png', dpi=200)
plt.show()
Now in some detector experiments, energy is proportional to integrated current so $\text{Energy} \propto \int I(t) dt$
integral = cumulative_trapezoid(y,x, initial=0)
integral
array([ 0. , -0.00176695, 0.0008127 , 0.00601943, 0.0049406 ,
0.03261586, 0.08927747, 0.14125721, 0.18823767, 0.23036449,
0.26693012, 0.29793018, 0.32891457, 0.3605713 , 0.38680793,
0.40590865, 0.42667898, 0.44506353, 0.46028223, 0.47660099,
0.48799089, 0.49902016, 0.51494855, 0.52859645, 0.53809169,
0.54479271, 0.54704817, 0.54870332, 0.55313515, 0.55817944,
0.56245236, 0.57000178, 0.57437946, 0.57326111, 0.57345225,
0.57404133, 0.57492252, 0.57884983, 0.58408142, 0.58738379,
0.58991912, 0.59097402, 0.59235079, 0.59085611, 0.5907692 ,
0.59312098, 0.5923518 , 0.59013349, 0.59129188, 0.59489687,
0.5977389 , 0.60247073, 0.60366721, 0.60276028, 0.60363803,
0.60719329, 0.61383137, 0.61665511, 0.61582941, 0.61521554,
0.6147472 , 0.6201559 , 0.62535482, 0.62372424, 0.62347486,
0.62594928, 0.627414 , 0.62819617, 0.63030706, 0.63086531,
0.62969903, 0.62692508, 0.62555741, 0.6296145 , 0.6323031 ,
0.63463096, 0.63444614, 0.63470338, 0.63664231, 0.63558181,
0.63445577, 0.62987094, 0.62649363, 0.62845506, 0.63262047,
0.63568507, 0.63724706, 0.63926206, 0.63942849, 0.63338463,
0.63037725, 0.63240539, 0.63058617, 0.6283304 , 0.62814672,
0.62838251, 0.62808153, 0.62937937, 0.63067378, 0.63267269])
x
array([ 0. , 0.1010101 , 0.2020202 , 0.3030303 , 0.4040404 ,
0.50505051, 0.60606061, 0.70707071, 0.80808081, 0.90909091,
1.01010101, 1.11111111, 1.21212121, 1.31313131, 1.41414141,
1.51515152, 1.61616162, 1.71717172, 1.81818182, 1.91919192,
2.02020202, 2.12121212, 2.22222222, 2.32323232, 2.42424242,
2.52525253, 2.62626263, 2.72727273, 2.82828283, 2.92929293,
3.03030303, 3.13131313, 3.23232323, 3.33333333, 3.43434343,
3.53535354, 3.63636364, 3.73737374, 3.83838384, 3.93939394,
4.04040404, 4.14141414, 4.24242424, 4.34343434, 4.44444444,
4.54545455, 4.64646465, 4.74747475, 4.84848485, 4.94949495,
5.05050505, 5.15151515, 5.25252525, 5.35353535, 5.45454545,
5.55555556, 5.65656566, 5.75757576, 5.85858586, 5.95959596,
6.06060606, 6.16161616, 6.26262626, 6.36363636, 6.46464646,
6.56565657, 6.66666667, 6.76767677, 6.86868687, 6.96969697,
7.07070707, 7.17171717, 7.27272727, 7.37373737, 7.47474747,
7.57575758, 7.67676768, 7.77777778, 7.87878788, 7.97979798,
8.08080808, 8.18181818, 8.28282828, 8.38383838, 8.48484848,
8.58585859, 8.68686869, 8.78787879, 8.88888889, 8.98989899,
9.09090909, 9.19191919, 9.29292929, 9.39393939, 9.49494949,
9.5959596 , 9.6969697 , 9.7979798 , 9.8989899 , 10. ])
plt.plot(x, integral)
plt.xlabel('Time [ns]')
plt.ylabel('Integrated Current [pC]')
plt.show()
simpson(y,x)
0.6322578749682304
np.shape(integral)
(100,)
integral[99]
0.6326726856709545
trapezoid(y,x)
0.6326726856709549
Example: Covid Data
x, y = np.loadtxt('data/coviddata.txt')
Plot cases per day
plt.plot(x,y)
plt.xlabel('Day')
plt.ylabel('Cases per Day')
plt.show()
Can find the cumulative number of cases by taking the integral
integral = cumulative_trapezoid(y,x, initial=0)
And plot
plt.plot(x,integral)
plt.xlabel('Day')
plt.ylabel('Cumulative Cases')
plt.grid()
plt.show()
x
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.,
11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21.,
22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32.,
33., 34., 35., 36., 37., 38., 39., 40., 41., 42., 43.,
44., 45., 46., 47., 48., 49., 50., 51., 52., 53., 54.,
55., 56., 57., 58., 59., 60., 61., 62., 63., 64., 65.,
66., 67., 68., 69., 70., 71., 72., 73., 74., 75., 76.,
77., 78., 79., 80., 81., 82., 83., 84., 85., 86., 87.,
88., 89., 90., 91., 92., 93., 94., 95., 96., 97., 98.,
99., 100., 101., 102., 103., 104., 105., 106., 107., 108., 109.,
110., 111., 112., 113., 114., 115., 116., 117., 118., 119., 120.,
121., 122., 123., 124., 125., 126., 127., 128., 129., 130., 131.,
132., 133., 134., 135., 136., 137., 138., 139., 140., 141., 142.,
143., 144., 145., 146., 147., 148., 149., 150., 151., 152., 153.,
154., 155., 156., 157., 158., 159., 160., 161., 162., 163., 164.,
165., 166., 167., 168., 169., 170., 171., 172., 173., 174., 175.,
176., 177., 178., 179., 180., 181., 182., 183., 184., 185., 186.,
187., 188., 189., 190., 191., 192., 193., 194., 195., 196., 197.,
198., 199., 200., 201., 202., 203., 204., 205., 206., 207., 208.,
209., 210., 211., 212., 213., 214., 215., 216., 217., 218., 219.,
220., 221., 222., 223., 224., 225., 226., 227., 228., 229., 230.,
231., 232., 233., 234., 235., 236., 237., 238., 239., 240., 241.,
242., 243., 244., 245., 246., 247., 248., 249., 250., 251., 252.,
253., 254., 255., 256., 257., 258., 259., 260., 261., 262., 263.,
264., 265., 266., 267., 268., 269., 270., 271., 272., 273., 274.,
275., 276., 277., 278., 279., 280., 281., 282., 283., 284., 285.,
286., 287., 288., 289., 290., 291., 292., 293., 294., 295., 296.,
297., 298., 299., 300., 301., 302., 303., 304., 305., 306., 307.,
308., 309., 310., 311., 312., 313., 314., 315., 316., 317., 318.,
319., 320., 321., 322., 323., 324., 325., 326., 327., 328., 329.,
330., 331., 332., 333., 334., 335., 336., 337., 338., 339., 340.,
341., 342., 343., 344., 345., 346., 347., 348., 349., 350., 351.,
352., 353., 354., 355., 356., 357., 358., 359., 360., 361., 362.,
363., 364., 365., 366., 367., 368., 369., 370., 371., 372., 373.,
374., 375., 376., 377., 378., 379., 380., 381., 382., 383., 384.,
385., 386., 387., 388., 389., 390., 391., 392., 393., 394., 395.,
396., 397., 398., 399., 400., 401., 402., 403., 404., 405., 406.,
407., 408., 409., 410., 411., 412., 413., 414., 415., 416., 417.,
418., 419., 420., 421., 422., 423., 424., 425., 426., 427., 428.,
429., 430., 431., 432., 433., 434., 435., 436., 437., 438., 439.,
440., 441., 442., 443., 444., 445., 446., 447., 448., 449., 450.,
451., 452., 453., 454., 455., 456., 457., 458., 459., 460., 461.,
462., 463., 464., 465., 466., 467., 468., 469., 470., 471., 472.,
473., 474., 475., 476., 477., 478., 479., 480., 481., 482., 483.,
484., 485., 486., 487., 488., 489., 490., 491., 492., 493., 494.,
495., 496., 497., 498., 499., 500., 501., 502., 503., 504., 505.,
506., 507., 508., 509., 510., 511., 512., 513., 514., 515., 516.,
517., 518., 519., 520., 521., 522., 523., 524., 525., 526., 527.,
528., 529., 530., 531., 532., 533., 534., 535., 536., 537., 538.,
539., 540., 541., 542., 543., 544., 545., 546., 547., 548., 549.,
550., 551.])
np.shape(x)
(552,)
np.array(integral)
array([0.000000e+00, 5.000000e-01, 5.000000e-01, 5.000000e-01,
5.000000e-01, 5.000000e-01, 5.000000e-01, 5.000000e-01,
2.000000e+00, 3.500000e+00, 3.500000e+00, 3.500000e+00,
4.500000e+00, 5.500000e+00, 5.500000e+00, 5.500000e+00,
5.500000e+00, 5.500000e+00, 5.500000e+00, 5.500000e+00,
7.000000e+00, 8.500000e+00, 8.500000e+00, 8.500000e+00,
8.500000e+00, 8.500000e+00, 1.150000e+01, 1.450000e+01,
1.450000e+01, 1.450000e+01, 1.450000e+01, 1.450000e+01,
1.450000e+01, 1.450000e+01, 2.200000e+01, 2.950000e+01,
3.850000e+01, 5.800000e+01, 7.000000e+01, 7.750000e+01,
8.800000e+01, 1.075000e+02, 1.465000e+02, 1.825000e+02,
2.185000e+02, 2.845000e+02, 3.700000e+02, 4.830000e+02,
6.275000e+02, 7.745000e+02, 9.310000e+02, 1.110000e+03,
1.314500e+03, 1.502500e+03, 1.661500e+03, 1.836500e+03,
2.052000e+03, 2.275500e+03, 2.469000e+03, 2.659500e+03,
2.827500e+03, 2.971500e+03, 3.132500e+03, 3.298000e+03,
3.446500e+03, 3.571000e+03, 3.670000e+03, 3.736000e+03,
3.832000e+03, 3.956500e+03, 4.054000e+03, 4.166500e+03,
4.277500e+03, 4.354000e+03, 4.411000e+03, 4.478500e+03,
4.580500e+03, 4.678000e+03, 4.772500e+03, 4.864000e+03,
4.936000e+03, 5.005000e+03, 5.102500e+03, 5.248500e+03,
5.373500e+03, 5.508000e+03, 5.703000e+03, 5.856500e+03,
5.943500e+03, 6.065500e+03, 6.192000e+03, 6.280500e+03,
6.369000e+03, 6.462500e+03, 6.547000e+03, 6.607500e+03,
6.668500e+03, 6.741000e+03, 6.802500e+03, 6.862500e+03,
6.919500e+03, 6.960000e+03, 6.999000e+03, 7.036500e+03,
7.069500e+03, 7.119500e+03, 7.166500e+03, 7.205500e+03,
7.249000e+03, 7.278000e+03, 7.304500e+03, 7.338500e+03,
7.377000e+03, 7.430000e+03, 7.476500e+03, 7.500500e+03,
7.525500e+03, 7.549000e+03, 7.567000e+03, 7.588000e+03,
7.615000e+03, 7.646500e+03, 7.682500e+03, 7.715500e+03,
7.748500e+03, 7.786000e+03, 7.810000e+03, 7.828000e+03,
7.849000e+03, 7.872000e+03, 7.899500e+03, 7.920500e+03,
7.952000e+03, 7.998500e+03, 8.038500e+03, 8.078500e+03,
8.124000e+03, 8.155000e+03, 8.176500e+03, 8.221000e+03,
8.261500e+03, 8.278500e+03, 8.298000e+03, 8.320500e+03,
8.360000e+03, 8.400000e+03, 8.441000e+03, 8.497000e+03,
8.535000e+03, 8.558500e+03, 8.588000e+03, 8.616500e+03,
8.640500e+03, 8.674000e+03, 8.711000e+03, 8.739000e+03,
8.771500e+03, 8.811500e+03, 8.841000e+03, 8.867000e+03,
8.910500e+03, 8.964500e+03, 9.021500e+03, 9.093500e+03,
9.168500e+03, 9.228500e+03, 9.274000e+03, 9.325500e+03,
9.408000e+03, 9.504000e+03, 9.600000e+03, 9.686500e+03,
9.769000e+03, 9.857500e+03, 9.945500e+03, 1.004100e+04,
1.013950e+04, 1.022700e+04, 1.029850e+04, 1.036200e+04,
1.042350e+04, 1.053200e+04, 1.064200e+04, 1.074050e+04,
1.087350e+04, 1.101000e+04, 1.114250e+04, 1.125600e+04,
1.136050e+04, 1.147350e+04, 1.161600e+04, 1.176150e+04,
1.188900e+04, 1.201300e+04, 1.216700e+04, 1.237550e+04,
1.261300e+04, 1.287600e+04, 1.318150e+04, 1.343900e+04,
1.364950e+04, 1.384500e+04, 1.403150e+04, 1.426900e+04,
1.455850e+04, 1.490200e+04, 1.519500e+04, 1.539800e+04,
1.558100e+04, 1.574050e+04, 1.595550e+04, 1.626950e+04,
1.659400e+04, 1.690900e+04, 1.719600e+04, 1.741400e+04,
1.764800e+04, 1.791500e+04, 1.826000e+04, 1.867100e+04,
1.902400e+04, 1.935600e+04, 1.965900e+04, 1.996650e+04,
2.032050e+04, 2.068500e+04, 2.109000e+04, 2.146500e+04,
2.174250e+04, 2.199200e+04, 2.234550e+04, 2.278850e+04,
2.323100e+04, 2.365550e+04, 2.401250e+04, 2.436350e+04,
2.469950e+04, 2.496650e+04, 2.530900e+04, 2.565150e+04,
2.591850e+04, 2.623950e+04, 2.654550e+04, 2.682150e+04,
2.713050e+04, 2.747400e+04, 2.786850e+04, 2.822700e+04,
2.855700e+04, 2.892950e+04, 2.924950e+04, 2.955250e+04,
2.990350e+04, 3.028000e+04, 3.074650e+04, 3.125950e+04,
3.166750e+04, 3.204850e+04, 3.249700e+04, 3.286900e+04,
3.323350e+04, 3.377800e+04, 3.429850e+04, 3.480850e+04,
3.532450e+04, 3.591850e+04, 3.674950e+04, 3.749650e+04,
3.820750e+04, 3.906100e+04, 3.984700e+04, 4.057900e+04,
4.136500e+04, 4.216950e+04, 4.308650e+04, 4.417100e+04,
4.531550e+04, 4.644650e+04, 4.751300e+04, 4.856900e+04,
5.000600e+04, 5.161400e+04, 5.300900e+04, 5.453600e+04,
5.614300e+04, 5.764200e+04, 5.943600e+04, 6.147450e+04,
6.334200e+04, 6.503400e+04, 6.726600e+04, 6.956700e+04,
7.127550e+04, 7.320000e+04, 7.558950e+04, 7.784250e+04,
8.020650e+04, 8.269200e+04, 8.475800e+04, 8.676400e+04,
8.905150e+04, 9.163150e+04, 9.408550e+04, 9.628000e+04,
9.856500e+04, 1.007495e+05, 1.025230e+05, 1.045005e+05,
1.067970e+05, 1.089645e+05, 1.108780e+05, 1.129255e+05,
1.149485e+05, 1.166770e+05, 1.185370e+05, 1.206720e+05,
1.229795e+05, 1.252115e+05, 1.273855e+05, 1.293360e+05,
1.309570e+05, 1.326100e+05, 1.344410e+05, 1.363255e+05,
1.382185e+05, 1.400455e+05, 1.416685e+05, 1.430295e+05,
1.444025e+05, 1.459745e+05, 1.476095e+05, 1.490480e+05,
1.501520e+05, 1.513625e+05, 1.526825e+05, 1.542155e+05,
1.560335e+05, 1.578560e+05, 1.596815e+05, 1.611935e+05,
1.626140e+05, 1.641920e+05, 1.658165e+05, 1.675735e+05,
1.693000e+05, 1.709095e+05, 1.724950e+05, 1.739630e+05,
1.752490e+05, 1.766160e+05, 1.781535e+05, 1.796790e+05,
1.812795e+05, 1.828285e+05, 1.839945e+05, 1.850380e+05,
1.863735e+05, 1.879775e+05, 1.895945e+05, 1.911015e+05,
1.925695e+05, 1.938490e+05, 1.948890e+05, 1.961960e+05,
1.977515e+05, 1.992845e+05, 2.006970e+05, 2.020435e+05,
2.032015e+05, 2.042385e+05, 2.054375e+05, 2.067485e+05,
2.081690e+05, 2.095460e+05, 2.109340e+05, 2.121775e+05,
2.132885e+05, 2.145910e+05, 2.159765e+05, 2.173000e+05,
2.186265e+05, 2.199755e+05, 2.211875e+05, 2.222110e+05,
2.232815e+05, 2.247925e+05, 2.265115e+05, 2.281015e+05,
2.296335e+05, 2.310520e+05, 2.324820e+05, 2.340090e+05,
2.355465e+05, 2.371950e+05, 2.388595e+05, 2.403740e+05,
2.417885e+05, 2.431490e+05, 2.446705e+05, 2.463510e+05,
2.481360e+05, 2.498955e+05, 2.515495e+05, 2.529830e+05,
2.543270e+05, 2.558650e+05, 2.575275e+05, 2.593860e+05,
2.611995e+05, 2.628125e+05, 2.643240e+05, 2.657705e+05,
2.672615e+05, 2.689250e+05, 2.709230e+05, 2.728910e+05,
2.746755e+05, 2.765845e+05, 2.784505e+05, 2.804070e+05,
2.826935e+05, 2.853830e+05, 2.881930e+05, 2.907570e+05,
2.931975e+05, 2.955785e+05, 2.982900e+05, 3.011440e+05,
3.040745e+05, 3.073420e+05, 3.105405e+05, 3.134495e+05,
3.162095e+05, 3.191525e+05, 3.226800e+05, 3.264835e+05,
3.302620e+05, 3.338080e+05, 3.369610e+05, 3.399290e+05,
3.428445e+05, 3.461865e+05, 3.494160e+05, 3.524370e+05,
3.555150e+05, 3.584940e+05, 3.611430e+05, 3.636585e+05,
3.664935e+05, 3.694520e+05, 3.723070e+05, 3.749535e+05,
3.774125e+05, 3.797240e+05, 3.820820e+05, 3.845830e+05,
3.870090e+05, 3.894675e+05, 3.918390e+05, 3.938400e+05,
3.956595e+05, 3.974550e+05, 3.994650e+05, 4.015070e+05,
4.034055e+05, 4.052645e+05, 4.070540e+05, 4.086815e+05,
4.103165e+05, 4.120835e+05, 4.137080e+05, 4.151045e+05,
4.165100e+05, 4.178975e+05, 4.191530e+05, 4.205330e+05,
4.218790e+05, 4.230900e+05, 4.242690e+05, 4.253000e+05,
4.262545e+05, 4.270870e+05, 4.278460e+05, 4.288150e+05,
4.298760e+05, 4.307240e+05, 4.314685e+05, 4.321635e+05,
4.327215e+05, 4.332495e+05, 4.338450e+05, 4.344110e+05,
4.350190e+05, 4.355665e+05, 4.359745e+05, 4.364065e+05,
4.368925e+05, 4.373840e+05, 4.378365e+05, 4.382145e+05,
4.385535e+05, 4.388370e+05, 4.391025e+05, 4.394250e+05,
4.397745e+05, 4.401320e+05, 4.404415e+05, 4.407165e+05,
4.409210e+05, 4.410740e+05, 4.412840e+05, 4.415195e+05,
4.417405e+05, 4.419315e+05, 4.420910e+05, 4.422285e+05,
4.423355e+05, 4.424515e+05, 4.425915e+05, 4.427165e+05,
4.428160e+05, 4.429185e+05, 4.430115e+05, 4.431155e+05,
4.432500e+05, 4.434165e+05, 4.435920e+05, 4.437450e+05,
4.438790e+05, 4.439805e+05, 4.440875e+05, 4.441980e+05,
4.443295e+05, 4.444840e+05, 4.446355e+05, 4.448125e+05,
4.449805e+05, 4.451500e+05, 4.453645e+05, 4.456270e+05,
4.459435e+05, 4.462495e+05, 4.465135e+05, 4.467895e+05,
4.471385e+05, 4.476300e+05, 4.482250e+05, 4.488820e+05,
4.494660e+05, 4.499925e+05, 4.505640e+05, 4.511430e+05])
np.array(integral)[551]
451143.0
trapezoid(y, x)
451143.0
simpson(y, x)
451138.9166666666
import matplotlib.pyplot as plt
plt.style.use(['seaborn', 'notebook'])
from pylab import plot, show, xlabel, ylabel, figure
from numpy import random, sqrt, pi, cos, sin, arange
n =10**4 #Number of tones
x =random.random_sample(n)
y =random.random_sample(n)
r =sqrt(x**2+y**2)
inside =r<=1
outside=1-inside
print(n,sum(inside)/n,pi/4)
figure(figsize=(10,10))
phi=arange(0,pi/2,0.001)
plot(cos(phi),sin(phi), c='orange',linewidth=4)
plot(x*inside,y*inside,"m.",markersize=5)
plot(x*outside,y*outside,"g.",markersize=5)
xlabel("$X$",fontsize=18)
ylabel("$y$",fontsize=18)
show()
10000 0.7834 0.7853981633974483
n =10**6 #Number of tones
x =random.random_sample(n)
y =random.random_sample(n)
r =sqrt(x**2+y**2)
inside =r<=1
outside=1-inside
print(n,sum(inside)/n,pi/4)
1000000 0.785397 0.7853981633974483
from pylab import plot,show,fill_between,xlim,ylim,figure
from numpy import linspace,sin, cos
def f(x):
return (cos(1/sin((x-1)*(4-x))))**(6)
x=linspace(1.00000001,3.9999999,10000)
figure(figsize=(12,7))
xlim(1,4)
ylim(0,0.03)
plot(x,f(x),'m')
fill_between(x,f(x),facecolor='gold')
show()
from math import sin, cos
from random import random
def f(x):
return (cos(1/sin((x-1)*(4-x))))**(6)
N = 100000
count = 0
for i in range(N):
x = 2*random()
y = random()
if y<f(x):
count += 1
I = 2*count/N
print(I)
0.21072
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
from scipy.integrate import odeint
from scipy.integrate import solve_ivp
Air friction while falling
$$ \frac{dv}{dt} - \alpha v^2 + \beta = 0 \hspace{10mm} v(0) = 0$$The first thing we need to do is write it in the form
$$\frac{dv}{dt} = f(t,v)$$In other words, "derivative of v equals something that depends v and time". This is easy in this example:
$$\frac{dv}{dt} = \alpha v^2 - \beta$$and note that there is no dependence on time in this particular example. We need to write this differential equation in python form
def dvdt(t, v):
return 3*v**2 - 5
v0 = 0 # Initial Condition
t = np.linspace(0, 1, 100)
t
array([0. , 0.01010101, 0.02020202, 0.03030303, 0.04040404,
0.05050505, 0.06060606, 0.07070707, 0.08080808, 0.09090909,
0.1010101 , 0.11111111, 0.12121212, 0.13131313, 0.14141414,
0.15151515, 0.16161616, 0.17171717, 0.18181818, 0.19191919,
0.2020202 , 0.21212121, 0.22222222, 0.23232323, 0.24242424,
0.25252525, 0.26262626, 0.27272727, 0.28282828, 0.29292929,
0.3030303 , 0.31313131, 0.32323232, 0.33333333, 0.34343434,
0.35353535, 0.36363636, 0.37373737, 0.38383838, 0.39393939,
0.4040404 , 0.41414141, 0.42424242, 0.43434343, 0.44444444,
0.45454545, 0.46464646, 0.47474747, 0.48484848, 0.49494949,
0.50505051, 0.51515152, 0.52525253, 0.53535354, 0.54545455,
0.55555556, 0.56565657, 0.57575758, 0.58585859, 0.5959596 ,
0.60606061, 0.61616162, 0.62626263, 0.63636364, 0.64646465,
0.65656566, 0.66666667, 0.67676768, 0.68686869, 0.6969697 ,
0.70707071, 0.71717172, 0.72727273, 0.73737374, 0.74747475,
0.75757576, 0.76767677, 0.77777778, 0.78787879, 0.7979798 ,
0.80808081, 0.81818182, 0.82828283, 0.83838384, 0.84848485,
0.85858586, 0.86868687, 0.87878788, 0.88888889, 0.8989899 ,
0.90909091, 0.91919192, 0.92929293, 0.93939394, 0.94949495,
0.95959596, 0.96969697, 0.97979798, 0.98989899, 1. ])
sol_m1 = odeint(dvdt, y0=v0, t=t, tfirst=True)
sol_m2 = solve_ivp(dvdt, t_span=(0,max(t)), y0=[v0], t_eval=t)
sol_m1
array([[ 0. ],
[-0.05047933],
[-0.10080451],
[-0.15082334],
[-0.20038726],
[-0.24935318],
[-0.29758502],
[-0.34495519],
[-0.39134576],
[-0.43664948],
[-0.48077056],
[-0.52362515],
[-0.56514164],
[-0.60526086],
[-0.6439356 ],
[-0.68113064],
[-0.71682205],
[-0.75099661],
[-0.78365109],
[-0.8147914 ],
[-0.84443176],
[-0.87259378],
[-0.89930556],
[-0.92460079],
[-0.94851787],
[-0.97109913],
[-0.99239002],
[-1.01243837],
[-1.03129376],
[-1.04900692],
[-1.06562918],
[-1.081212 ],
[-1.0958066 ],
[-1.10946355],
[-1.12223249],
[-1.13416195],
[-1.14529906],
[-1.15568941],
[-1.16537698],
[-1.17440397],
[-1.18281086],
[-1.19063623],
[-1.19791686],
[-1.20468768],
[-1.21098179],
[-1.21683053],
[-1.22226349],
[-1.22730856],
[-1.23199199],
[-1.23633849],
[-1.24037122],
[-1.24411192],
[-1.24758094],
[-1.25079735],
[-1.25377896],
[-1.25654241],
[-1.25910322],
[-1.2614759 ],
[-1.26367394],
[-1.26570992],
[-1.26759557],
[-1.26934179],
[-1.27095869],
[-1.27245574],
[-1.27384168],
[-1.27512464],
[-1.2763122 ],
[-1.27741137],
[-1.27842865],
[-1.27937009],
[-1.28024128],
[-1.28104744],
[-1.28179338],
[-1.28248356],
[-1.28312213],
[-1.28371292],
[-1.2842595 ],
[-1.28476514],
[-1.2852329 ],
[-1.28566562],
[-1.2860659 ],
[-1.28643616],
[-1.28677865],
[-1.28709545],
[-1.28738847],
[-1.28765951],
[-1.28791021],
[-1.28814208],
[-1.28835653],
[-1.28855487],
[-1.28873831],
[-1.28890797],
[-1.28906488],
[-1.28920999],
[-1.2893442 ],
[-1.28946833],
[-1.28958312],
[-1.28968929],
[-1.28978746],
[-1.28987826]])
sol_m1.T
array([[ 0. , -0.05047933, -0.10080451, -0.15082334, -0.20038726,
-0.24935318, -0.29758502, -0.34495519, -0.39134576, -0.43664948,
-0.48077056, -0.52362515, -0.56514164, -0.60526086, -0.6439356 ,
-0.68113064, -0.71682205, -0.75099661, -0.78365109, -0.8147914 ,
-0.84443176, -0.87259378, -0.89930556, -0.92460079, -0.94851787,
-0.97109913, -0.99239002, -1.01243837, -1.03129376, -1.04900692,
-1.06562918, -1.081212 , -1.0958066 , -1.10946355, -1.12223249,
-1.13416195, -1.14529906, -1.15568941, -1.16537698, -1.17440397,
-1.18281086, -1.19063623, -1.19791686, -1.20468768, -1.21098179,
-1.21683053, -1.22226349, -1.22730856, -1.23199199, -1.23633849,
-1.24037122, -1.24411192, -1.24758094, -1.25079735, -1.25377896,
-1.25654241, -1.25910322, -1.2614759 , -1.26367394, -1.26570992,
-1.26759557, -1.26934179, -1.27095869, -1.27245574, -1.27384168,
-1.27512464, -1.2763122 , -1.27741137, -1.27842865, -1.27937009,
-1.28024128, -1.28104744, -1.28179338, -1.28248356, -1.28312213,
-1.28371292, -1.2842595 , -1.28476514, -1.2852329 , -1.28566562,
-1.2860659 , -1.28643616, -1.28677865, -1.28709545, -1.28738847,
-1.28765951, -1.28791021, -1.28814208, -1.28835653, -1.28855487,
-1.28873831, -1.28890797, -1.28906488, -1.28920999, -1.2893442 ,
-1.28946833, -1.28958312, -1.28968929, -1.28978746, -1.28987826]])
sol_m1.T[0]
array([ 0. , -0.05047933, -0.10080451, -0.15082334, -0.20038726,
-0.24935318, -0.29758502, -0.34495519, -0.39134576, -0.43664948,
-0.48077056, -0.52362515, -0.56514164, -0.60526086, -0.6439356 ,
-0.68113064, -0.71682205, -0.75099661, -0.78365109, -0.8147914 ,
-0.84443176, -0.87259378, -0.89930556, -0.92460079, -0.94851787,
-0.97109913, -0.99239002, -1.01243837, -1.03129376, -1.04900692,
-1.06562918, -1.081212 , -1.0958066 , -1.10946355, -1.12223249,
-1.13416195, -1.14529906, -1.15568941, -1.16537698, -1.17440397,
-1.18281086, -1.19063623, -1.19791686, -1.20468768, -1.21098179,
-1.21683053, -1.22226349, -1.22730856, -1.23199199, -1.23633849,
-1.24037122, -1.24411192, -1.24758094, -1.25079735, -1.25377896,
-1.25654241, -1.25910322, -1.2614759 , -1.26367394, -1.26570992,
-1.26759557, -1.26934179, -1.27095869, -1.27245574, -1.27384168,
-1.27512464, -1.2763122 , -1.27741137, -1.27842865, -1.27937009,
-1.28024128, -1.28104744, -1.28179338, -1.28248356, -1.28312213,
-1.28371292, -1.2842595 , -1.28476514, -1.2852329 , -1.28566562,
-1.2860659 , -1.28643616, -1.28677865, -1.28709545, -1.28738847,
-1.28765951, -1.28791021, -1.28814208, -1.28835653, -1.28855487,
-1.28873831, -1.28890797, -1.28906488, -1.28920999, -1.2893442 ,
-1.28946833, -1.28958312, -1.28968929, -1.28978746, -1.28987826])
sol_m1.T[1]
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) /var/folders/dw/p9lncvq57tv4px69pk911vl40000gn/T/ipykernel_39741/2027523669.py in <module> ----> 1 sol_m1.T[1] IndexError: index 1 is out of bounds for axis 0 with size 1
sol_m2
message: 'The solver successfully reached the end of the integration interval.'
nfev: 68
njev: 0
nlu: 0
sol: None
status: 0
success: True
t: array([0. , 0.01010101, 0.02020202, 0.03030303, 0.04040404,
0.05050505, 0.06060606, 0.07070707, 0.08080808, 0.09090909,
0.1010101 , 0.11111111, 0.12121212, 0.13131313, 0.14141414,
0.15151515, 0.16161616, 0.17171717, 0.18181818, 0.19191919,
0.2020202 , 0.21212121, 0.22222222, 0.23232323, 0.24242424,
0.25252525, 0.26262626, 0.27272727, 0.28282828, 0.29292929,
0.3030303 , 0.31313131, 0.32323232, 0.33333333, 0.34343434,
0.35353535, 0.36363636, 0.37373737, 0.38383838, 0.39393939,
0.4040404 , 0.41414141, 0.42424242, 0.43434343, 0.44444444,
0.45454545, 0.46464646, 0.47474747, 0.48484848, 0.49494949,
0.50505051, 0.51515152, 0.52525253, 0.53535354, 0.54545455,
0.55555556, 0.56565657, 0.57575758, 0.58585859, 0.5959596 ,
0.60606061, 0.61616162, 0.62626263, 0.63636364, 0.64646465,
0.65656566, 0.66666667, 0.67676768, 0.68686869, 0.6969697 ,
0.70707071, 0.71717172, 0.72727273, 0.73737374, 0.74747475,
0.75757576, 0.76767677, 0.77777778, 0.78787879, 0.7979798 ,
0.80808081, 0.81818182, 0.82828283, 0.83838384, 0.84848485,
0.85858586, 0.86868687, 0.87878788, 0.88888889, 0.8989899 ,
0.90909091, 0.91919192, 0.92929293, 0.93939394, 0.94949495,
0.95959596, 0.96969697, 0.97979798, 0.98989899, 1. ])
t_events: None
y: array([[ 0. , -0.0504793 , -0.10080722, -0.15083038, -0.20039553,
-0.24935878, -0.29758564, -0.34495103, -0.39133929, -0.43664418,
-0.48076886, -0.52362591, -0.56513507, -0.60524282, -0.64391404,
-0.68112062, -0.71684143, -0.75106235, -0.78377623, -0.81498294,
-0.84468932, -0.87290923, -0.89966349, -0.92497994, -0.94889341,
-0.97144571, -0.99268566, -1.01266906, -1.03145871, -1.04912441,
-1.06573928, -1.08134937, -1.09599363, -1.10971235, -1.12254598,
-1.13453506, -1.14572027, -1.15614242, -1.16584241, -1.17486131,
-1.18324028, -1.19102062, -1.19824374, -1.20495119, -1.21118463,
-1.21698585, -1.22239666, -1.22744653, -1.23215067, -1.23652537,
-1.24058697, -1.24435186, -1.24783652, -1.25105747, -1.25403128,
-1.25677462, -1.25930417, -1.2616367 , -1.26378904, -1.26577807,
-1.26762074, -1.26933406, -1.27093508, -1.27243817, -1.27384571,
-1.27515955, -1.27638204, -1.27751582, -1.27856386, -1.27952945,
-1.28041617, -1.28122794, -1.28196899, -1.28264386, -1.2832574 ,
-1.28381478, -1.28432149, -1.28478332, -1.2852064 , -1.28559714,
-1.2859623 , -1.28630892, -1.2866444 , -1.28697068, -1.28727516,
-1.28755781, -1.28781965, -1.28806173, -1.28828513, -1.28849093,
-1.28868023, -1.28885416, -1.28901385, -1.28916046, -1.28929516,
-1.28941915, -1.28953362, -1.2896398 , -1.28973894, -1.2898323 ]])
y_events: None
sol_m2.t
array([0. , 0.01010101, 0.02020202, 0.03030303, 0.04040404,
0.05050505, 0.06060606, 0.07070707, 0.08080808, 0.09090909,
0.1010101 , 0.11111111, 0.12121212, 0.13131313, 0.14141414,
0.15151515, 0.16161616, 0.17171717, 0.18181818, 0.19191919,
0.2020202 , 0.21212121, 0.22222222, 0.23232323, 0.24242424,
0.25252525, 0.26262626, 0.27272727, 0.28282828, 0.29292929,
0.3030303 , 0.31313131, 0.32323232, 0.33333333, 0.34343434,
0.35353535, 0.36363636, 0.37373737, 0.38383838, 0.39393939,
0.4040404 , 0.41414141, 0.42424242, 0.43434343, 0.44444444,
0.45454545, 0.46464646, 0.47474747, 0.48484848, 0.49494949,
0.50505051, 0.51515152, 0.52525253, 0.53535354, 0.54545455,
0.55555556, 0.56565657, 0.57575758, 0.58585859, 0.5959596 ,
0.60606061, 0.61616162, 0.62626263, 0.63636364, 0.64646465,
0.65656566, 0.66666667, 0.67676768, 0.68686869, 0.6969697 ,
0.70707071, 0.71717172, 0.72727273, 0.73737374, 0.74747475,
0.75757576, 0.76767677, 0.77777778, 0.78787879, 0.7979798 ,
0.80808081, 0.81818182, 0.82828283, 0.83838384, 0.84848485,
0.85858586, 0.86868687, 0.87878788, 0.88888889, 0.8989899 ,
0.90909091, 0.91919192, 0.92929293, 0.93939394, 0.94949495,
0.95959596, 0.96969697, 0.97979798, 0.98989899, 1. ])
sol_m2.y
array([[ 0. , -0.0504793 , -0.10080722, -0.15083038, -0.20039553,
-0.24935878, -0.29758564, -0.34495103, -0.39133929, -0.43664418,
-0.48076886, -0.52362591, -0.56513507, -0.60524282, -0.64391404,
-0.68112062, -0.71684143, -0.75106235, -0.78377623, -0.81498294,
-0.84468932, -0.87290923, -0.89966349, -0.92497994, -0.94889341,
-0.97144571, -0.99268566, -1.01266906, -1.03145871, -1.04912441,
-1.06573928, -1.08134937, -1.09599363, -1.10971235, -1.12254598,
-1.13453506, -1.14572027, -1.15614242, -1.16584241, -1.17486131,
-1.18324028, -1.19102062, -1.19824374, -1.20495119, -1.21118463,
-1.21698585, -1.22239666, -1.22744653, -1.23215067, -1.23652537,
-1.24058697, -1.24435186, -1.24783652, -1.25105747, -1.25403128,
-1.25677462, -1.25930417, -1.2616367 , -1.26378904, -1.26577807,
-1.26762074, -1.26933406, -1.27093508, -1.27243817, -1.27384571,
-1.27515955, -1.27638204, -1.27751582, -1.27856386, -1.27952945,
-1.28041617, -1.28122794, -1.28196899, -1.28264386, -1.2832574 ,
-1.28381478, -1.28432149, -1.28478332, -1.2852064 , -1.28559714,
-1.2859623 , -1.28630892, -1.2866444 , -1.28697068, -1.28727516,
-1.28755781, -1.28781965, -1.28806173, -1.28828513, -1.28849093,
-1.28868023, -1.28885416, -1.28901385, -1.28916046, -1.28929516,
-1.28941915, -1.28953362, -1.2896398 , -1.28973894, -1.2898323 ]])
v_sol_m1 = sol_m1.T[0]
v_sol_m2 = sol_m2.y[0]
plt.plot(t, sol_m1.T[0], label = "Solusion 1", color = 'orange', linewidth = 4, zorder=0)
plt.ylabel('$v(t)$', fontsize=22)
plt.xlabel('$t$', fontsize=22)
plt.legend()
plt.show()
plt.plot(t, sol_m1.T[0], label = "Solusion 1", color = 'orange', linewidth = 8, zorder=0)
plt.scatter(t, sol_m2.y[0], label = "Solusion 2", color = 'green', s= 25, zorder=1, alpha=0.6)
plt.ylabel('$v(t)$', fontsize=22)
plt.xlabel('$t$', fontsize=22)
plt.legend()
plt.show()
Letting $S=(y_1, y_2)$ we need to write a function that returns $dS/dx = (dy_1/dx, dy_2/dx)$. The function $dS/dx$ can take in $S=(y_1, y_2)$ and $x$. This is like before, but in vector format
$$ \vec{S} = \begin{bmatrix} y_1 \\ y_2 \end{bmatrix} \hspace{10mm} \implies \hspace{10mm} \frac{d\vec{S}}{dx} = \vec{f}(x, \vec{S}) = \vec{f}(x, y_1, y_2) = \begin{bmatrix} y_1' \\ y_2' \end{bmatrix} = \begin{bmatrix} y_1 + y_2^2 + 3x\\ 3y_1 + y_2^3 - \cos(x) \end{bmatrix}$$def dSdx(x, S):
y1, y2 = S
return [y1 + y2**2 + 3*x,
3*y1 + y2**3 - np.cos(x)]
y1_0 = 0
y2_0 = 0
S_0 = (y1_0, y2_0)
x = np.linspace(0, 1, 100)
sol = odeint(dSdx, y0=S_0, t=x, tfirst=True)
x
array([0. , 0.01010101, 0.02020202, 0.03030303, 0.04040404,
0.05050505, 0.06060606, 0.07070707, 0.08080808, 0.09090909,
0.1010101 , 0.11111111, 0.12121212, 0.13131313, 0.14141414,
0.15151515, 0.16161616, 0.17171717, 0.18181818, 0.19191919,
0.2020202 , 0.21212121, 0.22222222, 0.23232323, 0.24242424,
0.25252525, 0.26262626, 0.27272727, 0.28282828, 0.29292929,
0.3030303 , 0.31313131, 0.32323232, 0.33333333, 0.34343434,
0.35353535, 0.36363636, 0.37373737, 0.38383838, 0.39393939,
0.4040404 , 0.41414141, 0.42424242, 0.43434343, 0.44444444,
0.45454545, 0.46464646, 0.47474747, 0.48484848, 0.49494949,
0.50505051, 0.51515152, 0.52525253, 0.53535354, 0.54545455,
0.55555556, 0.56565657, 0.57575758, 0.58585859, 0.5959596 ,
0.60606061, 0.61616162, 0.62626263, 0.63636364, 0.64646465,
0.65656566, 0.66666667, 0.67676768, 0.68686869, 0.6969697 ,
0.70707071, 0.71717172, 0.72727273, 0.73737374, 0.74747475,
0.75757576, 0.76767677, 0.77777778, 0.78787879, 0.7979798 ,
0.80808081, 0.81818182, 0.82828283, 0.83838384, 0.84848485,
0.85858586, 0.86868687, 0.87878788, 0.88888889, 0.8989899 ,
0.90909091, 0.91919192, 0.92929293, 0.93939394, 0.94949495,
0.95959596, 0.96969697, 0.97979798, 0.98989899, 1. ])
sol
array([[ 0.00000000e+00, 0.00000000e+00],
[ 1.53914061e-04, -1.00992734e-02],
[ 6.19093356e-04, -2.01882005e-02],
[ 1.40076628e-03, -3.02563164e-02],
[ 2.50418953e-03, -4.02930766e-02],
[ 3.93464633e-03, -5.02878361e-02],
[ 5.69744401e-03, -6.02298432e-02],
[ 7.79789517e-03, -7.01082428e-02],
[ 1.02413187e-02, -7.99120712e-02],
[ 1.30330245e-02, -8.96302498e-02],
[ 1.61783065e-02, -9.92515816e-02],
[ 1.96824391e-02, -1.08764749e-01],
[ 2.35506573e-02, -1.18158303e-01],
[ 2.77881638e-02, -1.27420669e-01],
[ 3.24001125e-02, -1.36540133e-01],
[ 3.73915921e-02, -1.45504834e-01],
[ 4.27676179e-02, -1.54302762e-01],
[ 4.85331404e-02, -1.62921756e-01],
[ 5.46930282e-02, -1.71349495e-01],
[ 6.12520553e-02, -1.79573493e-01],
[ 6.82148995e-02, -1.87581095e-01],
[ 7.55861308e-02, -1.95359467e-01],
[ 8.33702071e-02, -2.02895598e-01],
[ 9.15714669e-02, -2.10176290e-01],
[ 1.00194121e-01, -2.17188157e-01],
[ 1.09242251e-01, -2.23917621e-01],
[ 1.18719801e-01, -2.30350906e-01],
[ 1.28630571e-01, -2.36474041e-01],
[ 1.38978219e-01, -2.42272857e-01],
[ 1.49766252e-01, -2.47732981e-01],
[ 1.60998020e-01, -2.52839835e-01],
[ 1.72676732e-01, -2.57578659e-01],
[ 1.84805434e-01, -2.61934483e-01],
[ 1.97387014e-01, -2.65892141e-01],
[ 2.10424222e-01, -2.69436297e-01],
[ 2.23919648e-01, -2.72551418e-01],
[ 2.37875730e-01, -2.75221791e-01],
[ 2.52294784e-01, -2.77431559e-01],
[ 2.67178978e-01, -2.79164693e-01],
[ 2.82530347e-01, -2.80405012e-01],
[ 2.98350829e-01, -2.81136222e-01],
[ 3.14642248e-01, -2.81341905e-01],
[ 3.31406342e-01, -2.81005542e-01],
[ 3.48644774e-01, -2.80110529e-01],
[ 3.66359140e-01, -2.78640187e-01],
[ 3.84551013e-01, -2.76577800e-01],
[ 4.03221942e-01, -2.73906616e-01],
[ 4.22373488e-01, -2.70609873e-01],
[ 4.42007253e-01, -2.66670823e-01],
[ 4.62124894e-01, -2.62072745e-01],
[ 4.82728181e-01, -2.56798966e-01],
[ 5.03819005e-01, -2.50832883e-01],
[ 5.25399429e-01, -2.44157971e-01],
[ 5.47471735e-01, -2.36757804e-01],
[ 5.70038442e-01, -2.28616068e-01],
[ 5.93102385e-01, -2.19716553e-01],
[ 6.16666735e-01, -2.10043183e-01],
[ 6.40735063e-01, -1.99579993e-01],
[ 6.65311401e-01, -1.88311119e-01],
[ 6.90400276e-01, -1.76220821e-01],
[ 7.16006799e-01, -1.63293405e-01],
[ 7.42136714e-01, -1.49513226e-01],
[ 7.68796463e-01, -1.34864636e-01],
[ 7.95993253e-01, -1.19331954e-01],
[ 8.23735143e-01, -1.02899359e-01],
[ 8.52031104e-01, -8.55508556e-02],
[ 8.80891112e-01, -6.72701355e-02],
[ 9.10326223e-01, -4.80405161e-02],
[ 9.40348671e-01, -2.78447548e-02],
[ 9.70971957e-01, -6.66492984e-03],
[ 1.00221095e+00, 1.55177321e-02],
[ 1.03408198e+00, 3.87230761e-02],
[ 1.06660299e+00, 6.29722272e-02],
[ 1.09979361e+00, 8.82878711e-02],
[ 1.13367532e+00, 1.14694542e-01],
[ 1.16827157e+00, 1.42218950e-01],
[ 1.20360797e+00, 1.70890394e-01],
[ 1.23971243e+00, 2.00741167e-01],
[ 1.27661538e+00, 2.31807067e-01],
[ 1.31434994e+00, 2.64127972e-01],
[ 1.35295223e+00, 2.97748502e-01],
[ 1.39246158e+00, 3.32718734e-01],
[ 1.43292087e+00, 3.69095107e-01],
[ 1.47437687e+00, 4.06941417e-01],
[ 1.51688069e+00, 4.46329999e-01],
[ 1.56048820e+00, 4.87343105e-01],
[ 1.60526063e+00, 5.30074560e-01],
[ 1.65126523e+00, 5.74631731e-01],
[ 1.69857604e+00, 6.21137874e-01],
[ 1.74727479e+00, 6.69734974e-01],
[ 1.79745207e+00, 7.20587249e-01],
[ 1.84920864e+00, 7.73885430e-01],
[ 1.90265708e+00, 8.29852098e-01],
[ 1.95792385e+00, 8.88748463e-01],
[ 2.01515175e+00, 9.50882831e-01],
[ 2.07450310e+00, 1.01662163e+00],
[ 2.13616376e+00, 1.08640368e+00],
[ 2.20034823e+00, 1.16075924e+00],
[ 2.26730636e+00, 1.24033522e+00],
[ 2.33733216e+00, 1.32592999e+00]])
sol.T
array([[ 0.00000000e+00, 1.53914061e-04, 6.19093356e-04,
1.40076628e-03, 2.50418953e-03, 3.93464633e-03,
5.69744401e-03, 7.79789517e-03, 1.02413187e-02,
1.30330245e-02, 1.61783065e-02, 1.96824391e-02,
2.35506573e-02, 2.77881638e-02, 3.24001125e-02,
3.73915921e-02, 4.27676179e-02, 4.85331404e-02,
5.46930282e-02, 6.12520553e-02, 6.82148995e-02,
7.55861308e-02, 8.33702071e-02, 9.15714669e-02,
1.00194121e-01, 1.09242251e-01, 1.18719801e-01,
1.28630571e-01, 1.38978219e-01, 1.49766252e-01,
1.60998020e-01, 1.72676732e-01, 1.84805434e-01,
1.97387014e-01, 2.10424222e-01, 2.23919648e-01,
2.37875730e-01, 2.52294784e-01, 2.67178978e-01,
2.82530347e-01, 2.98350829e-01, 3.14642248e-01,
3.31406342e-01, 3.48644774e-01, 3.66359140e-01,
3.84551013e-01, 4.03221942e-01, 4.22373488e-01,
4.42007253e-01, 4.62124894e-01, 4.82728181e-01,
5.03819005e-01, 5.25399429e-01, 5.47471735e-01,
5.70038442e-01, 5.93102385e-01, 6.16666735e-01,
6.40735063e-01, 6.65311401e-01, 6.90400276e-01,
7.16006799e-01, 7.42136714e-01, 7.68796463e-01,
7.95993253e-01, 8.23735143e-01, 8.52031104e-01,
8.80891112e-01, 9.10326223e-01, 9.40348671e-01,
9.70971957e-01, 1.00221095e+00, 1.03408198e+00,
1.06660299e+00, 1.09979361e+00, 1.13367532e+00,
1.16827157e+00, 1.20360797e+00, 1.23971243e+00,
1.27661538e+00, 1.31434994e+00, 1.35295223e+00,
1.39246158e+00, 1.43292087e+00, 1.47437687e+00,
1.51688069e+00, 1.56048820e+00, 1.60526063e+00,
1.65126523e+00, 1.69857604e+00, 1.74727479e+00,
1.79745207e+00, 1.84920864e+00, 1.90265708e+00,
1.95792385e+00, 2.01515175e+00, 2.07450310e+00,
2.13616376e+00, 2.20034823e+00, 2.26730636e+00,
2.33733216e+00],
[ 0.00000000e+00, -1.00992734e-02, -2.01882005e-02,
-3.02563164e-02, -4.02930766e-02, -5.02878361e-02,
-6.02298432e-02, -7.01082428e-02, -7.99120712e-02,
-8.96302498e-02, -9.92515816e-02, -1.08764749e-01,
-1.18158303e-01, -1.27420669e-01, -1.36540133e-01,
-1.45504834e-01, -1.54302762e-01, -1.62921756e-01,
-1.71349495e-01, -1.79573493e-01, -1.87581095e-01,
-1.95359467e-01, -2.02895598e-01, -2.10176290e-01,
-2.17188157e-01, -2.23917621e-01, -2.30350906e-01,
-2.36474041e-01, -2.42272857e-01, -2.47732981e-01,
-2.52839835e-01, -2.57578659e-01, -2.61934483e-01,
-2.65892141e-01, -2.69436297e-01, -2.72551418e-01,
-2.75221791e-01, -2.77431559e-01, -2.79164693e-01,
-2.80405012e-01, -2.81136222e-01, -2.81341905e-01,
-2.81005542e-01, -2.80110529e-01, -2.78640187e-01,
-2.76577800e-01, -2.73906616e-01, -2.70609873e-01,
-2.66670823e-01, -2.62072745e-01, -2.56798966e-01,
-2.50832883e-01, -2.44157971e-01, -2.36757804e-01,
-2.28616068e-01, -2.19716553e-01, -2.10043183e-01,
-1.99579993e-01, -1.88311119e-01, -1.76220821e-01,
-1.63293405e-01, -1.49513226e-01, -1.34864636e-01,
-1.19331954e-01, -1.02899359e-01, -8.55508556e-02,
-6.72701355e-02, -4.80405161e-02, -2.78447548e-02,
-6.66492984e-03, 1.55177321e-02, 3.87230761e-02,
6.29722272e-02, 8.82878711e-02, 1.14694542e-01,
1.42218950e-01, 1.70890394e-01, 2.00741167e-01,
2.31807067e-01, 2.64127972e-01, 2.97748502e-01,
3.32718734e-01, 3.69095107e-01, 4.06941417e-01,
4.46329999e-01, 4.87343105e-01, 5.30074560e-01,
5.74631731e-01, 6.21137874e-01, 6.69734974e-01,
7.20587249e-01, 7.73885430e-01, 8.29852098e-01,
8.88748463e-01, 9.50882831e-01, 1.01662163e+00,
1.08640368e+00, 1.16075924e+00, 1.24033522e+00,
1.32592999e+00]])
sol.T[0]
array([0.00000000e+00, 1.53914061e-04, 6.19093356e-04, 1.40076628e-03,
2.50418953e-03, 3.93464633e-03, 5.69744401e-03, 7.79789517e-03,
1.02413187e-02, 1.30330245e-02, 1.61783065e-02, 1.96824391e-02,
2.35506573e-02, 2.77881638e-02, 3.24001125e-02, 3.73915921e-02,
4.27676179e-02, 4.85331404e-02, 5.46930282e-02, 6.12520553e-02,
6.82148995e-02, 7.55861308e-02, 8.33702071e-02, 9.15714669e-02,
1.00194121e-01, 1.09242251e-01, 1.18719801e-01, 1.28630571e-01,
1.38978219e-01, 1.49766252e-01, 1.60998020e-01, 1.72676732e-01,
1.84805434e-01, 1.97387014e-01, 2.10424222e-01, 2.23919648e-01,
2.37875730e-01, 2.52294784e-01, 2.67178978e-01, 2.82530347e-01,
2.98350829e-01, 3.14642248e-01, 3.31406342e-01, 3.48644774e-01,
3.66359140e-01, 3.84551013e-01, 4.03221942e-01, 4.22373488e-01,
4.42007253e-01, 4.62124894e-01, 4.82728181e-01, 5.03819005e-01,
5.25399429e-01, 5.47471735e-01, 5.70038442e-01, 5.93102385e-01,
6.16666735e-01, 6.40735063e-01, 6.65311401e-01, 6.90400276e-01,
7.16006799e-01, 7.42136714e-01, 7.68796463e-01, 7.95993253e-01,
8.23735143e-01, 8.52031104e-01, 8.80891112e-01, 9.10326223e-01,
9.40348671e-01, 9.70971957e-01, 1.00221095e+00, 1.03408198e+00,
1.06660299e+00, 1.09979361e+00, 1.13367532e+00, 1.16827157e+00,
1.20360797e+00, 1.23971243e+00, 1.27661538e+00, 1.31434994e+00,
1.35295223e+00, 1.39246158e+00, 1.43292087e+00, 1.47437687e+00,
1.51688069e+00, 1.56048820e+00, 1.60526063e+00, 1.65126523e+00,
1.69857604e+00, 1.74727479e+00, 1.79745207e+00, 1.84920864e+00,
1.90265708e+00, 1.95792385e+00, 2.01515175e+00, 2.07450310e+00,
2.13616376e+00, 2.20034823e+00, 2.26730636e+00, 2.33733216e+00])
sol.T[1]
array([ 0. , -0.01009927, -0.0201882 , -0.03025632, -0.04029308,
-0.05028784, -0.06022984, -0.07010824, -0.07991207, -0.08963025,
-0.09925158, -0.10876475, -0.1181583 , -0.12742067, -0.13654013,
-0.14550483, -0.15430276, -0.16292176, -0.17134949, -0.17957349,
-0.18758109, -0.19535947, -0.2028956 , -0.21017629, -0.21718816,
-0.22391762, -0.23035091, -0.23647404, -0.24227286, -0.24773298,
-0.25283984, -0.25757866, -0.26193448, -0.26589214, -0.2694363 ,
-0.27255142, -0.27522179, -0.27743156, -0.27916469, -0.28040501,
-0.28113622, -0.2813419 , -0.28100554, -0.28011053, -0.27864019,
-0.2765778 , -0.27390662, -0.27060987, -0.26667082, -0.26207275,
-0.25679897, -0.25083288, -0.24415797, -0.2367578 , -0.22861607,
-0.21971655, -0.21004318, -0.19957999, -0.18831112, -0.17622082,
-0.16329341, -0.14951323, -0.13486464, -0.11933195, -0.10289936,
-0.08555086, -0.06727014, -0.04804052, -0.02784475, -0.00666493,
0.01551773, 0.03872308, 0.06297223, 0.08828787, 0.11469454,
0.14221895, 0.17089039, 0.20074117, 0.23180707, 0.26412797,
0.2977485 , 0.33271873, 0.36909511, 0.40694142, 0.44633 ,
0.4873431 , 0.53007456, 0.57463173, 0.62113787, 0.66973497,
0.72058725, 0.77388543, 0.8298521 , 0.88874846, 0.95088283,
1.01662163, 1.08640368, 1.16075924, 1.24033522, 1.32592999])
y1_sol = sol.T[0]
y1_sol
array([0.00000000e+00, 1.53914061e-04, 6.19093356e-04, 1.40076628e-03,
2.50418953e-03, 3.93464633e-03, 5.69744401e-03, 7.79789517e-03,
1.02413187e-02, 1.30330245e-02, 1.61783065e-02, 1.96824391e-02,
2.35506573e-02, 2.77881638e-02, 3.24001125e-02, 3.73915921e-02,
4.27676179e-02, 4.85331404e-02, 5.46930282e-02, 6.12520553e-02,
6.82148995e-02, 7.55861308e-02, 8.33702071e-02, 9.15714669e-02,
1.00194121e-01, 1.09242251e-01, 1.18719801e-01, 1.28630571e-01,
1.38978219e-01, 1.49766252e-01, 1.60998020e-01, 1.72676732e-01,
1.84805434e-01, 1.97387014e-01, 2.10424222e-01, 2.23919648e-01,
2.37875730e-01, 2.52294784e-01, 2.67178978e-01, 2.82530347e-01,
2.98350829e-01, 3.14642248e-01, 3.31406342e-01, 3.48644774e-01,
3.66359140e-01, 3.84551013e-01, 4.03221942e-01, 4.22373488e-01,
4.42007253e-01, 4.62124894e-01, 4.82728181e-01, 5.03819005e-01,
5.25399429e-01, 5.47471735e-01, 5.70038442e-01, 5.93102385e-01,
6.16666735e-01, 6.40735063e-01, 6.65311401e-01, 6.90400276e-01,
7.16006799e-01, 7.42136714e-01, 7.68796463e-01, 7.95993253e-01,
8.23735143e-01, 8.52031104e-01, 8.80891112e-01, 9.10326223e-01,
9.40348671e-01, 9.70971957e-01, 1.00221095e+00, 1.03408198e+00,
1.06660299e+00, 1.09979361e+00, 1.13367532e+00, 1.16827157e+00,
1.20360797e+00, 1.23971243e+00, 1.27661538e+00, 1.31434994e+00,
1.35295223e+00, 1.39246158e+00, 1.43292087e+00, 1.47437687e+00,
1.51688069e+00, 1.56048820e+00, 1.60526063e+00, 1.65126523e+00,
1.69857604e+00, 1.74727479e+00, 1.79745207e+00, 1.84920864e+00,
1.90265708e+00, 1.95792385e+00, 2.01515175e+00, 2.07450310e+00,
2.13616376e+00, 2.20034823e+00, 2.26730636e+00, 2.33733216e+00])
y2_sol = sol.T[1]
y2_sol
array([ 0. , -0.01009927, -0.0201882 , -0.03025632, -0.04029308,
-0.05028784, -0.06022984, -0.07010824, -0.07991207, -0.08963025,
-0.09925158, -0.10876475, -0.1181583 , -0.12742067, -0.13654013,
-0.14550483, -0.15430276, -0.16292176, -0.17134949, -0.17957349,
-0.18758109, -0.19535947, -0.2028956 , -0.21017629, -0.21718816,
-0.22391762, -0.23035091, -0.23647404, -0.24227286, -0.24773298,
-0.25283984, -0.25757866, -0.26193448, -0.26589214, -0.2694363 ,
-0.27255142, -0.27522179, -0.27743156, -0.27916469, -0.28040501,
-0.28113622, -0.2813419 , -0.28100554, -0.28011053, -0.27864019,
-0.2765778 , -0.27390662, -0.27060987, -0.26667082, -0.26207275,
-0.25679897, -0.25083288, -0.24415797, -0.2367578 , -0.22861607,
-0.21971655, -0.21004318, -0.19957999, -0.18831112, -0.17622082,
-0.16329341, -0.14951323, -0.13486464, -0.11933195, -0.10289936,
-0.08555086, -0.06727014, -0.04804052, -0.02784475, -0.00666493,
0.01551773, 0.03872308, 0.06297223, 0.08828787, 0.11469454,
0.14221895, 0.17089039, 0.20074117, 0.23180707, 0.26412797,
0.2977485 , 0.33271873, 0.36909511, 0.40694142, 0.44633 ,
0.4873431 , 0.53007456, 0.57463173, 0.62113787, 0.66973497,
0.72058725, 0.77388543, 0.8298521 , 0.88874846, 0.95088283,
1.01662163, 1.08640368, 1.16075924, 1.24033522, 1.32592999])
plt.plot(x, y1_sol, c='orange')
plt.plot(x, y2_sol, c='g')
[<matplotlib.lines.Line2D at 0x7fb63c95edf0>]
Python does not have functions to directly solve second order ODEs.
Consider
$$\ddot{x} = -\dot{x}^2 + \sin(x)$$We can convert this into two first order ODEs as follows:
Our two equations:
$$\dot{x} = v$$$$\dot{v} = -v^2 + \sin(x)$$These are two coupled first order equations. They require an initial condition ($x_0$ and $v_0$)
def dSdx(x, S):
x, v = S
return [v,
-v**2 + np.sin(x)]
x_0 = 0
v_0 = 5
S_0 = (x_0, v_0)
t = np.linspace(0, 1, 100)
sol = odeint(dSdx, y0=S_0, t=t, tfirst=True)
x_sol = sol.T[0]
v_sol = sol.T[1]
plt.plot(t, x_sol, c='green')
plt.plot(t, v_sol, c='orange')
[<matplotlib.lines.Line2D at 0x7fb63caa5340>]
These make up four differential equations. Then noting that $\dot{a_1} = \dddot{x_1}$ and $\dot{a_2} = \dddot{x_2}$ we get
Then
def dSdt(t, S):
x1, v1, a1, x2, v2, a2 = S
return [v1,
a1,
-2*v2**2 + x2,
v2,
a2,
-a1**3 + v2 + v1 + np.sin(t)]
x1_0 = 0
v1_0 = 0
a1_0 = 0
x2_0 = 0
v2_0 = 0
a2_0 = 0
v_0 = 0
S_0 = (x1_0, v1_0, a1_0, x1_0, v1_0, a1_0)
t = np.linspace(0, 1, 100)
sol = odeint(dSdt, y0=S_0, t=t, tfirst=True)
plt.plot(t,sol.T[0], c = 'orange')
[<matplotlib.lines.Line2D at 0x7fb63cbd2970>]
plt.plot(t,sol.T[0], c = 'm')
plt.plot(t,sol.T[1], c = 'brown')
plt.plot(t,sol.T[2], c = '#FF00FF')
plt.plot(t,sol.T[3], c = '#800080')
plt.plot(t,sol.T[4], c = 'green')
plt.plot(t,sol.T[5], c = 'orange')
plt.ylim(0,0.002)
(0.0, 0.002)
Not all solvers work for all ODEs. For example, the ODEs for 3 body motion
Require the DOP853 solver with low values for rtol and atol to solve
Always review the literature on your specific ODE to find an appropriate solver. In addition, play around with the parameters rtol and atol to ensure you get a proper solution
def dSdt(t, S):
x1, y1, x2, y2, x3, y3, vx1, vy1, vx2, vy2, vx3, vy3 = S
r12 = np.sqrt((x2-x1)**2 + (y2-y1)**2)
r13 = np.sqrt((x3-x1)**2 + (y3-y1)**2)
r23 = np.sqrt((x2-x3)**2 + (y2-y3)**2)
return [ vx1,
vy1,
vx2,
vy2,
vx3,
vy3,
1/r12**3 * (x2-x1) + 1/r13**3 * (x3-x1), #mass 1
1/r12**3 * (y2-y1) + 1/r13**3 * (y3-y1),
1/r12**3 * (x1-x2) + 1/r23**3 * (x3-x2), #mass 2
1/r12**3 * (y1-y2) + 1/r23**3 * (y3-y2),
1/r13**3 * (x1-x3) + 1/r23**3 * (x2-x3), #mass 3
1/r13**3 * (y1-y3) + 1/r23**3 * (y2-y3)
]
v1 = 0.39295
v2 = 0.09758
x1_0 = -1
y1_0 = 0
x2_0 = 1
y2_0 = 0
x3_0 = 0
y3_0 = 0
vx1_0 = v1
vy1_0 = v2
vx2_0 = v1
vy2_0 = v2
vx3_0 = -2*v1
vy3_0 = -2*v2
t = np.linspace(0, 40, 1000)
sol = solve_ivp(dSdt, (0,40), y0=[x1_0, y1_0, x2_0, y2_0, x3_0, y3_0,
vx1_0, vy1_0, vx2_0, vy2_0, vx3_0, vy3_0], method = 'DOP853',
t_eval=t, rtol=1e-10, atol=1e-13)
t = sol.t
x1 = sol.y[0]
y1 = sol.y[1]
x2 = sol.y[2]
y2 = sol.y[3]
x3 = sol.y[4]
y3 = sol.y[5]
plt.plot(t, y1, c='orange')
[<matplotlib.lines.Line2D at 0x7fcef97c33d0>]
t = np.linspace(0, 40, 1000)
sol = solve_ivp(dSdt, (0,40), y0=[x1_0, y1_0, x2_0, y2_0, x3_0, y3_0,
vx1_0, vy1_0, vx2_0, vy2_0, vx3_0, vy3_0], method = 'DOP853',
t_eval=t)
t = sol.t
x1 = sol.y[0]
y1 = sol.y[1]
x2 = sol.y[2]
y2 = sol.y[3]
x3 = sol.y[4]
y3 = sol.y[5]
plt.plot(t, y1, c='orange')
[<matplotlib.lines.Line2D at 0x7fce70877640>]
t = np.linspace(0, 40, 1000)
sol = solve_ivp(dSdt, (0,40), y0=[x1_0, y1_0, x2_0, y2_0, x3_0, y3_0,
vx1_0, vy1_0, vx2_0, vy2_0, vx3_0, vy3_0],
t_eval=t)
t = sol.t
x1 = sol.y[0]
y1 = sol.y[1]
x2 = sol.y[2]
y2 = sol.y[3]
x3 = sol.y[4]
y3 = sol.y[5]
plt.plot(t, y1, c='orange')
[<matplotlib.lines.Line2D at 0x7fcefaac6430>]