# 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]:

    %%javascript
    $.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')

## Inhalt¶

## Abrufen von Naturkonstanten¶

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

(Quelle hier: scipy + version)

In [2]:

    import numpy as np
    # physical constants
    import scipy.constants as const

    const.epsilon_0

Out[2]:

    8.8541878128e-12

In [3]:

    # a list of all included constants
    # search the web for "scipy physical constants reference guide" to find the same list
    const.physical_constants

Out[3]:

    {'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.756758, 'T', 7.1e-05),
     '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': (176085963023.0, 's^-1 T^-1', 53.0),
     'electron gyromagn. ratio over 2 pi': (28024.9532, 'MHz T^-1', 0.0024),
     '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.2566370614e-06, 'N A^-2', 0.0),
     'magn. flux quantum': (2.067833848e-15, 'Wb', 0.0),
     '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': (183247171.0, 's^-1 T^-1', 43.0),
     'neutron gyromagn. ratio over 2 pi': (29.164695, 'MHz T^-1', 7.3e-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': (267522187.44, 's^-1 T^-1', 0.11),
     'proton gyromagn. ratio over 2 pi': (42.5774813, 'MHz T^-1', 3.7e-06),
     '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.5689e-05, '', 1.1e-08),
     'proton-neutron magn. moment ratio': (-1.45989805, '', 3.4e-07),
     'shielded helion gyromagn. ratio': (203789456.9, 's^-1 T^-1', 2.4),
     'shielded helion gyromagn. ratio over 2 pi': (32.4341015,
      'MHz T^-1',
      2.8e-06),
     '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.29954142, '', 2.4e-07),
     'alpha particle mass': (6.6446573357e-27, 'kg', 2e-36),
     'alpha particle mass energy equivalent': (5.9719201914e-10, 'J', 1.8e-19),
     'alpha particle mass energy equivalent in MeV': (3727.3794066,
      'MeV',
      1.1e-06),
     'alpha particle mass in u': (4.001506179127, 'u', 6.3e-11),
     'alpha particle molar mass': (0.0040015061777, 'kg mol^-1', 1.2e-12),
     'alpha particle-proton mass ratio': (3.97259969009, '', 2.2e-10),
     'Angstrom star': (1.00001495e-10, 'm', 9e-17),
     'atomic mass constant': (1.6605390666e-27, 'kg', 5e-37),
     'atomic mass constant energy equivalent': (1.4924180856e-10, 'J', 4.5e-20),
     'atomic mass constant energy equivalent in MeV': (931.49410242,
      'MeV',
      2.8e-07),
     'atomic mass unit-electron volt relationship': (931494102.42, 'eV', 0.28),
     'atomic mass unit-hartree relationship': (34231776.874, 'E_h', 0.01),
     'atomic mass unit-hertz relationship': (2.25234271871e+23,
      'Hz',
      68000000000000.0),
     'atomic mass unit-inverse meter relationship': (751300661040000.0,
      'm^-1',
      230000.0),
     'atomic mass unit-joule relationship': (1.4924180856e-10, 'J', 4.5e-20),
     'atomic mass unit-kelvin relationship': (10809540191600.0, 'K', 3300.0),
     'atomic mass unit-kilogram relationship': (1.6605390666e-27, 'kg', 5e-37),
     'atomic unit of 1st hyperpolarizability': (3.2063613061e-53,
      'C^3 m^3 J^-2',
      1.5e-62),
     'atomic unit of 2nd hyperpolarizability': (6.2353799905e-65,
      'C^4 m^4 J^-3',
      3.8e-74),
     'atomic unit of action': (1.054571817e-34, 'J s', 0.0),
     'atomic unit of charge': (1.602176634e-19, 'C', 0.0),
     'atomic unit of charge density': (1081202384570.0, 'C m^-3', 490.0),
     'atomic unit of current': (0.00662361823751, 'A', 1.3e-14),
     'atomic unit of electric dipole mom.': (8.4783536255e-30, 'C m', 1.3e-39),
     'atomic unit of electric field': (514220674763.0, 'V m^-1', 78.0),
     'atomic unit of electric field gradient': (9.7173624292e+21,
      'V m^-2',
      2900000000000.0),
     'atomic unit of electric polarizability': (1.64877727436e-41,
      'C^2 m^2 J^-1',
      5e-51),
     'atomic unit of electric potential': (27.211386245988, 'V', 5.3e-11),
     'atomic unit of electric quadrupole mom.': (4.4865515246e-40,
      'C m^2',
      1.4e-49),
     'atomic unit of energy': (4.3597447222071e-18, 'J', 8.5e-30),
     'atomic unit of force': (8.2387234983e-08, 'N', 1.2e-17),
     'atomic unit of length': (5.29177210903e-11, 'm', 8e-21),
     'atomic unit of mag. dipole mom.': (1.85480201566e-23, 'J T^-1', 5.6e-33),
     'atomic unit of mag. flux density': (235051.756758, 'T', 7.1e-05),
     'atomic unit of magnetizability': (7.8910366008e-29, 'J T^-2', 4.8e-38),
     'atomic unit of mass': (9.1093837015e-31, 'kg', 2.8e-40),
     'atomic unit of momentum': (1.9928519141e-24, 'kg m s^-1', 3e-34),
     'atomic unit of permittivity': (1.11265005545e-10, 'F m^-1', 1.7e-20),
     'atomic unit of time': (2.4188843265857e-17, 's', 4.7e-29),
     'atomic unit of velocity': (2187691.26364, 'm s^-1', 0.00033),
     'Avogadro constant': (6.02214076e+23, 'mol^-1', 0.0),
     'Bohr magneton': (9.2740100783e-24, 'J T^-1', 2.8e-33),
     'Bohr magneton in eV/T': (5.788381806e-05, 'eV T^-1', 1.7e-14),
     'Bohr magneton in Hz/T': (13996244936.1, 'Hz T^-1', 4.2),
     'Bohr magneton in inverse meters per tesla': (46.68644814,
      'm^-1 T^-1',
      2.9e-07),
     'Bohr magneton in K/T': (0.67171381563, 'K T^-1', 2e-10),
     'Bohr radius': (5.29177210903e-11, 'm', 8e-21),
     'Boltzmann constant': (1.380649e-23, 'J K^-1', 0.0),
     'Boltzmann constant in eV/K': (8.617333262e-05, 'eV K^-1', 0.0),
     'Boltzmann constant in Hz/K': (20836619120.0, 'Hz K^-1', 0.0),
     'Boltzmann constant in inverse meters per kelvin': (69.503457,
      'm^-1 K^-1',
      4e-05),
     'characteristic impedance of vacuum': (376.73031366686166,
      'ohm',
      5.61366546036269e-08),
     'classical electron radius': (2.8179403262e-15, 'm', 1.3e-24),
     'Compton wavelength': (2.42631023867e-12, 'm', 7.3e-22),
     'Compton wavelength over 2 pi': (3.8615926764e-13, 'm', 1.8e-22),
     'conductance quantum': (7.748091729e-05, 'S', 0.0),
     '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.0004664345551, '', 1.2e-12),
     'deuteron-electron mass ratio': (3670.48296788, '', 1.3e-07),
     'deuteron g factor': (0.8574382338, '', 2.2e-09),
     'deuteron mag. mom.': (4.330735094e-27, 'J T^-1', 1.1e-35),
     'deuteron mag. mom. to Bohr magneton ratio': (0.000466975457, '', 1.2e-12),
     'deuteron mag. mom. to nuclear magneton ratio': (0.8574382338, '', 2.2e-09),
     'deuteron mass': (3.3435837724e-27, 'kg', 1e-36),
     'deuteron mass energy equivalent': (3.00506323102e-10, 'J', 9.1e-20),
     'deuteron mass energy equivalent in MeV': (1875.61294257, 'MeV', 5.7e-07),
     'deuteron mass in u': (2.013553212745, 'u', 4e-11),
     'deuteron molar mass': (0.00201355321205, 'kg mol^-1', 6.1e-13),
     'deuteron-neutron mag. mom. ratio': (-0.44820653, '', 1.1e-07),
     'deuteron-proton mag. mom. ratio': (0.30701220939, '', 7.9e-10),
     'deuteron-proton mass ratio': (1.99900750139, '', 1.1e-10),
     'deuteron rms charge radius': (2.12799e-15, 'm', 7.4e-19),
     'electric constant': (8.8541878128e-12, 'F m^-1', 1.3e-21),
     'electron charge to mass quotient': (-175882001076.0, 'C kg^-1', 53.0),
     'electron-deuteron mag. mom. ratio': (-2143.9234915, '', 5.6e-06),
     'electron-deuteron mass ratio': (0.0002724437107462, '', 9.6e-15),
     'electron g factor': (-2.00231930436256, '', 3.5e-13),
     'electron gyromag. ratio': (176085963023.0, 's^-1 T^-1', 53.0),
     'electron gyromag. ratio over 2 pi': (28024.95164, 'MHz T^-1', 0.00017),
     'electron mag. mom.': (-9.2847647043e-24, 'J T^-1', 2.8e-33),
     'electron mag. mom. anomaly': (0.00115965218128, '', 1.8e-13),
     'electron mag. mom. to Bohr magneton ratio': (-1.00115965218128, '', 1.8e-13),
     'electron mag. mom. to nuclear magneton ratio': (-1838.28197188, '', 1.1e-07),
     'electron mass': (9.1093837015e-31, 'kg', 2.8e-40),
     'electron mass energy equivalent': (8.1871057769e-14, 'J', 2.5e-23),
     'electron mass energy equivalent in MeV': (0.51099895, 'MeV', 1.5e-10),
     'electron mass in u': (0.000548579909065, 'u', 1.6e-14),
     'electron molar mass': (5.4857990888e-07, 'kg mol^-1', 1.7e-16),
     'electron-muon mag. mom. ratio': (206.7669883, '', 4.6e-06),
     'electron-muon mass ratio': (0.00483633169, '', 1.1e-10),
     'electron-neutron mag. mom. ratio': (960.9205, '', 0.00023),
     'electron-neutron mass ratio': (0.00054386734424, '', 2.6e-13),
     'electron-proton mag. mom. ratio': (-658.21068789, '', 2e-07),
     'electron-proton mass ratio': (0.000544617021487, '', 3.3e-14),
     'electron-tau mass ratio': (0.000287585, '', 1.9e-08),
     'electron to alpha particle mass ratio': (0.0001370933554787, '', 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.602176634e-19, 'J', 0.0),
     'electron volt-atomic mass unit relationship': (1.07354410233e-09,
      'u',
      3.2e-19),
     'electron volt-hartree relationship': (0.036749322175655, 'E_h', 7.1e-14),
     'electron volt-hertz relationship': (241798924200000.0, 'Hz', 0.0),
     'electron volt-inverse meter relationship': (806554.3937, 'm^-1', 0.0),
     'electron volt-joule relationship': (1.602176634e-19, 'J', 0.0),
     'electron volt-kelvin relationship': (11604.51812, 'K', 0.0),
     'electron volt-kilogram relationship': (1.782661921e-36, 'kg', 0.0),
     'elementary charge': (1.602176634e-19, 'C', 0.0),
     'elementary charge over h': (241798926200000.0, 'A J^-1', 1500000.0),
     'Faraday constant': (96485.33212, 'C mol^-1', 0.0),
     '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.0072973525693, '', 1.1e-12),
     'first radiation constant': (3.741771852e-16, 'W m^2', 0.0),
     'first radiation constant for spectral radiance': (1.191042972e-16,
      'W m^2 sr^-1',
      0.0),
     'hartree-atomic mass unit relationship': (2.92126232205e-08, 'u', 8.8e-18),
     'hartree-electron volt relationship': (27.211386245988, 'eV', 5.3e-11),
     'Hartree energy': (4.3597447222071e-18, 'J', 8.5e-30),
     'Hartree energy in eV': (27.211386245988, 'eV', 5.3e-11),
     'hartree-hertz relationship': (6579683920502000.0, 'Hz', 13000.0),
     'hartree-inverse meter relationship': (21947463.13632, 'm^-1', 4.3e-05),
     'hartree-joule relationship': (4.3597447222071e-18, 'J', 8.5e-30),
     'hartree-kelvin relationship': (315775.02480407, 'K', 6.1e-07),
     'hartree-kilogram relationship': (4.8508702095432e-35, 'kg', 9.4e-47),
     'helion-electron mass ratio': (5495.88528007, '', 2.4e-07),
     'helion mass': (5.0064127796e-27, 'kg', 1.5e-36),
     'helion mass energy equivalent': (4.4995394125e-10, 'J', 1.4e-19),
     'helion mass energy equivalent in MeV': (2808.39160743, 'MeV', 8.5e-07),
     'helion mass in u': (3.014932247175, 'u', 9.7e-11),
     'helion molar mass': (0.00301493224613, 'kg mol^-1', 9.1e-13),
     'helion-proton mass ratio': (2.99315267167, '', 1.3e-10),
     'hertz-atomic mass unit relationship': (4.4398216652e-24, 'u', 1.3e-33),
     'hertz-electron volt relationship': (4.135667696e-15, 'eV', 0.0),
     'hertz-hartree relationship': (1.519829846057e-16, 'E_h', 2.9e-28),
     'hertz-inverse meter relationship': (3.3356409519815204e-09, 'm^-1', 0.0),
     'hertz-joule relationship': (6.62607015e-34, 'J', 0.0),
     'hertz-kelvin relationship': (4.799243073e-11, 'K', 0.0),
     'hertz-kilogram relationship': (7.372497323e-51, 'kg', 0.0),
     'inverse fine-structure constant': (137.035999084, '', 2.1e-08),
     'inverse meter-atomic mass unit relationship': (1.3310250501e-15, 'u', 4e-25),
     'inverse meter-electron volt relationship': (1.239841984e-06, 'eV', 0.0),
     'inverse meter-hartree relationship': (4.556335252912e-08, 'E_h', 8.8e-20),
     'inverse meter-hertz relationship': (299792458.0, 'Hz', 0.0),
     'inverse meter-joule relationship': (1.986445857e-25, 'J', 0.0),
     'inverse meter-kelvin relationship': (0.01438776877, 'K', 0.0),
     'inverse meter-kilogram relationship': (2.210219094e-42, 'kg', 0.0),
     'inverse of conductance quantum': (12906.40372, 'ohm', 0.0),
     'Josephson constant': (483597848400000.0, 'Hz V^-1', 0.0),
     'joule-atomic mass unit relationship': (6700535256.5, 'u', 2.0),
     'joule-electron volt relationship': (6.241509074e+18, 'eV', 0.0),
     'joule-hartree relationship': (2.2937122783963e+17, 'E_h', 450000.0),
     'joule-hertz relationship': (1.509190179e+33, 'Hz', 0.0),
     'joule-inverse meter relationship': (5.034116567e+24, 'm^-1', 0.0),
     'joule-kelvin relationship': (7.242970516e+22, 'K', 0.0),
     'joule-kilogram relationship': (1.1126500560536185e-17, 'kg', 0.0),
     'kelvin-atomic mass unit relationship': (9.2510873014e-14, 'u', 2.8e-23),
     'kelvin-electron volt relationship': (8.617333262e-05, 'eV', 0.0),
     'kelvin-hartree relationship': (3.1668115634556e-06, 'E_h', 6.1e-18),
     'kelvin-hertz relationship': (20836619120.0, 'Hz', 0.0),
     'kelvin-inverse meter relationship': (69.50348004, 'm^-1', 0.0),
     'kelvin-joule relationship': (1.380649e-23, 'J', 0.0),
     'kelvin-kilogram relationship': (1.536179187e-40, 'kg', 0.0),
     'kilogram-atomic mass unit relationship': (6.0221407621e+26, 'u', 1.8e+17),
     'kilogram-electron volt relationship': (5.609588603e+35, 'eV', 0.0),
     'kilogram-hartree relationship': (2.0614857887409e+34, 'E_h', 4e+22),
     'kilogram-hertz relationship': (1.356392489e+50, 'Hz', 0.0),
     'kilogram-inverse meter relationship': (4.524438335e+41, 'm^-1', 0.0),
     'kilogram-joule relationship': (8.987551787368176e+16, 'J', 0.0),
     'kilogram-kelvin relationship': (6.50965726e+39, 'K', 0.0),
     'lattice parameter of silicon': (5.431020511e-10, 'm', 8.9e-18),
     'Loschmidt constant (273.15 K, 101.325 kPa)': (2.686780111e+25, 'm^-3', 0.0),
     'mag. constant': (1.25663706212e-06, 'N A^-2', 1.9e-16),
     'mag. flux quantum': (2.067833848e-15, 'Wb', 0.0),
     'molar gas constant': (8.314462618, 'J mol^-1 K^-1', 0.0),
     'molar mass constant': (0.00099999999965, 'kg mol^-1', 3e-13),
     'molar mass of carbon-12': (0.0119999999958, 'kg mol^-1', 3.6e-12),
     'molar Planck constant': (3.990312712e-10, 'J Hz^-1 mol^-1', 0.0),
     '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.02271095464,
      'm^3 mol^-1',
      0.0),
     'molar volume of ideal gas (273.15 K, 101.325 kPa)': (0.02241396954,
      'm^3 mol^-1',
      0.0),
     'molar volume of silicon': (1.205883199e-05, 'm^3 mol^-1', 6e-13),
     'Mo x unit': (1.00209952e-13, 'm', 5.3e-20),
     'muon Compton wavelength': (1.17344411e-14, 'm', 2.6e-22),
     'muon Compton wavelength over 2 pi': (1.867594308e-15, 'm', 4.2e-23),
     'muon-electron mass ratio': (206.768283, '', 4.6e-06),
     'muon g factor': (-2.0023318418, '', 1.3e-09),
     'muon mag. mom.': (-4.4904483e-26, 'J T^-1', 1e-33),
     'muon mag. mom. anomaly': (0.00116592089, '', 6.3e-10),
     'muon mag. mom. to Bohr magneton ratio': (-0.00484197047, '', 1.1e-10),
     'muon mag. mom. to nuclear magneton ratio': (-8.89059703, '', 2e-07),
     'muon mass': (1.883531627e-28, 'kg', 4.2e-36),
     'muon mass energy equivalent': (1.692833804e-11, 'J', 3.8e-19),
     'muon mass energy equivalent in MeV': (105.6583755, 'MeV', 2.3e-06),
     'muon mass in u': (0.1134289259, 'u', 2.5e-09),
     'muon molar mass': (0.0001134289259, 'kg mol^-1', 2.5e-12),
     'muon-neutron mass ratio': (0.112454517, '', 2.5e-09),
     'muon-proton mag. mom. ratio': (-3.183345142, '', 7.1e-08),
     'muon-proton mass ratio': (0.1126095264, '', 2.5e-09),
     'muon-tau mass ratio': (0.0594635, '', 4e-06),
     'natural unit of action': (1.054571817e-34, 'J s', 0.0),
     'natural unit of action in eV s': (6.582119569e-16, 'eV s', 0.0),
     'natural unit of energy': (8.1871057769e-14, 'J', 2.5e-23),
     'natural unit of energy in MeV': (0.51099895, 'MeV', 1.5e-10),
     'natural unit of length': (3.8615926796e-13, 'm', 1.2e-22),
     'natural unit of mass': (9.1093837015e-31, 'kg', 2.8e-40),
     '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.28808866819e-21, 's', 3.9e-31),
     'natural unit of velocity': (299792458.0, 'm s^-1', 0.0),
     'neutron Compton wavelength': (1.31959090581e-15, 'm', 7.5e-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.68366173, '', 8.9e-07),
     'neutron g factor': (-3.82608545, '', 9e-07),
     'neutron gyromag. ratio': (183247171.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.6623651e-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.67492749804e-27, 'kg', 9.5e-37),
     'neutron mass energy equivalent': (1.50534976287e-10, 'J', 8.6e-20),
     'neutron mass energy equivalent in MeV': (939.56542052, 'MeV', 5.4e-07),
     'neutron mass in u': (1.00866491595, 'u', 4.9e-10),
     'neutron molar mass': (0.0010086649156, 'kg mol^-1', 5.7e-13),
     'neutron-muon mass ratio': (8.89248406, '', 2e-07),
     'neutron-proton mag. mom. ratio': (-0.68497934, '', 1.6e-07),
     'neutron-proton mass ratio': (1.00137841931, '', 4.9e-10),
     'neutron-tau mass ratio': (0.528779, '', 3.6e-05),
     'neutron to shielded proton mag. mom. ratio': (-0.68499694, '', 1.6e-07),
     'Newtonian constant of gravitation': (6.6743e-11, 'm^3 kg^-1 s^-2', 1.5e-15),
     'Newtonian constant of gravitation over h-bar c': (6.70883e-39,
      '(GeV/c^2)^-2',
      1.5e-43),
     'nuclear magneton': (5.0507837461e-27, 'J T^-1', 1.5e-36),
     'nuclear magneton in eV/T': (3.15245125844e-08, 'eV T^-1', 9.6e-18),
     'nuclear magneton in inverse meters per tesla': (0.02542623432,
      'm^-1 T^-1',
      1.6e-10),
     'nuclear magneton in K/T': (0.00036582677756, 'K T^-1', 1.1e-13),
     'nuclear magneton in MHz/T': (7.6225932291, 'MHz T^-1', 2.3e-09),
     'Planck constant': (6.62607015e-34, 'J Hz^-1', 0.0),
     '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.616255e-35, 'm', 1.8e-40),
     'Planck mass': (2.176434e-08, 'kg', 2.4e-13),
     'Planck mass energy equivalent in GeV': (1.22089e+19,
      'GeV',
      140000000000000.0),
     'Planck temperature': (1.416784e+32, 'K', 1.6e+27),
     'Planck time': (5.391247e-44, 's', 6e-49),
     'proton charge to mass quotient': (95788331.56, 'C kg^-1', 0.029),
     'proton Compton wavelength': (1.32140985539e-15, 'm', 4e-25),
     'proton Compton wavelength over 2 pi': (2.10308910109e-16, 'm', 9.7e-26),
     'proton-electron mass ratio': (1836.15267343, '', 1.1e-07),
     'proton g factor': (5.5856946893, '', 1.6e-09),
     'proton gyromag. ratio': (267522187.44, 's^-1 T^-1', 0.11),
     'proton gyromag. ratio over 2 pi': (42.57747892, 'MHz T^-1', 2.9e-07),
     'proton mag. mom.': (1.41060679736e-26, 'J T^-1', 6e-36),
     'proton mag. mom. to Bohr magneton ratio': (0.0015210322023, '', 4.6e-13),
     'proton mag. mom. to nuclear magneton ratio': (2.79284734463, '', 8.2e-10),
     'proton mag. shielding correction': (2.5689e-05, '', 1.1e-08),
     'proton mass': (1.67262192369e-27, 'kg', 5.1e-37),
     'proton mass energy equivalent': (1.50327761598e-10, 'J', 4.6e-20),
     'proton mass energy equivalent in MeV': (938.27208816, 'MeV', 2.9e-07),
     'proton mass in u': (1.007276466621, 'u', 5.3e-11),
     'proton molar mass': (0.00100727646627, 'kg mol^-1', 3.1e-13),
     'proton-muon mass ratio': (8.88024337, '', 2e-07),
     'proton-neutron mag. mom. ratio': (-1.45989805, '', 3.4e-07),
     'proton-neutron mass ratio': (0.99862347812, '', 4.9e-10),
     'proton rms charge radius': (8.414e-16, 'm', 1.9e-18),
     'proton-tau mass ratio': (0.528051, '', 3.6e-05),
     'quantum of circulation': (0.00036369475516, 'm^2 s^-1', 1.1e-13),
     'quantum of circulation times 2': (0.00072738951032, 'm^2 s^-1', 2.2e-13),
     'Rydberg constant': (10973731.56816, 'm^-1', 2.1e-05),
     'Rydberg constant times c in Hz': (3289841960250800.0, 'Hz', 6400.0),
     'Rydberg constant times hc in eV': (13.605693122994, 'eV', 2.6e-11),
     'Rydberg constant times hc in J': (2.1798723611035e-18, 'J', 4.2e-30),
     'Sackur-Tetrode constant (1 K, 100 kPa)': (-1.15170753706, '', 4.5e-10),
     'Sackur-Tetrode constant (1 K, 101.325 kPa)': (-1.16487052358, '', 4.5e-10),
     'second radiation constant': (0.01438776877, 'm K', 0.0),
     'shielded helion gyromag. ratio': (203789456.9, 's^-1 T^-1', 2.4),
     'shielded helion gyromag. ratio over 2 pi': (32.43409966,
      'MHz T^-1',
      4.3e-07),
     'shielded helion mag. mom.': (-1.07455309e-26, 'J T^-1', 1.3e-34),
     'shielded helion mag. mom. to Bohr magneton ratio': (-0.001158671471,
      '',
      1.4e-11),
     'shielded helion mag. mom. to nuclear magneton ratio': (-2.127497719,
      '',
      2.5e-08),
     'shielded helion to proton mag. mom. ratio': (-0.7617665618, '', 8.9e-09),
     'shielded helion to shielded proton mag. mom. ratio': (-0.7617861313,
      '',
      3.3e-09),
     'shielded proton gyromag. ratio': (267515315.1, 's^-1 T^-1', 2.9),
     'shielded proton gyromag. ratio over 2 pi': (42.57638507,
      'MHz T^-1',
      5.3e-07),
     'shielded proton mag. mom.': (1.41057056e-26, 'J T^-1', 1.5e-34),
     'shielded proton mag. mom. to Bohr magneton ratio': (0.001520993128,
      '',
      1.7e-11),
     'shielded proton mag. mom. to nuclear magneton ratio': (2.792775599,
      '',
      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.670374419e-08, 'W m^-2 K^-4', 0.0),
     'tau Compton wavelength': (6.97771e-16, 'm', 4.7e-20),
     'tau Compton wavelength over 2 pi': (1.11056e-16, 'm', 1e-20),
     'tau-electron mass ratio': (3477.23, '', 0.23),
     'tau mass': (3.16754e-27, 'kg', 2.1e-31),
     'tau mass energy equivalent': (2.84684e-10, 'J', 1.9e-14),
     'tau mass energy equivalent in MeV': (1776.82, 'MeV', 0.16),
     'tau mass in u': (1.90754, 'u', 0.00013),
     'tau molar mass': (0.00190754, 'kg mol^-1', 1.3e-07),
     'tau-muon mass ratio': (16.817, '', 0.0011),
     'tau-neutron mass ratio': (1.89115, '', 0.00013),
     'tau-proton mass ratio': (1.89376, '', 0.00013),
     'Thomson cross section': (6.6524587321e-29, 'm^2', 6e-38),
     'triton-electron mag. mom. ratio': (-0.001620514423, '', 2.1e-11),
     'triton-electron mass ratio': (5496.92153573, '', 2.7e-07),
     'triton g factor': (5.957924931, '', 1.2e-08),
     'triton mag. mom.': (1.5046095202e-26, 'J T^-1', 3e-35),
     'triton mag. mom. to Bohr magneton ratio': (0.0016223936651, '', 3.2e-12),
     'triton mag. mom. to nuclear magneton ratio': (2.9789624656, '', 5.9e-09),
     'triton mass': (5.0073567446e-27, 'kg', 1.5e-36),
     'triton mass energy equivalent': (4.500387806e-10, 'J', 1.4e-19),
     'triton mass energy equivalent in MeV': (2808.92113298, 'MeV', 8.5e-07),
     'triton mass in u': (3.01550071621, 'u', 1.2e-10),
     'triton molar mass': (0.00301550071517, 'kg mol^-1', 9.2e-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.99371703414, '', 1.5e-10),
     'unified atomic mass unit': (1.6605390666e-27, 'kg', 5e-37),
     'von Klitzing constant': (25812.80745, 'ohm', 0.0),
     'weak mixing angle': (0.2229, '', 0.0003),
     'Wien frequency displacement law constant': (58789257570.0, 'Hz K^-1', 0.0),
     'Wien wavelength displacement law constant': (0.002897771955, 'm K', 0.0),
     'atomic unit of mom.um': (1.992851882e-24, 'kg m s^-1', 2.4e-32),
     'electron-helion mass ratio': (0.0001819543074573, '', 7.9e-15),
     'electron-triton mass ratio': (0.0001819200062251, '', 9e-15),
     'helion g factor': (-4.255250615, '', 5e-08),
     'helion mag. mom.': (-1.074617532e-26, 'J T^-1', 1.3e-34),
     'helion mag. mom. to Bohr magneton ratio': (-0.001158740958, '', 1.4e-11),
     'helion mag. mom. to nuclear magneton ratio': (-2.127625307, '', 2.5e-08),
     'Loschmidt constant (273.15 K, 100 kPa)': (2.651645804e+25, 'm^-3', 0.0),
     '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.30557435e-30, 'kg', 8.2e-37),
     'neutron-proton mass difference energy equivalent': (2.07214689e-13,
      'J',
      7.4e-20),
     'neutron-proton mass difference energy equivalent in MeV': (1.29333236,
      'MeV',
      4.6e-07),
     'neutron-proton mass difference in u': (0.00138844933, 'u', 4.9e-10),
     'standard-state pressure': (100000.0, 'Pa', 0.0),
     'alpha particle relative atomic mass': (4.001506179127, '', 6.3e-11),
     'Bohr magneton in inverse meter per tesla': (46.686447783,
      'm^-1 T^-1',
      1.4e-08),
     'Boltzmann constant in inverse meter per kelvin': (69.50348004,
      'm^-1 K^-1',
      0.0),
     'conventional value of ampere-90': (1.00000008887, 'A', 0.0),
     'conventional value of coulomb-90': (1.00000008887, 'C', 0.0),
     'conventional value of farad-90': (0.9999999822, 'F', 0.0),
     'conventional value of henry-90': (1.00000001779, 'H', 0.0),
     'conventional value of ohm-90': (1.00000001779, 'ohm', 0.0),
     'conventional value of volt-90': (1.00000010666, 'V', 0.0),
     'conventional value of watt-90': (1.00000019553, 'W', 0.0),
     'deuteron relative atomic mass': (2.013553212745, '', 4e-11),
     'electron gyromag. ratio in MHz/T': (28024.9514242, 'MHz T^-1', 8.5e-06),
     'electron relative atomic mass': (0.000548579909065, '', 1.6e-14),
     'elementary charge over h-bar': (1519267447000000.0, 'A J^-1', 0.0),
     'helion relative atomic mass': (3.014932247175, '', 9.7e-11),
     'helion shielding shift': (5.996743e-05, '', 1e-10),
     'hyperfine transition frequency of Cs-133': (9192631770.0, 'Hz', 0.0),
     'lattice spacing of ideal Si (220)': (1.920155716e-10, 'm', 3.2e-18),
     'luminous efficacy': (683.0, 'lm W^-1', 0.0),
     'neutron gyromag. ratio in MHz/T': (29.1646931, 'MHz T^-1', 6.9e-06),
     'neutron relative atomic mass': (1.00866491595, '', 4.9e-10),
     'nuclear magneton in inverse meter per tesla': (0.0254262341353,
      'm^-1 T^-1',
      7.8e-12),
     'Planck constant in eV/Hz': (4.135667696e-15, 'eV Hz^-1', 0.0),
     'proton gyromag. ratio in MHz/T': (42.577478518, 'MHz T^-1', 1.8e-08),
     'proton relative atomic mass': (1.007276466621, '', 5.3e-11),
     'reduced Compton wavelength': (3.8615926796e-13, 'm', 1.2e-22),
     'reduced muon Compton wavelength': (1.867594306e-15, 'm', 4.2e-23),
     'reduced neutron Compton wavelength': (2.1001941552e-16, 'm', 1.2e-25),
     'reduced Planck constant': (1.054571817e-34, 'J s', 0.0),
     'reduced Planck constant in eV s': (6.582119569e-16, 'eV s', 0.0),
     'reduced Planck constant times c in MeV fm': (197.3269804, 'MeV fm', 0.0),
     'reduced proton Compton wavelength': (2.10308910336e-16, 'm', 6.4e-26),
     'reduced tau Compton wavelength': (1.110538e-16, 'm', 7.5e-21),
     'shielded helion gyromag. ratio in MHz/T': (32.43409942, 'MHz T^-1', 3.8e-07),
     'shielded proton gyromag. ratio in MHz/T': (42.57638474, 'MHz T^-1', 4.6e-07),
     'shielding difference of d and p in HD': (2.02e-08, '', 2e-11),
     'shielding difference of t and p in HT': (2.414e-08, '', 2e-11),
     'tau energy equivalent': (1776.86, 'MeV', 0.12),
     'triton relative atomic mass': (3.01550071621, '', 1.2e-10),
     'triton to proton mag. mom. ratio': (1.0666399191, '', 2.1e-09),
     'vacuum electric permittivity': (8.8541878128e-12, 'F m^-1', 1.3e-21),
     'vacuum mag. permeability': (1.25663706212e-06, 'N A^-2', 1.9e-16),
     'W to Z mass ratio': (0.88153, '', 0.00017)}

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["proton mass"]
    # value, unit, error

