# SciPy¶

- Baut auf NumPy auf
- Kann numerisch integrieren, DGLs lösen, optimieren, minimieren, …
- Enthält auch physikalische Konstanten und wichtige mathematische Funktionen

Überblick über (fast) alles weitere was SciPy kann gibt es hier:

- https://github.com/MaxNoe/scientific_python_notebooks/blob/master/scipy.ipynb

In [1]:

    import numpy as np

    x = np.array([2, 5, 7])

In [2]:

    # standard error of the mean
    from scipy.stats import sem

    sem(x)

Out[2]:

    1.4529663145135578

In [3]:

    # physical constants
    import scipy.constants as const

    const.epsilon_0

Out[3]:

    8.854187817620389e-12

In [4]:

    # convert temperatures:
    print(const.convert_temperature(100, 'c', 'K'))
    print(const.convert_temperature(100, 'kelvin', 'Celsius'))

    373.15
    -173.14999999999998

In [5]:

    # convert angles:
    print(np.rad2deg(np.pi))
    print(np.deg2rad(90))

    180.0
    1.5707963267948966

In [6]:

    const.physical_constants

Out[6]:

    {'Wien displacement law constant': (0.0028977685, 'm K', 5.1e-09),
     'atomic unit of 1st hyperpolarizablity': (3.20636151e-53,
      'C^3 m^3 J^-2',
      2.8e-60),
     'atomic unit of 2nd hyperpolarizablity': (6.2353808e-65,
      'C^4 m^4 J^-3',
      1.1e-71),
     'atomic unit of electric dipole moment': (8.47835309e-30, 'C m', 7.3e-37),
     'atomic unit of electric polarizablity': (1.648777274e-41,
      'C^2 m^2 J^-1',
      1.6e-49),
     'atomic unit of electric quadrupole moment': (4.48655124e-40,
      'C m^2',
      3.9e-47),
     'atomic unit of magn. dipole moment': (1.8548019e-23, 'J T^-1', 1.6e-30),
     'atomic unit of magn. flux density': (235051.755, 'T', 0.0014),
     'deuteron magn. moment': (4.33073482e-27, 'J T^-1', 3.8e-34),
     'deuteron magn. moment to Bohr magneton ratio': (0.0004669754567, '', 5e-12),
     'deuteron magn. moment to nuclear magneton ratio': (0.8574382329,
      '',
      9.2e-09),
     'deuteron-electron magn. moment ratio': (-0.0004664345548, '', 5e-12),
     'deuteron-proton magn. moment ratio': (0.3070122084, '', 4.5e-09),
     'deuteron-neutron magn. moment ratio': (-0.44820652, '', 1.1e-07),
     'electron gyromagn. ratio': (176085964400.0, 's^-1 T^-1', 1100.0),
     'electron gyromagn. ratio over 2 pi': (28024.95164, 'MHz T^-1', 0.00017),
     'electron magn. moment': (-9.28476412e-24, 'J T^-1', 8e-31),
     'electron magn. moment to Bohr magneton ratio': (-1.0011596521859,
      '',
      3.8e-12),
     'electron magn. moment to nuclear magneton ratio': (-1838.28197107,
      '',
      8.5e-07),
     'electron magn. moment anomaly': (0.0011596521859, '', 3.8e-12),
     'electron to shielded proton magn. moment ratio': (-658.2275956, '', 7.1e-06),
     'electron to shielded helion magn. moment ratio': (864.058255, '', 1e-05),
     'electron-deuteron magn. moment ratio': (-2143.923493, '', 2.3e-05),
     'electron-muon magn. moment ratio': (206.7669894, '', 5.4e-06),
     'electron-neutron magn. moment ratio': (960.9205, '', 0.00023),
     'electron-proton magn. moment ratio': (-658.2106862, '', 6.6e-06),
     'magn. constant': (1.2566370614359173e-06, 'N A^-2', 0.0),
     'magn. flux quantum': (2.067833831e-15, 'Wb', 1.3e-23),
     'muon magn. moment': (-4.49044799e-26, 'J T^-1', 4e-33),
     'muon magn. moment to Bohr magneton ratio': (-0.00484197045, '', 1.3e-10),
     'muon magn. moment to nuclear magneton ratio': (-8.89059698, '', 2.3e-07),
     'muon-proton magn. moment ratio': (-3.183345118, '', 8.9e-08),
     'neutron gyromagn. ratio': (183247172.0, 's^-1 T^-1', 43.0),
     'neutron gyromagn. ratio over 2 pi': (29.1646933, 'MHz T^-1', 6.9e-06),
     'neutron magn. moment': (-9.6623645e-27, 'J T^-1', 2.4e-33),
     'neutron magn. moment to Bohr magneton ratio': (-0.00104187563, '', 2.5e-10),
     'neutron magn. moment to nuclear magneton ratio': (-1.91304273, '', 4.5e-07),
     'neutron to shielded proton magn. moment ratio': (-0.68499694, '', 1.6e-07),
     'neutron-electron magn. moment ratio': (0.00104066882, '', 2.5e-10),
     'neutron-proton magn. moment ratio': (-0.68497934, '', 1.6e-07),
     'proton gyromagn. ratio': (267522190.0, 's^-1 T^-1', 1.8),
     'proton gyromagn. ratio over 2 pi': (42.57747892, 'MHz T^-1', 2.9e-07),
     'proton magn. moment': (1.41060671e-26, 'J T^-1', 1.2e-33),
     'proton magn. moment to Bohr magneton ratio': (0.001521032206, '', 1.5e-11),
     'proton magn. moment to nuclear magneton ratio': (2.792847351, '', 2.8e-08),
     'proton magn. shielding correction': (2.5691e-05, '', 1.1e-08),
     'proton-neutron magn. moment ratio': (-1.45989805, '', 3.4e-07),
     'shielded helion gyromagn. ratio': (203789458.5, 's^-1 T^-1', 2.7),
     'shielded helion gyromagn. ratio over 2 pi': (32.43409966,
      'MHz T^-1',
      4.3e-07),
     'shielded helion magn. moment': (-1.074553024e-26, 'J T^-1', 9.3e-34),
     'shielded helion magn. moment to Bohr magneton ratio': (-0.001158671474,
      '',
      1.4e-11),
     'shielded helion magn. moment to nuclear magneton ratio': (-2.127497723,
      '',
      2.5e-08),
     'shielded helion to proton magn. moment ratio': (-0.761766562, '', 1.2e-08),
     'shielded helion to shielded proton magn. moment ratio': (-0.7617861313,
      '',
      3.3e-09),
     'shielded proton magn. moment': (1.41057047e-26, 'J T^-1', 1.2e-33),
     'shielded proton magn. moment to Bohr magneton ratio': (0.001520993132,
      '',
      1.6e-11),
     'shielded proton magn. moment to nuclear magneton ratio': (2.792775604,
      '',
      3e-08),
     '{220} lattice spacing of silicon': (1.920155714e-10, 'm', 3.2e-18),
     'lattice spacing of silicon': (1.920155762e-10, 'm', 5e-18),
     'alpha particle-electron mass ratio': (7294.29954136, '', 2.4e-07),
     'alpha particle mass': (6.64465723e-27, 'kg', 8.2e-35),
     'alpha particle mass energy equivalent': (5.971920097e-10, 'J', 7.3e-18),
     'alpha particle mass energy equivalent in MeV': (3727.379378, 'MeV', 2.3e-05),
     'alpha particle mass in u': (4.001506179127, 'u', 6.3e-11),
     'alpha particle molar mass': (0.004001506179127, 'kg mol^-1', 6.3e-14),
     'alpha particle-proton mass ratio': (3.97259968907, '', 3.6e-10),
     'Angstrom star': (1.00001495e-10, 'm', 9e-17),
     'atomic mass constant': (1.66053904e-27, 'kg', 2e-35),
     'atomic mass constant energy equivalent': (1.492418062e-10, 'J', 1.8e-18),
     'atomic mass constant energy equivalent in MeV': (931.4940954,
      'MeV',
      5.7e-06),
     'atomic mass unit-electron volt relationship': (931494095.4, 'eV', 5.7),
     'atomic mass unit-hartree relationship': (34231776.902, 'E_h', 0.016),
     'atomic mass unit-hertz relationship': (2.2523427206e+23,
      'Hz',
      100000000000000.0),
     'atomic mass unit-inverse meter relationship': (751300661660000.0,
      'm^-1',
      340000.0),
     'atomic mass unit-joule relationship': (1.492418062e-10, 'J', 1.8e-18),
     'atomic mass unit-kelvin relationship': (10809543800000.0, 'K', 6200000.0),
     'atomic mass unit-kilogram relationship': (1.66053904e-27, 'kg', 2e-35),
     'atomic unit of 1st hyperpolarizability': (3.206361329e-53,
      'C^3 m^3 J^-2',
      2e-61),
     'atomic unit of 2nd hyperpolarizability': (6.235380085e-65,
      'C^4 m^4 J^-3',
      7.7e-73),
     'atomic unit of action': (1.0545718e-34, 'J s', 1.3e-42),
     'atomic unit of charge': (1.6021766208e-19, 'C', 9.8e-28),
     'atomic unit of charge density': (1081202377000.0, 'C m^-3', 6700.0),
     'atomic unit of current': (0.006623618183, 'A', 4.1e-11),
     'atomic unit of electric dipole mom.': (8.478353552e-30, 'C m', 5.2e-38),
     'atomic unit of electric field': (514220670700.0, 'V m^-1', 3200.0),
     'atomic unit of electric field gradient': (9.717362356e+21,
      'V m^-2',
      60000000000000.0),
     'atomic unit of electric polarizability': (1.6487772731e-41,
      'C^2 m^2 J^-1',
      1.1e-50),
     'atomic unit of electric potential': (27.21138602, 'V', 1.7e-07),
     'atomic unit of electric quadrupole mom.': (4.486551484e-40,
      'C m^2',
      2.8e-48),
     'atomic unit of energy': (4.35974465e-18, 'J', 5.4e-26),
     'atomic unit of force': (8.23872336e-08, 'N', 1e-15),
     'atomic unit of length': (5.2917721067e-11, 'm', 1.2e-20),
     'atomic unit of mag. dipole mom.': (1.854801999e-23, 'J T^-1', 1.1e-31),
     'atomic unit of mag. flux density': (235051.755, 'T', 0.0014),
     'atomic unit of magnetizability': (7.8910365886e-29, 'J T^-2', 9e-38),
     'atomic unit of mass': (9.10938356e-31, 'kg', 1.1e-38),
     'atomic unit of momentum': (1.992851882e-24, 'kg m s^-1', 2.4e-32),
     'atomic unit of permittivity': (1.1126500560536183e-10, 'F m^-1', 0.0),
     'atomic unit of time': (2.418884326509e-17, 's', 1.4e-28),
     'atomic unit of velocity': (2187691.26277, 'm s^-1', 0.0005),
     'Avogadro constant': (6.022140857e+23, 'mol^-1', 7400000000000000.0),
     'Bohr magneton': (9.274009994e-24, 'J T^-1', 5.7e-32),
     'Bohr magneton in eV/T': (5.7883818012e-05, 'eV T^-1', 2.6e-14),
     'Bohr magneton in Hz/T': (13996245042.0, 'Hz T^-1', 86.0),
     'Bohr magneton in inverse meters per tesla': (46.68644814,
      'm^-1 T^-1',
      2.9e-07),
     'Bohr magneton in K/T': (0.67171405, 'K T^-1', 3.9e-07),
     'Bohr radius': (5.2917721067e-11, 'm', 1.2e-20),
     'Boltzmann constant': (1.38064852e-23, 'J K^-1', 7.9e-30),
     'Boltzmann constant in eV/K': (8.6173303e-05, 'eV K^-1', 5e-11),
     'Boltzmann constant in Hz/K': (20836612000.0, 'Hz K^-1', 12000.0),
     'Boltzmann constant in inverse meters per kelvin': (69.503457,
      'm^-1 K^-1',
      4e-05),
     'characteristic impedance of vacuum': (376.73031346177066, 'ohm', 0.0),
     'classical electron radius': (2.8179403227e-15, 'm', 1.9e-24),
     'Compton wavelength': (2.4263102367e-12, 'm', 1.1e-21),
     'Compton wavelength over 2 pi': (3.8615926764e-13, 'm', 1.8e-22),
     'conductance quantum': (7.748091731e-05, 'S', 1.8e-14),
     'conventional value of Josephson constant': (483597900000000.0,
      'Hz V^-1',
      0.0),
     'conventional value of von Klitzing constant': (25812.807, 'ohm', 0.0),
     'Cu x unit': (1.00207697e-13, 'm', 2.8e-20),
     'deuteron-electron mag. mom. ratio': (-0.0004664345535, '', 2.6e-12),
     'deuteron-electron mass ratio': (3670.48296785, '', 1.3e-07),
     'deuteron g factor': (0.8574382311, '', 4.8e-09),
     'deuteron mag. mom.': (4.33073504e-27, 'J T^-1', 3.6e-35),
     'deuteron mag. mom. to Bohr magneton ratio': (0.0004669754554, '', 2.6e-12),
     'deuteron mag. mom. to nuclear magneton ratio': (0.8574382311, '', 4.8e-09),
     'deuteron mass': (3.343583719e-27, 'kg', 4.1e-35),
     'deuteron mass energy equivalent': (3.005063183e-10, 'J', 3.7e-18),
     'deuteron mass energy equivalent in MeV': (1875.612928, 'MeV', 1.2e-05),
     'deuteron mass in u': (2.013553212745, 'u', 4e-11),
     'deuteron molar mass': (0.002013553212745, 'kg mol^-1', 4e-14),
     'deuteron-neutron mag. mom. ratio': (-0.44820652, '', 1.1e-07),
     'deuteron-proton mag. mom. ratio': (0.3070122077, '', 1.5e-09),
     'deuteron-proton mass ratio': (1.99900750087, '', 1.9e-10),
     'deuteron rms charge radius': (2.1413e-15, 'm', 2.5e-18),
     'electric constant': (8.854187817620389e-12, 'F m^-1', 0.0),
     'electron charge to mass quotient': (-175882002400.0, 'C kg^-1', 1100.0),
     'electron-deuteron mag. mom. ratio': (-2143.923499, '', 1.2e-05),
     'electron-deuteron mass ratio': (0.0002724437107484, '', 9.6e-15),
     'electron g factor': (-2.00231930436182, '', 5.2e-13),
     'electron gyromag. ratio': (176085964400.0, 's^-1 T^-1', 1100.0),
     'electron gyromag. ratio over 2 pi': (28024.95164, 'MHz T^-1', 0.00017),
     'electron mag. mom.': (-9.28476462e-24, 'J T^-1', 5.7e-32),
     'electron mag. mom. anomaly': (0.00115965218091, '', 2.6e-13),
     'electron mag. mom. to Bohr magneton ratio': (-1.00115965218091, '', 2.6e-13),
     'electron mag. mom. to nuclear magneton ratio': (-1838.28197234, '', 1.7e-07),
     'electron mass': (9.10938356e-31, 'kg', 1.1e-38),
     'electron mass energy equivalent': (8.18710565e-14, 'J', 1e-21),
     'electron mass energy equivalent in MeV': (0.5109989461, 'MeV', 3.1e-09),
     'electron mass in u': (0.00054857990907, 'u', 1.6e-14),
     'electron molar mass': (5.4857990907e-07, 'kg mol^-1', 1.6e-17),
     'electron-muon mag. mom. ratio': (206.766988, '', 4.6e-06),
     'electron-muon mass ratio': (0.0048363317, '', 1.1e-10),
     'electron-neutron mag. mom. ratio': (960.9205, '', 0.00023),
     'electron-neutron mass ratio': (0.00054386734428, '', 2.7e-13),
     'electron-proton mag. mom. ratio': (-658.2106866, '', 2e-06),
     'electron-proton mass ratio': (0.000544617021352, '', 5.2e-14),
     'electron-tau mass ratio': (0.000287592, '', 2.6e-08),
     'electron to alpha particle mass ratio': (0.0001370933554798, '', 4.5e-15),
     'electron to shielded helion mag. mom. ratio': (864.058257, '', 1e-05),
     'electron to shielded proton mag. mom. ratio': (-658.2275971, '', 7.2e-06),
     'electron volt': (1.6021766208e-19, 'J', 9.8e-28),
     'electron volt-atomic mass unit relationship': (1.0735441105e-09,
      'u',
      6.6e-18),
     'electron volt-hartree relationship': (0.03674932248, 'E_h', 2.3e-10),
     'electron volt-hertz relationship': (241798926200000.0, 'Hz', 1500000.0),
     'electron volt-inverse meter relationship': (806554.4005, 'm^-1', 0.005),
     'electron volt-joule relationship': (1.6021766208e-19, 'J', 9.8e-28),
     'electron volt-kelvin relationship': (11604.5221, 'K', 0.0067),
     'electron volt-kilogram relationship': (1.782661907e-36, 'kg', 1.1e-44),
     'elementary charge': (1.6021766208e-19, 'C', 9.8e-28),
     'elementary charge over h': (241798926200000.0, 'A J^-1', 1500000.0),
     'Faraday constant': (96485.33289, 'C mol^-1', 0.00059),
     'Faraday constant for conventional electric current': (96485.3251,
      'C_90 mol^-1',
      0.0012),
     'Fermi coupling constant': (1.1663787e-05, 'GeV^-2', 6e-12),
     'fine-structure constant': (0.0072973525664, '', 1.7e-12),
     'first radiation constant': (3.74177179e-16, 'W m^2', 4.6e-24),
     'first radiation constant for spectral radiance': (1.191042953e-16,
      'W m^2 sr^-1',
      1.5e-24),
     'hartree-atomic mass unit relationship': (2.9212623197e-08, 'u', 1.3e-17),
     'hartree-electron volt relationship': (27.21138602, 'eV', 1.7e-07),
     'Hartree energy': (4.35974465e-18, 'J', 5.4e-26),
     'Hartree energy in eV': (27.21138602, 'eV', 1.7e-07),
     'hartree-hertz relationship': (6579683920711000.0, 'Hz', 39000.0),
     'hartree-inverse meter relationship': (21947463.13702, 'm^-1', 0.00013),
     'hartree-joule relationship': (4.35974465e-18, 'J', 5.4e-26),
     'hartree-kelvin relationship': (315775.13, 'K', 0.18),
     'hartree-kilogram relationship': (4.850870129e-35, 'kg', 6e-43),
     'helion-electron mass ratio': (5495.88527922, '', 2.7e-07),
     'helion mass': (5.0064127e-27, 'kg', 6.2e-35),
     'helion mass energy equivalent': (4.499539341e-10, 'J', 5.5e-18),
     'helion mass energy equivalent in MeV': (2808.391586, 'MeV', 1.7e-05),
     'helion mass in u': (3.01493224673, 'u', 1.2e-10),
     'helion molar mass': (0.00301493224673, 'kg mol^-1', 1.2e-13),
     'helion-proton mass ratio': (2.99315267046, '', 2.9e-10),
     'hertz-atomic mass unit relationship': (4.4398216616e-24, 'u', 2e-33),
     'hertz-electron volt relationship': (4.135667662e-15, 'eV', 2.5e-23),
     'hertz-hartree relationship': (1.5198298460088e-16, 'E_h', 9e-28),
     'hertz-inverse meter relationship': (3.3356409519815204e-09, 'm^-1', 0.0),
     'hertz-joule relationship': (6.62607004e-34, 'J', 8.1e-42),
     'hertz-kelvin relationship': (4.7992447e-11, 'K', 2.8e-17),
     'hertz-kilogram relationship': (7.372497201e-51, 'kg', 9.1e-59),
     'inverse fine-structure constant': (137.035999139, '', 3.1e-08),
     'inverse meter-atomic mass unit relationship': (1.331025049e-15,
      'u',
      6.1e-25),
     'inverse meter-electron volt relationship': (1.2398419739e-06, 'eV', 7.6e-15),
     'inverse meter-hartree relationship': (4.556335252767e-08, 'E_h', 2.7e-19),
     'inverse meter-hertz relationship': (299792458.0, 'Hz', 0.0),
     'inverse meter-joule relationship': (1.986445824e-25, 'J', 2.4e-33),
     'inverse meter-kelvin relationship': (0.0143877736, 'K', 8.3e-09),
     'inverse meter-kilogram relationship': (2.210219057e-42, 'kg', 2.7e-50),
     'inverse of conductance quantum': (12906.4037278, 'ohm', 2.9e-06),
     'Josephson constant': (483597852500000.0, 'Hz V^-1', 3000000.0),
     'joule-atomic mass unit relationship': (6700535363.0, 'u', 82.0),
     'joule-electron volt relationship': (6.241509126e+18, 'eV', 38000000000.0),
     'joule-hartree relationship': (2.293712317e+17, 'E_h', 2800000000.0),
     'joule-hertz relationship': (1.509190205e+33, 'Hz', 1.9e+25),
     'joule-inverse meter relationship': (5.034116651e+24, 'm^-1', 6.2e+16),
     'joule-kelvin relationship': (7.2429731e+22, 'K', 4.2e+16),
     'joule-kilogram relationship': (1.1126500560536185e-17, 'kg', 0.0),
     'kelvin-atomic mass unit relationship': (9.2510842e-14, 'u', 5.3e-20),
     'kelvin-electron volt relationship': (8.6173303e-05, 'eV', 5e-11),
     'kelvin-hartree relationship': (3.1668105e-06, 'E_h', 1.8e-12),
     'kelvin-hertz relationship': (20836612000.0, 'Hz', 12000.0),
     'kelvin-inverse meter relationship': (69.503457, 'm^-1', 4e-05),
     'kelvin-joule relationship': (1.38064852e-23, 'J', 7.9e-30),
     'kelvin-kilogram relationship': (1.53617865e-40, 'kg', 8.8e-47),
     'kilogram-atomic mass unit relationship': (6.022140857e+26, 'u', 7.4e+18),
     'kilogram-electron volt relationship': (5.60958865e+35, 'eV', 3.4e+27),
     'kilogram-hartree relationship': (2.061485823e+34, 'E_h', 2.5e+26),
     'kilogram-hertz relationship': (1.356392512e+50, 'Hz', 1.7e+42),
     'kilogram-inverse meter relationship': (4.524438411e+41, 'm^-1', 5.6e+33),
     'kilogram-joule relationship': (8.987551787368176e+16, 'J', 0.0),
     'kilogram-kelvin relationship': (6.5096595e+39, 'K', 3.7e+33),
     'lattice parameter of silicon': (5.431020504e-10, 'm', 8.9e-18),
     'Loschmidt constant (273.15 K, 101.325 kPa)': (2.6867811e+25,
      'm^-3',
      1.5e+19),
     'mag. constant': (1.2566370614359173e-06, 'N A^-2', 0.0),
     'mag. flux quantum': (2.067833831e-15, 'Wb', 1.3e-23),
     'molar gas constant': (8.3144598, 'J mol^-1 K^-1', 4.8e-06),
     'molar mass constant': (0.001, 'kg mol^-1', 0.0),
     'molar mass of carbon-12': (0.012, 'kg mol^-1', 0.0),
     'molar Planck constant': (3.990312711e-10, 'J s mol^-1', 1.8e-19),
     'molar Planck constant times c': (0.119626565582, 'J m mol^-1', 5.4e-11),
     'molar volume of ideal gas (273.15 K, 100 kPa)': (0.022710947,
      'm^3 mol^-1',
      1.3e-08),
     'molar volume of ideal gas (273.15 K, 101.325 kPa)': (0.022413962,
      'm^3 mol^-1',
      1.3e-08),
     'molar volume of silicon': (1.205883214e-05, 'm^3 mol^-1', 6.1e-13),
     'Mo x unit': (1.00209952e-13, 'm', 5.3e-20),
     'muon Compton wavelength': (1.173444111e-14, 'm', 2.6e-22),
     'muon Compton wavelength over 2 pi': (1.867594308e-15, 'm', 4.2e-23),
     'muon-electron mass ratio': (206.7682826, '', 4.6e-06),
     'muon g factor': (-2.0023318418, '', 1.3e-09),
     'muon mag. mom.': (-4.49044826e-26, 'J T^-1', 1e-33),
     'muon mag. mom. anomaly': (0.00116592089, '', 6.3e-10),
     'muon mag. mom. to Bohr magneton ratio': (-0.00484197048, '', 1.1e-10),
     'muon mag. mom. to nuclear magneton ratio': (-8.89059705, '', 2e-07),
     'muon mass': (1.883531594e-28, 'kg', 4.8e-36),
     'muon mass energy equivalent': (1.692833774e-11, 'J', 4.3e-19),
     'muon mass energy equivalent in MeV': (105.6583745, 'MeV', 2.4e-06),
     'muon mass in u': (0.1134289257, 'u', 2.5e-09),
     'muon molar mass': (0.0001134289257, 'kg mol^-1', 2.5e-12),
     'muon-neutron mass ratio': (0.1124545167, '', 2.5e-09),
     'muon-proton mag. mom. ratio': (-3.183345142, '', 7.1e-08),
     'muon-proton mass ratio': (0.1126095262, '', 2.5e-09),
     'muon-tau mass ratio': (0.0594649, '', 5.4e-06),
     'natural unit of action': (1.0545718e-34, 'J s', 1.3e-42),
     'natural unit of action in eV s': (6.582119514e-16, 'eV s', 4e-24),
     'natural unit of energy': (8.18710565e-14, 'J', 1e-21),
     'natural unit of energy in MeV': (0.5109989461, 'MeV', 3.1e-09),
     'natural unit of length': (3.8615926764e-13, 'm', 1.8e-22),
     'natural unit of mass': (9.10938356e-31, 'kg', 1.1e-38),
     'natural unit of momentum': (2.730924488e-22, 'kg m s^-1', 3.4e-30),
     'natural unit of momentum in MeV/c': (0.5109989461, 'MeV/c', 3.1e-09),
     'natural unit of time': (1.28808866712e-21, 's', 5.8e-31),
     'natural unit of velocity': (299792458.0, 'm s^-1', 0.0),
     'neutron Compton wavelength': (1.31959090481e-15, 'm', 8.8e-25),
     'neutron Compton wavelength over 2 pi': (2.1001941536e-16, 'm', 1.4e-25),
     'neutron-electron mag. mom. ratio': (0.00104066882, '', 2.5e-10),
     'neutron-electron mass ratio': (1838.68366158, '', 9e-07),
     'neutron g factor': (-3.82608545, '', 9e-07),
     'neutron gyromag. ratio': (183247172.0, 's^-1 T^-1', 43.0),
     'neutron gyromag. ratio over 2 pi': (29.1646933, 'MHz T^-1', 6.9e-06),
     'neutron mag. mom.': (-9.662365e-27, 'J T^-1', 2.3e-33),
     'neutron mag. mom. to Bohr magneton ratio': (-0.00104187563, '', 2.5e-10),
     'neutron mag. mom. to nuclear magneton ratio': (-1.91304273, '', 4.5e-07),
     'neutron mass': (1.674927471e-27, 'kg', 2.1e-35),
     'neutron mass energy equivalent': (1.505349739e-10, 'J', 1.9e-18),
     'neutron mass energy equivalent in MeV': (939.5654133, 'MeV', 5.8e-06),
     'neutron mass in u': (1.00866491588, 'u', 4.9e-10),
     'neutron molar mass': (0.00100866491588, 'kg mol^-1', 4.9e-13),
     'neutron-muon mass ratio': (8.89248408, '', 2e-07),
     'neutron-proton mag. mom. ratio': (-0.68497934, '', 1.6e-07),
     'neutron-proton mass ratio': (1.00137841898, '', 5.1e-10),
     'neutron-tau mass ratio': (0.52879, '', 4.8e-05),
     'neutron to shielded proton mag. mom. ratio': (-0.68499694, '', 1.6e-07),
     'Newtonian constant of gravitation': (6.67408e-11, 'm^3 kg^-1 s^-2', 3.1e-15),
     'Newtonian constant of gravitation over h-bar c': (6.70861e-39,
      '(GeV/c^2)^-2',
      3.1e-43),
     'nuclear magneton': (5.050783699e-27, 'J T^-1', 3.1e-35),
     'nuclear magneton in eV/T': (3.152451255e-08, 'eV T^-1', 1.5e-17),
     'nuclear magneton in inverse meters per tesla': (0.02542623432,
      'm^-1 T^-1',
      1.6e-10),
     'nuclear magneton in K/T': (0.0003658269, 'K T^-1', 2.1e-10),
     'nuclear magneton in MHz/T': (7.622593285, 'MHz T^-1', 4.7e-08),
     'Planck constant': (6.62607004e-34, 'J s', 8.1e-42),
     'Planck constant in eV s': (4.135667662e-15, 'eV s', 2.5e-23),
     'Planck constant over 2 pi': (1.0545718e-34, 'J s', 1.3e-42),
     'Planck constant over 2 pi in eV s': (6.582119514e-16, 'eV s', 4e-24),
     'Planck constant over 2 pi times c in MeV fm': (197.3269788,
      'MeV fm',
      1.2e-06),
     'Planck length': (1.616229e-35, 'm', 3.8e-40),
     'Planck mass': (2.17647e-08, 'kg', 5.1e-13),
     'Planck mass energy equivalent in GeV': (1.22091e+19,
      'GeV',
      290000000000000.0),
     'Planck temperature': (1.416808e+32, 'K', 3.3e+27),
     'Planck time': (5.39116e-44, 's', 1.3e-48),
     'proton charge to mass quotient': (95788332.26, 'C kg^-1', 0.59),
     'proton Compton wavelength': (1.32140985396e-15, 'm', 6.1e-25),
     'proton Compton wavelength over 2 pi': (2.10308910109e-16, 'm', 9.7e-26),
     'proton-electron mass ratio': (1836.15267389, '', 1.7e-07),
     'proton g factor': (5.585694702, '', 1.7e-08),
     'proton gyromag. ratio': (267522190.0, 's^-1 T^-1', 1.8),
     'proton gyromag. ratio over 2 pi': (42.57747892, 'MHz T^-1', 2.9e-07),
     'proton mag. mom.': (1.4106067873e-26, 'J T^-1', 9.7e-35),
     'proton mag. mom. to Bohr magneton ratio': (0.0015210322053, '', 4.6e-12),
     'proton mag. mom. to nuclear magneton ratio': (2.7928473508, '', 8.5e-09),
     'proton mag. shielding correction': (2.5691e-05, '', 1.1e-08),
     'proton mass': (1.672621898e-27, 'kg', 2.1e-35),
     'proton mass energy equivalent': (1.503277593e-10, 'J', 1.8e-18),
     'proton mass energy equivalent in MeV': (938.2720813, 'MeV', 5.8e-06),
     'proton mass in u': (1.007276466879, 'u', 9.1e-11),
     'proton molar mass': (0.001007276466879, 'kg mol^-1', 9.1e-14),
     'proton-muon mass ratio': (8.88024338, '', 2e-07),
     'proton-neutron mag. mom. ratio': (-1.45989805, '', 3.4e-07),
     'proton-neutron mass ratio': (0.99862347844, '', 5.1e-10),
     'proton rms charge radius': (8.751e-16, 'm', 6.1e-18),
     'proton-tau mass ratio': (0.528063, '', 4.8e-05),
     'quantum of circulation': (0.00036369475486, 'm^2 s^-1', 1.7e-13),
     'quantum of circulation times 2': (0.00072738950972, 'm^2 s^-1', 3.3e-13),
     'Rydberg constant': (10973731.568508, 'm^-1', 6.5e-05),
     'Rydberg constant times c in Hz': (3289841960355000.0, 'Hz', 19000.0),
     'Rydberg constant times hc in eV': (13.605693009, 'eV', 8.4e-08),
     'Rydberg constant times hc in J': (2.179872325e-18, 'J', 2.7e-26),
     'Sackur-Tetrode constant (1 K, 100 kPa)': (-1.1517084, '', 1.4e-06),
     'Sackur-Tetrode constant (1 K, 101.325 kPa)': (-1.1648714, '', 1.4e-06),
     'second radiation constant': (0.0143877736, 'm K', 8.3e-09),
     'shielded helion gyromag. ratio': (203789458.5, 's^-1 T^-1', 2.7),
     'shielded helion gyromag. ratio over 2 pi': (32.43409966,
      'MHz T^-1',
      4.3e-07),
     'shielded helion mag. mom.': (-1.07455308e-26, 'J T^-1', 1.4e-34),
     'shielded helion mag. mom. to Bohr magneton ratio': (-0.001158671471,
      '',
      1.4e-11),
     'shielded helion mag. mom. to nuclear magneton ratio': (-2.12749772,
      '',
      2.5e-08),
     'shielded helion to proton mag. mom. ratio': (-0.7617665603, '', 9.2e-09),
     'shielded helion to shielded proton mag. mom. ratio': (-0.7617861313,
      '',
      3.3e-09),
     'shielded proton gyromag. ratio': (267515317.1, 's^-1 T^-1', 3.3),
     'shielded proton gyromag. ratio over 2 pi': (42.57638507,
      'MHz T^-1',
      5.3e-07),
     'shielded proton mag. mom.': (1.410570547e-26, 'J T^-1', 1.8e-34),
     'shielded proton mag. mom. to Bohr magneton ratio': (0.001520993128,
      '',
      1.7e-11),
     'shielded proton mag. mom. to nuclear magneton ratio': (2.7927756, '', 3e-08),
     'speed of light in vacuum': (299792458.0, 'm s^-1', 0.0),
     'standard acceleration of gravity': (9.80665, 'm s^-2', 0.0),
     'standard atmosphere': (101325.0, 'Pa', 0.0),
     'Stefan-Boltzmann constant': (5.670367e-08, 'W m^-2 K^-4', 1.3e-13),
     'tau Compton wavelength': (6.97787e-16, 'm', 6.3e-20),
     'tau Compton wavelength over 2 pi': (1.11056e-16, 'm', 1e-20),
     'tau-electron mass ratio': (3477.15, '', 0.31),
     'tau mass': (3.16747e-27, 'kg', 2.9e-31),
     'tau mass energy equivalent': (2.84678e-10, 'J', 2.6e-14),
     'tau mass energy equivalent in MeV': (1776.82, 'MeV', 0.16),
     'tau mass in u': (1.90749, 'u', 0.00017),
     'tau molar mass': (0.00190749, 'kg mol^-1', 1.7e-07),
     'tau-muon mass ratio': (16.8167, '', 0.0015),
     'tau-neutron mass ratio': (1.89111, '', 0.00017),
     'tau-proton mass ratio': (1.89372, '', 0.00017),
     'Thomson cross section': (6.6524587158e-29, 'm^2', 9.1e-38),
     'triton-electron mag. mom. ratio': (-0.001620514423, '', 2.1e-11),
     'triton-electron mass ratio': (5496.92153588, '', 2.6e-07),
     'triton g factor': (5.95792492, '', 2.8e-08),
     'triton mag. mom.': (1.504609503e-26, 'J T^-1', 1.2e-34),
     'triton mag. mom. to Bohr magneton ratio': (0.0016223936616, '', 7.6e-12),
     'triton mag. mom. to nuclear magneton ratio': (2.97896246, '', 1.4e-08),
     'triton mass': (5.007356665e-27, 'kg', 6.2e-35),
     'triton mass energy equivalent': (4.500387735e-10, 'J', 5.5e-18),
     'triton mass energy equivalent in MeV': (2808.921112, 'MeV', 1.7e-05),
     'triton mass in u': (3.01550071632, 'u', 1.1e-10),
     'triton molar mass': (0.00301550071632, 'kg mol^-1', 1.1e-13),
     'triton-neutron mag. mom. ratio': (-1.55718553, '', 3.7e-07),
     'triton-proton mag. mom. ratio': (1.066639908, '', 1e-08),
     'triton-proton mass ratio': (2.99371703348, '', 2.2e-10),
     'unified atomic mass unit': (1.66053904e-27, 'kg', 2e-35),
     'von Klitzing constant': (25812.8074555, 'ohm', 5.9e-06),
     'weak mixing angle': (0.2223, '', 0.0021),
     'Wien frequency displacement law constant': (58789238000.0,
      'Hz K^-1',
      34000.0),
     'Wien wavelength displacement law constant': (0.0028977729, 'm K', 1.7e-09),
     'atomic unit of mom.um': (1.992851882e-24, 'kg m s^-1', 2.4e-32),
     'electron-helion mass ratio': (0.0001819543074854, '', 8.8e-15),
     'electron-triton mass ratio': (0.0001819200062203, '', 8.4e-15),
     'helion g factor': (-4.255250616, '', 5e-08),
     'helion mag. mom.': (-1.074617522e-26, 'J T^-1', 1.4e-34),
     'helion mag. mom. to Bohr magneton ratio': (-0.001158740958, '', 1.4e-11),
     'helion mag. mom. to nuclear magneton ratio': (-2.127625308, '', 2.5e-08),
     'Loschmidt constant (273.15 K, 100 kPa)': (2.6516467e+25, 'm^-3', 1.5e+19),
     'natural unit of mom.um': (2.730924488e-22, 'kg m s^-1', 3.4e-30),
     'natural unit of mom.um in MeV/c': (0.5109989461, 'MeV/c', 3.1e-09),
     'neutron-proton mass difference': (2.30557377e-30, '', 8.5e-37),
     'neutron-proton mass difference energy equivalent': (2.07214637e-13,
      '',
      7.6e-20),
     'neutron-proton mass difference energy equivalent in MeV': (1.29333205,
      '',
      4.8e-07),
     'neutron-proton mass difference in u': (0.001388449, '', 5.1e-10),
     'standard-state pressure': (100000.0, 'Pa', 0.0)}

