import numpy as np
import matplotlib.pyplot as plt

"""
Explanation: Measurements_and_their_errors
End of explanation
"""


x = np.linspace(0, 1, 4)
y = np.exp(x)

yerr = 0.1 + 0.08*np.sqrt(x)
xerr = 0.1 + yerr

fig, ax = plt.subplots()
ax.errorbar(x, y, xerr=xerr, yerr=yerr, color='red', linestyle='-', ecolor='b')

ax.set_title("error bar example")
ax.grid(True)


plt.show()

"""
Explanation: Contents
1.Base Units
2.Derived Units
- List of ones with special names
3.Prefixes
4.Physical Measurements
- Errors
- Measurements
<a id='Base_Units'></a>
Base Units

these are globally agreed systems of units
they can also be called SI base units (Système International d'Unités)
there are 7
lower-case symbols unless named after person
form set of mutually independent dimensions

Ampere (A)

electrical current

Candela (cd)

luminous intensity

Kelvin (K)

thermodynamic temperature

Kilogram (kg)

mass

Metre (m)

length

Mole (mol)

amount of substance

Second (s)

time

<a id='Derived_Units'></a>
Derived Units

These are units are derived from base units
either dimensionless, expressed as product of one or more of the base units, possibly scaled by appropriate power of exponentiation
22 units with special names
many units don't (eg. $m^2$)
for Equivalents and SI equivalents, go here 

<a id='List_of_ones_with_special_names'></a>
List of ones with special names
hertz (Hz)

frequency

radian (rad)

angle

steradian (sr)

solid angle

newton (N)

force, weight

pascal (Pa)

pressure, stress

joule (J)

energy, work, heat

watt (W)

power, radiant flux

coulomb (C)

electric charge or quantity of electricity

volt (V)

voltage, electrical potential difference, electromotive force

farad (F)

electrical capacitance    

ohm ($\Omega$)

electrical resistance, impedance, reactance

siemens (S)

electrical conductance    

weber (Wb)

magnetic flux 

tesla (T)

magnetic field strength, magnetic flux density

henry (H)

electrical inductance 

degree celsius ($^{\circ} C$)

temperature relative to 273.15 K

lumen (lm)

luminous flux

lux (lx)

illuminance

becquerel (Bq)

radioactivity (decays per unit time)

gray (Gy)

absorbed dose (of ionizing radiation)

sievert (Sv)

equivalent dose (of ionizing radiation)

katal (kat)

catalytic activity

<a id='Prefixes'></a>
Prefixes
| Symbol | Prefix | Value      |
|--------|--------|------------|
| E      | exa    | $10^{18}$  |
| P      | peta   | $10^{15}$  |
| T      | tera   | $10^{12}$  |
| G      | giga   | $10^{9}$   |
| M      | mega   | $10^{6}$   |
| k      | kilo   | $10^{3}$   |
| h      | hecto  | $10^{2}$   |
| da     | deka   | $10$       |
| d      | deci   | $10^{-1}$  |
| c      | centi  | $10^{-2}$  |
| m      | milli  | $10^{-3}$  |
| $\mu$  | micro  | $10^{-6}$  |
| n      | nano   | $10^{-9}$  |
| p      | pico   | $10^{-12}$ |
| f      | femto  | $10^{-15}$ |
| a      | atto   | $10^{-18}$ |
<a id='Physical_Measurements'></a>
Physical Measurements
<a id='Errors'></a>
Errors
The error is the differences between the values and the mean value

range: difference between min and max measurement
systematic errors: differences with a pattern
random errors: differences without a pattern
$a \pm b$
with $a$ as the mean and $b$ as the greatest difference

<a id='Measurements'></a>
Measurements
Precision: degree of exactness of measurement

extent of random errors (range)
when reading fluctuates, use multiple readings
when reading is constant, estimate precision
precision $\not=$ accuracy because systematic errors affect precision

Repeatability: same result obtained if:

same experimenter
same equipment
same method

Reproducibility: same result obtained if:

different experimenter
or different equipment
or different technique

Accuracy: measure of confidence in measurement

an accurate measurement is close to the accepted value
expressed as uncertainty of measurement

linearity: reading is directly proportional to value reading is measuring
Uncertainty: interval in which the true value is expected to lie

$a \pm b$
b is the uncertainty
if readings are the same, instrument precision should be used
uncertainty can be given as absolute, fractional or percentage
absolute eg. $I = 3.6 \pm 0.1 A$
fractional eg. $I = 3.6 \pm 0.1 A$ uncertainty: 0.028% (uncertainty/mean as fraction or decimal)
percentage eg. $I = 3.6 \pm 0.1 A$ uncertainty: 2.8% (uncertainty/mean as percentage)

Experimental uncertainties:
when adding or subtracting values, combine main value and uncertainty separately
- eg. $3.6 \pm 0.2 + 1.2 \pm 0.02 = 4.8 \pm 0.22$
when multiplying, dividing or dealing with power values, combine main value and add percentage uncertainty
- eg. $3.6 \pm 0.2 \times 1.2 \pm 0.02 = 4.32$
- $\frac{0.2}{3.6} = \frac{1}{18} \times 100 = \frac{50}{9}\%$
- $\frac{0.02}{1.2} = \frac{1}{60} \times 100 = \frac{15}{9}\%$
- $= 4.32 \pm 7.2\%$
if raising value to power n, multiply percentage uncertainty by n
error bars example below:
- only need to plot first and last error bars
- error bars with worst uncertainty
- axis with worst uncertainty
End of explanation
"""


x = np.linspace(1, 4, 3)

x2 = np.linspace(0, 5, 5)

def f(x):
    return 2*x

y = f(x)

yerr = 0.9
xerr = 0.1

maxg = f(0.7*x)+1.5
ming = f(1.3*x)-1.5

maxi = f(0.7*x2)+1.5
mini = f(1.3*x2)-1.5

fig, ax = plt.subplots(1, 2, figsize=(15,7))

ax[0].errorbar(x, y, xerr=xerr, yerr=yerr, color='red', linestyle='-', ecolor='b', label='optimal line')

ax[0].plot([x[0]-xerr, x[1], x[-1]+xerr], maxg, label='min gradient')
ax[0].plot([x[0]+xerr, x[1], x[-1]-xerr], ming, label='max gradient')

ax[0].set_title("error bar gradient example")

ax[1].errorbar(x, y, xerr=xerr, yerr=yerr, color='red', linestyle='-', ecolor='b', label='optimal line')

ax[1].plot(x2, maxi, label='max intercept')
ax[1].plot(x2, mini, label='min intercept')

ax[1].set_title("error bar intercept example")

for axes in ax:
    axes.grid(True)
    axes.legend()

plt.show()

"""
Explanation: Gradient Uncertainty
On a graph, we:
- draw the main line
- draw the error bars
- draw a line which fit in the error bars area of maximum gradient
- draw a line which fit in the error bars area of minimum gradient
- compare the gradients to the optimal line
Intercept Uncertainty
The same as gradient uncertainty but instead comparing the y intercept
End of explanation
"""


