meta data for this page
  •  

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
a_mechanik:drehschwingungen:gruppenseiten:gruppe342:start [26 January 2021 15:17] – [Code zu den Plots] juliusriekenberga_mechanik:drehschwingungen:gruppenseiten:gruppe342:start [26 January 2021 15:19] (current) juliusriekenberg
Line 434: Line 434:
         i, i_err = err_evolution(trägmom, [i2, slope, slope2], [i2_err, slope_err, slope2_err])         i, i_err = err_evolution(trägmom, [i2, slope, slope2], [i2_err, slope_err, slope2_err])
         print('I: ', string_correctly(i, i_err), end='\n\n\n')         print('I: ', string_correctly(i, i_err), end='\n\n\n')
 +
 +</code>
 +        
 +
 +
 +===== Hilfscode =====
 +<code python>
 +def round_correctly(val, err):
 +    first_sign = 0
 +    while err // (10 ** first_sign) > 0:
 +        first_sign += 1
 +    while err // (10 ** first_sign) == 0:
 +        first_sign -= 1
 +    if err // (10 ** first_sign) < 3:
 +        first_sign -= 1
 +    return round(val * 10 ** -first_sign), round(err * 10 ** -first_sign), first_sign
 +
 +
 +def lin_fit_label(sol, xstr):
 +    return '$Fit = ' + string_correctly(sol.slope, sol.stderr) + ' \\cdot ' + xstr + ' + ' + string_correctly(
 +        sol.intercept, sol.intercept_stderr) + '$;  $R^2 = ' + str(round(sol.rvalue ** 2, 3)) + '$'
 +
 +
 +def string_correctly(val, err):
 +    val, err, pot = round_correctly(val, err)
 +    return ' ( ' + str(int(val)) + ' \pm ' + str(int(err)) + ' )' + (' \\cdot 10^{' + str(pot) + '} ') * (pot != 0)
 +
 +
 +def err_evolution(func, params, errors):
 +    val = func(*params)
 +    err_q = 0
 +    for i in range(len(params)):
 +        _, _, pot = round_correctly(params[i], errors[i])
 +        h = 10 ** (pot - 5)
 +        upper = (params[j] + h * (j == i) for j in range(len(params)))
 +        lower = (params[j] - h * (j == i) for j in range(len(params)))
 +        diff = (func(*upper) - func(*lower)) / (2 * h)
 +        err_q += (diff * errors[i]) ** 2
 +    return val, err_q ** 0.5
  
 </code> </code>