### Achtung¶

Wenn solche Konstanten genutzt werden, muss das korrekt mitgeteilt, also zitiert werden. Darauf gehen wir nächste Woche im LaTeX-Workshop ein :-)

(Quelle hier: python-scipy)

In [7]:

    const.physical_constants["proton mass"]
    # value, unit, error

Out[7]:

    (1.672621898e-27, 'kg', 2.1e-35)

## Fitten¶

Oft möchte man eine Funktion, zum Beispiel eine Erwartung aus der Theorie, an die gemessenen Werte anpassen. Dies nennt man Fit.

Die Funktion `scipy.optimize.curve_fit` nutzt die numerische Methode der kleinsten Quadrate, die arbiträre Funktionen fitten kann. Für Funktionen, die eine Linearkombination von Einzelfunktionen sind, also

$$ f(x) = \sum_i^N a_i \cdot f_i(x) $$

existiert eine analytische Lösung. Deswegen sollten in solchen Fällen (z.B. alle Polynome) entsprechende Funktionen genutzt werden (z.B. `np.polyfit`)

### Lineare Regression bzw. Polynome¶

In [8]:

    # prepare plot
    %matplotlib inline
    import matplotlib.pyplot as plt
    plt.rcParams['figure.figsize'] = (10, 8)
    plt.rcParams['font.size'] = 16

    # load data
    x, y = np.genfromtxt('example_data_linear.txt', unpack=True)

    plt.plot(x, y, 'k.', label="example data")