Out[6]:

    (1.67262192369e-27, 'kg', 5.1e-37)

## Fitten¶

Oft möchte man eine Funktion mit freien Parametern, 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. Regression von Polynomen¶

Im folgenden wird eine lineare Regression mit `np.polyfit` durchgeführt:

$$ f(x) = a + b \cdot x $$
In [7]:

    # 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('data/example_data_linear.txt', unpack=True)

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

Out[7]:

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

In [8]:

    # 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))

    for name, value, error in zip('ab', params, errors):
        print(f'{name} = {value:.3f} ± {error:.3f}')

    a = 4.977 ± 0.021
    b = 0.070 ± 0.124

In [9]:

    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[9]:

    <matplotlib.legend.Legend at 0x7fa3e84938e0>

### 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.

Als Beispiel einer komplexeren Funktion wird im Folgenden die `Sigmoidfunktion` verwendet (Ähnlich zum `tanh`):

$$ f(x; a, b, c) = \frac{a}{1 + \exp(-(x-b))} + c$$
In [10]:

    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[10]:

    <matplotlib.legend.Legend at 0x7fa3e8467280>

Die Messwerte aus einem Praktikumsversuch:

In [11]:

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

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

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

Out[11]:

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

Ein einfacher Fit wie oben funktioniert hier nicht so gut:

In [12]:

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

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

    for name, value, uncertainty in zip('abc', params, uncertainties):
        print(f'{name} = {value:8.3f} ± {uncertainty:.3f}')

    a = -307.056 ± inf
    b =    1.000 ± inf
    c =  307.769 ± inf

    /home/luckyjosh/.local/anaconda3/envs/toolbox/lib/python3.9/site-packages/scipy/optimize/minpack.py:833: OptimizeWarning: Covariance of the parameters could not be estimated
      warnings.warn('Covariance of the parameters could not be estimated',

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

In [13]:

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

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

    plt.legend(loc='best')

Out[13]:

    <matplotlib.legend.Legend at 0x7fa3e6621910>

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 [14]:

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

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

    for name, value, uncertainty in zip('abc', params, uncertainties):
        print(f'{name} = {value:8.3f} ± {uncertainty:.3f}')

    a =   -0.494 ± 0.014
    b =   40.668 ± 0.137
    c =    0.839 ± 0.006

In [15]:

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

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

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

    plt.legend(loc='best')

Out[15]:

    <matplotlib.legend.Legend at 0x7fa3e4d81ac0>

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

In [16]:

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

    parameter = [default_seed, good_seed, params]

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

    for a, b, c in parameter:
        plt.plot(x_plot, sigmoid(x_plot, a, b, c),  label=f"f(x; {a:0.3f}, {b:0.3f}, {c:0.3f})")

    plt.legend()

Out[16]:

    <matplotlib.legend.Legend at 0x7fa3e83d0cd0>

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 [17]:

    bs = [0, 20, 40]

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

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

    for b in bs:

        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.get_color(),
            ms=20,
            label=f"f(x={b}) = {sigmoid(b, 1, b, 0)}"
        )

    plt.legend()

