Вывод моего кода следующий:
Код: Выделить всё
Beta: [0.00046253 0.00025365]
Beta Std Error: [0. 0.]
Beta Covariance: [[0. 0.]
[0. 0.]]
Residual Variance: 0.0
Inverse Condition #: 0.0
Reason(s) for Halting:
Problem is not full rank at solution
Sum of squares convergence
fitted values of Gradient and Intercept: [0.00046253 0.00025365]
error on Gradient and Intercept: [0. 0.]
Обратите внимание, что суффикс NC предназначен только для «Нет сердечника» (лаборатория, исследующая закон Фарадея, эффективность с разными сердечниками первичной и вторичной катушек, не совсем актуально, но это контекст моего кода), поскольку я собирался скопировать и вставить тот же код, но изменил суффикс и каталог, в котором он повторяется (я знаю, это звучит запутанно) но я уже перебираю файлы в каталоге, и, если честно, это слишком близко к моему лабораторному отчету, потому что мне нужно сделать это более удобным способом)
Код: Выделить всё
# This one is for the ODR method because uhm i cant do weights with both the xerror and yerror
def fit_function_linear(m_and_c_vector, x): #cuz y = mx + c
m = m_and_c_vector[0]
c = m_and_c_vector[1]
output = m*x + c
return output
... (other code)
linear_model_NC = odr.Model(fit_function_linear)
Data_For_This_NC = odr.RealData(x_values_NC, Isecondary_NC, sx=x_err_NC, sy=err_Isecondary_NC)
odrThingyThatIDontReallyUnderstand_NC = odr.ODR(Data_For_This_NC, linear_model_NC, beta0=[(Temp_Isecondary_value/Temp_x_value), (Temp_Isecondary_value - (Temp_Isecondary_value/Temp_x_value))])
output_Thing_For_Fit_NC = odrThingyThatIDontReallyUnderstand_NC.run()
output_Thing_For_Fit_NC.pprint()
print("")
print(f"fitted values of Gradient and Intercept: {output_Thing_For_Fit_NC.beta}")
print("")
print(f"error on Gradient and Intercept: {output_Thing_For_Fit_NC.sd_beta}")
Код: Выделить всё
#might also want to do the calculation method of V_p = V_supply * R_p / (R+R_p), probably should do this for the lab report
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy.optimize import curve_fit
import os
import scipy.odr
from scipy import odr
def calcurr_and_error(Voltage, Resistance, err_Voltage, err_Resistance):
Current = Voltage/Resistance
err_Current = Current*((err_Voltage/Voltage)**2 + (err_Resistance/Resistance)**2)**0.5
return Current, err_Current
def Error_Vr18_multimeter(Voltage):
power_of_10_of_rightmost_digit = -3
err_Vr18 = Voltage*(0.01) + 4*10**power_of_10_of_rightmost_digit # we can do this as in ALL of the readings the power of 10 is -3. thank FUCK
return err_Vr18
def calculate_error_on_x_value_AND_calculate_x_value(Vp, Ip, Vs, err_Vp, err_Ip, err_Vs): #not the right method for calculating uncertainties as its second order. talk about monte carlo simulations and how we dont know how do these yet in 1st year in lab report
x = Vp*Ip / Vs
err_x = x*((err_Vp/Vp)**2 + (err_Ip/Ip)**2 + (err_Vs/Vs)**2)**0.5
return x, err_x
# This one is for the ODR method because uhm i cant do weights with both the xerror and yerror
def fit_function_linear(m_and_c_vector, x): #cuz y = mx + c
m = m_and_c_vector[0]
c = m_and_c_vector[1]
output = m*x + c
return output
r18 = 17.69 #resistances in ohms
r27 = 26.39
rprimary = 1.139
rsecondary = 0.416
err_r18 = 0.08845
err_r27 = 0.13195
err_rprimary = 5.695e-3
err_rsecondary = 2.08e-3 # all in ohms
Vr18_array = np.loadtxt("Data/Multimeter.csv", skiprows=2, delimiter=",")
#--- start of bit to copy paste ------------------------------------------------------------------------
Vsupply_NC = []
err_Vsupply_NC = []
Vprimary_NC = []
err_Vprimary_NC = []
Vsecondary_NC = []
err_Vsecondary_NC = []
Vr18_NC = [] #this one is the multimetre stuff which is stored in a seperate CSV
Iprimary_NC = []
err_Iprimary_NC = []
Isecondary_NC = []
err_Isecondary_NC = []
x_values_NC = []
x_err_NC = []
i=0
num_index_of_core = 1 #index of the measurement number in the column for the Vr18, which was measured by the multimetre. ACTUALLY this is irrelevant ignore this line i dont use it
#the code for the subsequent core, and i will use this number to just get the right Vr18 values from each thing
#Doing this cuz the osilliscope saves the stats in a new CSV every time. Just change the directory and copy paste everything
directory = "Data/NC"
for dirpath, dirnames, filenames in os.walk(directory):
for filename in filenames:
if filename.endswith('.csv'):
file = np.loadtxt((directory+"/"+filename), skiprows=21, usecols=(2,4,6), max_rows=2, delimiter=",")
while i
Подробнее здесь: [url]https://stackoverflow.com/questions/79887754/linear-regression-using-scipy-odr-gives-0-error-on-all-parameters[/url]
Мобильная версия