Out[8]:

    [<matplotlib.lines.Line2D at 0x7f556bb59c50>]

In [9]:

    # Fit a polynomial of degree 1, return covariance matrix
    params, covariance_matrix = np.polyfit(x, y, deg=1, cov=True)

    errors = np.sqrt(np.diag(covariance_matrix))

    print('a = {:.3f} ± {:.3f}'.format(params[0], errors[0]))
    print('b = {:.3f} ± {:.3f}'.format(params[1], errors[1]))

    a = 4.977 ± 0.021
    b = 0.070 ± 0.124

In [10]:

    x_plot = np.linspace(0, 10)

    plt.plot(x, y, '.', label="Messwerte")
    plt.plot(x_plot, params[0] * x_plot + params[1], label='Lineare Regression', linewidth=3)
    plt.legend(loc="best")

Out[10]:

    <matplotlib.legend.Legend at 0x7f556be0cf98>

### Nichtlineare Funktionen der Parameter¶

Wenn eine Funktion nicht linear bezüglich der freien Parameter ist, muss die Lösung numerisch gefunden werden.

Hierbei kann es sein, dass der Minimierungsalgorithmus in lokale Maxima hineinläuft und unsinnige Ergebnisse liefert, dies kann mit guten Startwerten meistens vermieden werden.

## Fit einer komplexeren Funktion: Sigmoidfunktion¶

(Ähnlich zum `tanh`)

$$ f(x; a, b, c) = \frac{a}{1 + \exp(-(x-b))} + c$$

In [11]:

    def sigmoid(x, a, b, c):
        return a / (1 + np.exp(-(x - b))) + c

    x_plot = np.linspace(-50, 50, 1000)

    plt.xlabel('x')
    plt.ylabel('y')

    plt.plot(x_plot, sigmoid(x_plot, 1, 0, 0), label="Sigmoid")
    plt.plot(x_plot, np.tanh(x_plot), label="tanh")
    plt.legend()

Out[11]:

    <matplotlib.legend.Legend at 0x7f556bb45e80>

Die Messwerte aus einem Praktikumsversuch:

In [12]:

    x, y = np.loadtxt('fit_data_with_init_values.txt', unpack=True)

    plt.plot(x, y, 'ro', label=r'Messwerte')

    plt.xlabel('Temperatur / °C')
    plt.ylabel('$GP$')

Out[12]:

    Text(0,0.5,'$GP$')

Ein einfacher Fit wie oben funktioniert hier nicht so gut:

In [13]:

    from scipy.optimize import curve_fit
    params, covariance_matrix = curve_fit(sigmoid, x, y)

    errors = np.sqrt(np.diag(covariance_matrix))

    print('a =', params[0], '±', errors[0])
    print('b =', params[1], '±', errors[1])

    a = -307.05591617836905 ± inf
    b = 1.0 ± inf

    /home/maxnoe/.local/anaconda3/lib/python3.6/site-packages/scipy/optimize/minpack.py:794: OptimizeWarning: Covariance of the parameters could not be estimated
      category=OptimizeWarning)

Schaut man sich die berechnete Ausgleichskurve an sieht man auch,
dass das nicht stimmen kann:

In [14]:

    plt.xlabel('Temperatur / °C')
    plt.ylabel('$GP$')

    plt.plot(x, y, 'ro', label='Messdaten')
    plt.plot(x_plot, sigmoid(x_plot, *params), "b-", label=r'Sigmoid Fit')

    plt.legend(loc='best')

Out[14]:

    <matplotlib.legend.Legend at 0x7f556ba41f98>

Was macht man jetzt?
Bei solchen Fragen hilft die Dokumentation der Pythonmodule (hier: scipy) oder Stackoverflow weiter.
Folgendes Google-Muster ist ein guter Anfang (beachte englische Sprache):

    python <module-name> <function-name> <What went wrong?>

Also in diesem Fall: `python scipy curve_fit fails`

Damit dieser Fit funktioniert müssen die Startwerte für den internen
Minimierungsalgorithmus angepasst werden.
Aus der Dokumentation/Stackoverflow wissen wir jetzt, dass man mit dem
keyword argument `p0` (Standardwert is `p0=(1,1,1)`) die Startwerte einstellt:

In [15]:

    params, covariance_matrix = curve_fit(sigmoid, x, y, p0=(-1, 40, 1))

    errors = np.sqrt(np.diag(covariance_matrix))

    print('a =', params[0], '±', errors[0])
    print('b =', params[1], '±', errors[1])

    a = -0.4939355813626815 ± 0.013853829784760135
    b = 40.667553529388165 ± 0.13696084283475848

In [16]:

    plt.xlabel('Temperatur / °C')
    plt.ylabel('$GP$')

    x_plot = np.linspace(0, 50, 1000)

    plt.plot(x, y, 'ro', label='Messwerte')
    plt.plot(x_plot, sigmoid(x_plot, *params), "b-", label='Sigmoid Fit')

    plt.legend(loc='best')

Out[16]:

    <matplotlib.legend.Legend at 0x7f556b0d3860>

Zum Vergleich der beiden Anfangswerte (seeds) kann man sich die einmal ansehen
und mit den angepassten Parametern vergleichen:

In [17]:

    default_seed = (1,1,1)
    good_seed = (-1,40,1)

    parameter = [default_seed, good_seed, params]

    x_plot = np.linspace(-80, 80, 1000)

    for p in parameter:
        plt.plot(x_plot, sigmoid(x_plot, *p),  label="f(x; {0:0.3f}, {1:0.3f}, {2:0.3f})".format(*p))

    plt.legend()

Out[17]:

    <matplotlib.legend.Legend at 0x7f556b10b390>

Die richtigen Startwerte findet man entweder durch

1. trial and error => einfach ausprobieren bis es klappt

2. nachdenken => siehe unten

Im obigen Beispiel musste nur Parameter `b` angepasst werden,
weil der für die Form der Kurve sehr wichtig ist.

$$ f(x; a, b, c) = \frac{a}{1 + \exp(-(x-b))} + c$$

In [18]:

    B = [0, 20, 40]

    x_plot = np.linspace(-50, 50, 1000)

    plt.xlabel('x')
    plt.ylabel('y')

    for b in B:

        line = plt.plot(x_plot, sigmoid(x_plot, 1, b, 0),  label=f"f(x; 1, {b}, 0)")
        plt.plot(b, sigmoid(b, 1, b, 0), "o", color=line[0].get_color(), ms=20, label=f"f(x={b}) = {sigmoid(b, 1, b, 0)}")

    plt.legend()

Out[18]:

    <matplotlib.legend.Legend at 0x7f556b005cf8>

Der Parameter $b$ gibt den $x$-Wert an bei dem die Funktion auf die Hälfte des Maximums abgefallen ist.
Bei den Messwerten oben ist die Stelle ungefähr bei $x=40$ also ist `b=40` ein guter Startwert.

Das lässt sich auch automatisieren:

In [19]:

    plt.plot(x, y, 'o')

    idx = np.argmin(np.abs(y - 0.5))

    plt.plot(x[idx], y[idx], 'o')

Out[19]:

    [<matplotlib.lines.Line2D at 0x7f556af83550>]