Out[17]:

    <matplotlib.legend.Legend at 0x7fa3e83d7c40>

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 [18]:

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

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

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

    x[idx], y[idx]

Out[18]:

    (40.8, 0.5586250642)

## Weitere nützliche Funktionen für das Praktikum¶

Folgende Eigenschaften von Scipy sollen nur die Idee vermitteln, was noch alles möglich ist.

### Statistische Verteilungen und Funktionen¶

Das `scipy.stats` Modul enthält viele Wahrscheinlichkeitsverteilungen und -funktionen. Als Beispiel wird hier jedoch nur die Standardabweichung des Mittelwertes berechnet.

In [19]:

    import numpy as np
    x = np.array([2, 5, 7])  # create test array

    from scipy.stats import sem
    print(sem(x)) # standard error of the mean

    1.4529663145135578

### Finden von Peaks¶

Das `scipy.signal` Modul enthält Funktionen zur Signalverarbeitung

In [20]:

    # example data
    from scipy.misc import electrocardiogram
    x = electrocardiogram()[2000:4000]

    # find peaks
    from scipy.signal import find_peaks
    peaks, _ = find_peaks(x, distance=150)

    plt.plot(x, label='EKG')
    plt.plot(peaks, x[peaks], "x", ms=10, label='peaks')
    plt.legend(loc='best')
    plt.show()

In [ ]: