Skip to content
Snippets Groups Projects
Commit 1601b305 authored by JupyterHub User's avatar JupyterHub User
Browse files

tlm-ctlm measurement done

setup,plots,export pandas dataframe  to txt and csv
parent 2d9e2635
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,10 @@ import matplotlib.pyplot as plt ...@@ -5,6 +5,10 @@ import matplotlib.pyplot as plt
import pandas as pd import pandas as pd
from datetime import datetime from datetime import datetime
import os import os
from sklearn.linear_model import LinearRegression
import sys
import numpy as np
from IPython.display import display, clear_output
def I_V_Measurement(start,stop,step): def I_V_Measurement(start,stop,step):
...@@ -61,9 +65,10 @@ def I_V_Measurement(start,stop,step): ...@@ -61,9 +65,10 @@ def I_V_Measurement(start,stop,step):
print(df) print(df)
#exporting the data frame in an excel file #exporting the data frame in an excel file
file_name ="\results.csv" file_name ="results.csv"
path = "C:\Users\user\Desktop" path = r"\\fileserver.cst.rwth-aachen.de\public\Datentransfer\Asonitis, Alexandros"
directory = os.path.join(cwd,file_name) #r"C:\Users\user\Desktop"
directory = os.path.join(path,file_name)
df.to_csv(directory) df.to_csv(directory)
del device del device
...@@ -141,7 +146,7 @@ def stress_sampling(V2_stress=10,V3_stress=3,stress_time=30,V2_sampling=10,V3_sa ...@@ -141,7 +146,7 @@ def stress_sampling(V2_stress=10,V3_stress=3,stress_time=30,V2_sampling=10,V3_sa
#prepare full measurement #prepare full measurement
def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),comp=10,distances=(5,10,15,25,45),time='MED'): def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),comp=10,distances=(5,10,15,25,45),time='MED',innen=0):
#connect to the device #connect to the device
device = module.HP4155a('GPIB0::17::INSTR') device = module.HP4155a('GPIB0::17::INSTR')
...@@ -153,6 +158,10 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com ...@@ -153,6 +158,10 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com
plt.ylabel('Current(A)') plt.ylabel('Current(A)')
plt.title("CTLM plot") plt.title("CTLM plot")
#lists for appending all data values
ctlm_voltage = []
ctlm_current = []
ctlm_resistance = []
#execute five measurements #execute five measurements
for j in range(len(distances)): for j in range(len(distances)):
...@@ -161,6 +170,11 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com ...@@ -161,6 +170,11 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com
device.inst.write(":PAGE:MEAS") device.inst.write(":PAGE:MEAS")
device.inst.write(":PAGE:CHAN:MODE SWEEP") #go to sweep page and prepare sweep measurement device.inst.write(":PAGE:CHAN:MODE SWEEP") #go to sweep page and prepare sweep measurement
#vsus and vmus are disabled
device.disable_vsu(1)
device.disable_vsu(2)
device.disable_vmu(1)
device.disable_vmu(2)
#smu1 is constant and common #smu1 is constant and common
device.smu_mode_meas(1,'COMM') device.smu_mode_meas(1,'COMM')
device.smu_function_sweep(1,'CONS') device.smu_function_sweep(1,'CONS')
...@@ -168,6 +182,7 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com ...@@ -168,6 +182,7 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com
#smu2 is constant and I #smu2 is constant and I
device.smu_mode_meas(2,'I') device.smu_mode_meas(2,'I')
device.smu_function_sweep(2,'CONS') device.smu_function_sweep(2,'CONS')
device.cons_smu_value(2,0)
#smu3 is var1 and I #smu3 is var1 and I
device.smu_mode_meas(3,'I') device.smu_mode_meas(3,'I')
...@@ -176,6 +191,7 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com ...@@ -176,6 +191,7 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com
#smu4 is constant and I #smu4 is constant and I
device.smu_mode_meas(4,'I') device.smu_mode_meas(4,'I')
device.smu_function_sweep(4,'CONS') device.smu_function_sweep(4,'CONS')
device.cons_smu_value(4,0)
#select compliance of smu3 #select compliance of smu3
device.comp('VAR1',comp) device.comp('VAR1',comp)
...@@ -186,6 +202,14 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com ...@@ -186,6 +202,14 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com
# smu1 is common and compliance is automatically set to maximum # smu1 is common and compliance is automatically set to maximum
#define user functions
device.user_function('I','A','I3')
print(device.error())
device.user_function('V','V','V4-V2')
print(device.error())
device.user_function('R','OHM','DIFF(V,I)')
print(device.error())
#integration time #integration time
device.integration_time(time) device.integration_time(time)
...@@ -202,15 +226,22 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com ...@@ -202,15 +226,22 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com
voltage_values = device.return_data('V3') voltage_values = device.return_data('V3')
current_values = device.return_data('I3') current_values = device.return_data('I3')
voltage = device.return_data('V')
print(voltage_values)
print(current_values)
resistance_values=[] ctlm_voltage.append(voltage_values)
ctlm_current.append(current_values)
for i in range(len(voltage_values)):
pass
#resistance_values.append(voltage_values[i]/current_values[i])
#plot results of the single measurement #plot results of the single measurement
plt.plot(voltage_values,current_values,label=f"distance={distances[j]}") #plt.plot(voltage_values,current_values,label=f"distance={distances[j]}")
plt.plot(voltage_values,current_values,label='ausgangvoltage')
plt.legend()
plt.show()
plt.figure()
plt.plot(voltage,current_values,label='Eingangsvoltage')
plt.legend()
plt.show()
#save measurement as txt file #save measurement as txt file
#add title to the results #add title to the results
...@@ -220,14 +251,18 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com ...@@ -220,14 +251,18 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com
df = pd.DataFrame(data) df = pd.DataFrame(data)
print(df) print(df)
file_name = field_name+"_CTLM_"+str(j+1)+"_"+date+".txt" file_name = field_name+"_CTLM_"+str(j+1)+".txt"
cwd = os.getcwd() path =r"\\fileserver.cst.rwth-aachen.de\public\Datentransfer\Asonitis, Alexandros"
path = os.path.join(cwd,file_name) directory = os.path.join(path,file_name)
#export DataFrame to text file (keep header row and index column) #export DataFrame to text file (keep header row and index column)
#f=open(path, 'a') f=open(directory, 'a')
#f.write('title\n') f.write('title\n')
#df_string = df.to_string() df_string = df.to_string()
#f.write(df_string) f.write(df_string)
#plot diagramm
#plt.legend()
#plt.show()
#wait for confirmation from user after a measurement is done #wait for confirmation from user after a measurement is done
while True: while True:
...@@ -236,7 +271,189 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com ...@@ -236,7 +271,189 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com
break break
#close the connection and plot all the diagramms #close the connection and plot all the diagramms
plt.legend()
plt.show()
del device del device
#tlm/ctlm final part
def tlm_final(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),comp=10,distances=(5,10,15,25,45),time='MED',innen=0):
#connect to the device
device = module.HP4155a('GPIB0::17::INSTR')
date = str(datetime.today().replace(microsecond=0))
#initialize figure
fig, (ax1, ax2) = plt.subplots(2,sharex=True,figsize=(8,6)) #the plots share the same x axis
fig.suptitle('CTLM plot')
ax1.set_title('I(V)')
ax1.set(xlabel='Voltage(V)',ylabel='Current(A)')
ax2.set_title('R(V)')
ax2.set(xlabel='Voltage(V)',ylabel='Resistance(Ohm)')
#repeat five times
for j in range(len(distances)):
#setup
device.reset()
device.inst.write(":PAGE:MEAS")
device.inst.write(":PAGE:CHAN:MODE SWEEP") #go to sweep page and prepare sweep measurement
#disable vmus and vsus
device.disable_vsu(1)
device.disable_vsu(2)
device.disable_vmu(1)
device.disable_vmu(2)
#smu1 is constant and common
device.smu_mode_meas(1,'COMM')
device.smu_function_sweep(1,'CONS')
#smu2 is constant and I
device.smu_mode_meas(2,'I')
device.smu_function_sweep(2,'CONS')
device.cons_smu_value(2,0)
#smu3 is var1 and I
device.smu_mode_meas(3,'I')
device.smu_function_sweep(3,'VAR1')
#smu4 is constant and I
device.smu_mode_meas(4,'I')
device.smu_function_sweep(4,'CONS')
device.cons_smu_value(4,0)
#select compliance of smu3
device.comp('VAR1',comp)
#compliance of smu2 and smu4 is 10V
device.const_comp(2,10)
device.const_comp(4,10)
#define user functions
device.user_function('I','A','I3')
device.user_function('V','V','V4-V2')
device.user_function('R','OHM','DIFF(V,I)')
device.user_function('VS','V','V3')
#integration time
device.integration_time(time)
#define start-step-stop
device.start_value_sweep(start)
device.step_sweep(step)
device.stop_value_sweep(stop)
#display variables
device.display_variable('X','V')
device.display_variable('Y1','I')
device.display_variable('Y2','R')
device.display_variable_min_max('X','MIN',-10)
device.display_variable_min_max('X','MAX',10)
device.display_variable_min_max('Y1','MIN',start)
device.display_variable_min_max('Y1','MAX',stop)
device.display_variable_min_max('Y2','MIN',0)
device.display_variable_min_max('Y2','MAX',200)
#start measurement
device.single_measurement()
while device.operation_completed() == False:
pass
#return data from the device
V=device.return_data('V')
I=device.return_data('I')
R=device.return_data('R')
# now we have to remove resistance values that R=inf(nan) that means that the current is zero
for i in range(len(R)):
if R[i]>10**6:
R[i]=float('NAN')
# plot the results
ax1.plot(V,I,label=f"distance={distances[j]}")
ax2.plot(V,R,label=f"distance={distances[j]}")
ax1.legend(loc='best')
ax2.legend(loc="best")
clear_output(wait=True)
fig.tight_layout()
display(fig)
#export data frame to csv(for evaluation) and txt
header = ['Voltage(V)', 'Current(A)','Resistance(Ohm)']
data = {header[0]:V,header[1]:I,header[2]:R}
df = pd.DataFrame(data)
print(df)
#export to txt
#check tlm or ctlm
if(innen==0):
#specify path and file_name
file_name = field_name+"_TLM_"+str(j+1)+".txt"
location =r"\\fileserver.cst.rwth-aachen.de\public\Datentransfer\Asonitis, Alexandros"
path= os.path.join(location,file_name)
#check if file name exists
i=1
while os.path.exists(path):
file_name = field_name+"_TLM_"+str(j+1)+"_"str(i)+".txt"
path= os.path.join(location,file_name)
i=i+1
else:
#specify path and file_name
file_name = field_name+"_CTLM_"+str(j+1)+".txt"
location =r"\\fileserver.cst.rwth-aachen.de\public\Datentransfer\Asonitis, Alexandros"
path= os.path.join(location,file_name)
#check if file name exists
i=1
while os.path.exists(path):
file_name = field_name+"_CTLM_"+str(j+1)+"_"str(i)+".txt"
path= os.path.join(location,file_name)
i=i+1
title = "measured field:"+field_name+"\ndistance:"+str(distances[j])+"\nI:"+str(start)+"A to "+str(stop)+"A with step:"+str(step)+"\n"
f=open(path, 'a')
f.write(title)
df_string = df.to_string()
f.write(df_string)
f.close()
#export to csv for evaluataion
if(innen==0):
#specify path and file_name
file_name = field_name+"_TLM_"+str(j+1)+".csv"
location =r"\\fileserver.cst.rwth-aachen.de\public\Datentransfer\Asonitis, Alexandros"
path= os.path.join(location,file_name)
#check if file name exists
i=1
while os.path.exists(path):
file_name = field_name+"_TLM_"+str(j+1)+"_"str(i)+".csv"
path= os.path.join(location,file_name)
i=i+1
else:
#specify path and file_name
file_name = field_name+"_CTLM_"+str(j+1)+".csv"
location =r"\\fileserver.cst.rwth-aachen.de\public\Datentransfer\Asonitis, Alexandros"
path= os.path.join(location,file_name)
#check if file name exists
i=1
while os.path.exists(path):
file_name = field_name+"_CTLM_"+str(j+1)+"_"str(i)+".csv"
path= os.path.join(location,file_name)
i=i+1
df.to_csv(path)
# give user confirmation to do the next measurement
while True:
answer=input("Press enter to continue or anything else to stop the programm:")
if answer=="":
break
else:
sys.exit()
%% Cell type:code id:6aec8d34-d4bf-4ae0-98e3-0ad8d6a3780f tags: %% Cell type:code id:6aec8d34-d4bf-4ae0-98e3-0ad8d6a3780f tags:
``` python ``` python
''' this is the part 2 of the single step of ctlm measurement. ''' this is the part 2 of the single step of ctlm measurement.
Here we save the results in a txt file and we do the linear regression Here we save the results in a txt file and we do the linear regression
''' '''
!pip install pandas
!pip install -U scikit-learn
``` ```
%% Output %% Output
' this is the part 2 of the single step of ctlm measurement. \nHere we save the results in a txt file and we do the linear regression\n' Requirement already satisfied: pandas in /opt/conda/lib/python3.9/site-packages (2.1.0)
Requirement already satisfied: numpy>=1.22.4 in /opt/conda/lib/python3.9/site-packages (from pandas) (1.25.2)
Requirement already satisfied: python-dateutil>=2.8.2 in /opt/conda/lib/python3.9/site-packages (from pandas) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in /opt/conda/lib/python3.9/site-packages (from pandas) (2021.3)
Requirement already satisfied: tzdata>=2022.1 in /opt/conda/lib/python3.9/site-packages (from pandas) (2023.3)
Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.9/site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)
Requirement already satisfied: scikit-learn in /opt/conda/lib/python3.9/site-packages (1.3.0)
Requirement already satisfied: scipy>=1.5.0 in /opt/conda/lib/python3.9/site-packages (from scikit-learn) (1.11.2)
Requirement already satisfied: joblib>=1.1.1 in /opt/conda/lib/python3.9/site-packages (from scikit-learn) (1.3.2)
Requirement already satisfied: numpy>=1.17.3 in /opt/conda/lib/python3.9/site-packages (from scikit-learn) (1.25.2)
Requirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/lib/python3.9/site-packages (from scikit-learn) (3.2.0)
%% Cell type:code id:f2f531eb-6e42-42b1-83b8-078a439e7226 tags: %% Cell type:code id:f2f531eb-6e42-42b1-83b8-078a439e7226 tags:
``` python ``` python
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import numpy as np import numpy as np
import pandas as pd import pandas as pd
from datetime import datetime from datetime import datetime
import os import os
from sklearn.linear_model import LinearRegression from sklearn.linear_model import LinearRegression
import sys import sys
``` ```
%% Cell type:code id:ddd23aac-b903-46e2-9b96-9f6676d4411b tags: %% Cell type:code id:ddd23aac-b903-46e2-9b96-9f6676d4411b tags:
``` python ``` python
resistance = [199.0687, 203.1307, 207.3624, 211.7744, 216.3797, 221.1893, 226.2174, 231.4787, 236.9896, 242.7693, 248.838, 255.2181, 261.9341, 269.0141, 276.4874, 284.3889, 292.7551, 301.6264, 311.0509, 321.0848, 331.7863, 343.2259, 355.4841, 368.6504, 382.8311, 398.1461, 414.7365, 432.7694, 452.4416, 473.9852, 491.4541, 488.2188, 485.5548, 482.6365, 480.0089, 477.8993, 475.462, 473.3512, 471.7728, 469.9153, 468.2796, 467.1869, 465.7367, 465.1277, 463.9013, 462.756, 462.8145, 461.8933, 460.749, 462.548, -9.9e+307, 459.826, 459.255, 460.9573, 462.064, 462.1692, 463.4757, 464.6631, 465.969, 466.9178, 468.4606, 470.2536, 471.66, 473.7292, 475.982, 478.0425, 480.7104, 483.5262, 486.6198, 489.6606, 493.1878, 473.9649, 452.4223, 432.7527, 414.7214, 398.1343, 382.8215, 368.6438, 355.4759, 343.2188, 331.7775, 321.0737, 311.0401, 301.6142, 292.7427, 284.3794, 276.4811, 269.0093, 261.9302, 255.2141, 248.8343, 242.7657, 236.9855, 231.4752, 226.2149, 221.1874, 216.3789, 211.7756, 207.3632, 203.1304, 199.0674] resistance = [199.0687, 203.1307, 207.3624, 211.7744, 216.3797, 221.1893, 226.2174, 231.4787, 236.9896, 242.7693, 248.838, 255.2181, 261.9341, 269.0141, 276.4874, 284.3889, 292.7551, 301.6264, 311.0509, 321.0848, 331.7863, 343.2259, 355.4841, 368.6504, 382.8311, 398.1461, 414.7365, 432.7694, 452.4416, 473.9852, 491.4541, 488.2188, 485.5548, 482.6365, 480.0089, 477.8993, 475.462, 473.3512, 471.7728, 469.9153, 468.2796, 467.1869, 465.7367, 465.1277, 463.9013, 462.756, 462.8145, 461.8933, 460.749, 462.548, -9.9e+307, 459.826, 459.255, 460.9573, 462.064, 462.1692, 463.4757, 464.6631, 465.969, 466.9178, 468.4606, 470.2536, 471.66, 473.7292, 475.982, 478.0425, 480.7104, 483.5262, 486.6198, 489.6606, 493.1878, 473.9649, 452.4223, 432.7527, 414.7214, 398.1343, 382.8215, 368.6438, 355.4759, 343.2188, 331.7775, 321.0737, 311.0401, 301.6142, 292.7427, 284.3794, 276.4811, 269.0093, 261.9302, 255.2141, 248.8343, 242.7657, 236.9855, 231.4752, 226.2149, 221.1874, 216.3789, 211.7756, 207.3632, 203.1304, 199.0674]
print(len(resistance)) print(len(resistance))
``` ```
%% Output %% Output
101 101
%% Cell type:code id:a2d522b1-5a59-42c6-95a3-a92de47fd413 tags: %% Cell type:code id:a2d522b1-5a59-42c6-95a3-a92de47fd413 tags:
``` python ``` python
voltage = [-9.953434, -9.953406, -9.953396, -9.953398, -9.953468, -9.95352, -9.953564, -9.953582, -9.953562, -9.95354, -9.95352, -9.953506, -9.953494, -9.953522, -9.953548, -9.95361, -9.953674, -9.953672, -9.953628, -9.953628, -9.953588, -9.953552, -9.953556, -9.953562, -9.953608, -9.953652, -9.953676, -9.953696, -9.953716, -9.95369, -9.829082, -9.276158, -8.739986, -8.20482, -7.680142, -7.16849, -6.656468, -6.153566, -5.661274, -5.169068, -4.682796, -4.204682, -3.725894, -3.255894, -2.783408, -2.31378, -1.851258, -1.38568, -0.921498, -0.462548, -0.003156, 0.459826, 0.91851, 1.382872, 1.848256, 2.310846, 2.780854, 3.252642, 3.727752, 4.20226, 4.684606, 5.17279, 5.65992, 6.15848, 6.663748, 7.170638, 7.691366, 8.219946, 8.759156, 9.303552, 9.863756, 9.953262, 9.95329, 9.953312, 9.953314, 9.953358, 9.95336, 9.953382, 9.953324, 9.953344, 9.953324, 9.953284, 9.953284, 9.95327, 9.953252, 9.953278, 9.953318, 9.953344, 9.953348, 9.95335, 9.953374, 9.953392, 9.953392, 9.953434, 9.953456, 9.953434, 9.95343, 9.953454, 9.953434, 9.95339, 9.95337] voltage = [-9.953434, -9.953406, -9.953396, -9.953398, -9.953468, -9.95352, -9.953564, -9.953582, -9.953562, -9.95354, -9.95352, -9.953506, -9.953494, -9.953522, -9.953548, -9.95361, -9.953674, -9.953672, -9.953628, -9.953628, -9.953588, -9.953552, -9.953556, -9.953562, -9.953608, -9.953652, -9.953676, -9.953696, -9.953716, -9.95369, -9.829082, -9.276158, -8.739986, -8.20482, -7.680142, -7.16849, -6.656468, -6.153566, -5.661274, -5.169068, -4.682796, -4.204682, -3.725894, -3.255894, -2.783408, -2.31378, -1.851258, -1.38568, -0.921498, -0.462548, -0.003156, 0.459826, 0.91851, 1.382872, 1.848256, 2.310846, 2.780854, 3.252642, 3.727752, 4.20226, 4.684606, 5.17279, 5.65992, 6.15848, 6.663748, 7.170638, 7.691366, 8.219946, 8.759156, 9.303552, 9.863756, 9.953262, 9.95329, 9.953312, 9.953314, 9.953358, 9.95336, 9.953382, 9.953324, 9.953344, 9.953324, 9.953284, 9.953284, 9.95327, 9.953252, 9.953278, 9.953318, 9.953344, 9.953348, 9.95335, 9.953374, 9.953392, 9.953392, 9.953434, 9.953456, 9.953434, 9.95343, 9.953454, 9.953434, 9.95339, 9.95337]
print(len(voltage)) print(len(voltage))
``` ```
%% Output %% Output
101 101
%% Cell type:code id:cf775aa2-e15a-4030-8b03-cc013f660f1c tags: %% Cell type:code id:cf775aa2-e15a-4030-8b03-cc013f660f1c tags:
``` python ``` python
current=[-0.05, -0.049, -0.048, -0.047, -0.046, -0.045, -0.044, -0.043, -0.042, -0.041, -0.04, -0.039, -0.038, -0.037, -0.036, -0.035, -0.034, -0.033, -0.032, -0.031, -0.03, -0.029, -0.028, -0.027, -0.026, -0.025, -0.024, -0.023, -0.022, -0.021, -0.02, -0.019, -0.018, -0.017, -0.016, -0.015, -0.014, -0.013, -0.012, -0.011, -0.01, -0.009, -0.008, -0.007, -0.006, -0.005, -0.004, -0.003, -0.002, -0.001, 0.0, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.011, 0.012, 0.013, 0.014, 0.015, 0.016, 0.017, 0.018, 0.019, 0.02, 0.021, 0.022, 0.023, 0.024, 0.025, 0.026, 0.027, 0.028, 0.029, 0.03, 0.031, 0.032, 0.033, 0.034, 0.035, 0.036, 0.037, 0.038, 0.039, 0.04, 0.041, 0.042, 0.043, 0.044, 0.045, 0.046, 0.047, 0.048, 0.049, 0.05] current=[-0.05, -0.049, -0.048, -0.047, -0.046, -0.045, -0.044, -0.043, -0.042, -0.041, -0.04, -0.039, -0.038, -0.037, -0.036, -0.035, -0.034, -0.033, -0.032, -0.031, -0.03, -0.029, -0.028, -0.027, -0.026, -0.025, -0.024, -0.023, -0.022, -0.021, -0.02, -0.019, -0.018, -0.017, -0.016, -0.015, -0.014, -0.013, -0.012, -0.011, -0.01, -0.009, -0.008, -0.007, -0.006, -0.005, -0.004, -0.003, -0.002, -0.001, 0.0, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.011, 0.012, 0.013, 0.014, 0.015, 0.016, 0.017, 0.018, 0.019, 0.02, 0.021, 0.022, 0.023, 0.024, 0.025, 0.026, 0.027, 0.028, 0.029, 0.03, 0.031, 0.032, 0.033, 0.034, 0.035, 0.036, 0.037, 0.038, 0.039, 0.04, 0.041, 0.042, 0.043, 0.044, 0.045, 0.046, 0.047, 0.048, 0.049, 0.05]
print(len(current)) print(len(current))
``` ```
%% Output %% Output
101 101
%% Cell type:code id:7bc98b19-f8d9-4549-9775-92c3c9bd974b tags: %% Cell type:code id:7bc98b19-f8d9-4549-9775-92c3c9bd974b tags:
``` python ``` python
#we need to find element which has current value 0 #we need to find element which has current value 0
resistance[current.index(0)]=float('NAN') resistance[current.index(0)]=float('NAN')
print(resistance[current.index(0)]) print(resistance[current.index(0)])
``` ```
%% Output %% Output
--------------------------------------------------------------------------- nan
AttributeError Traceback (most recent call last)
/tmp/ipykernel_9327/792668923.py in <module>
1 #we need to find element which has current value 0
----> 2 resistance[current.index(0)]=float('NAN')
3 print(resistance[current.index(0)])
4 resistance=np.array(resistance)
AttributeError: 'numpy.ndarray' object has no attribute 'index'
%% Cell type:code id:265c23ad-29f4-4664-94c3-60fa440998f4 tags: %% Cell type:code id:265c23ad-29f4-4664-94c3-60fa440998f4 tags:
``` python ``` python
#Here we plot the current and the resistance #Here we plot the current and the resistance
fig, (ax1, ax2) = plt.subplots(2,sharex=True) #the plots share the same x axis fig, (ax1, ax2) = plt.subplots(2,sharex=True) #the plots share the same x axis
fig.suptitle('CTLM plot') fig.suptitle('CTLM plot')
ax1.set_title('I(V)') ax1.set_title('I(V)')
ax1.set(xlabel='Voltage(V)',ylabel='Current(A)') ax1.set(xlabel='Voltage(V)',ylabel='Current(A)')
ax1.plot(voltage, current) ax1.plot(voltage, current)
ax2.set_title('R(V)') ax2.set_title('R(V)')
ax2.set(xlabel='Voltage(V)',ylabel='Resistance(Ohm)') ax2.set(xlabel='Voltage(V)',ylabel='Resistance(Ohm)')
ax2.plot(voltage,resistance) ax2.plot(voltage,resistance)
fig.tight_layout() #leave space between plots fig.tight_layout() #leave space between plots
plt.show() plt.show()
``` ```
%% Output
%% Cell type:code id:ace8d693-f5cb-4aa4-b82d-66c842c3df5a tags: %% Cell type:code id:ace8d693-f5cb-4aa4-b82d-66c842c3df5a tags:
``` python ``` python
'''lets test the linear regression fitting. '''lets test the linear regression fitting.
for these tasks the are are two methods. for these tasks the are are two methods.
Method 1: measure the slope. the current step is constant. so we need only the voltage difference. Method 1: measure the slope. the current step is constant. so we need only the voltage difference.
when the second derivative I''(V) is big then we are not in the linear region(for a line is I''(V)=0) when the second derivative I''(V) is big then we are not in the linear region(for a line is I''(V)=0)
then dv should be big then dv should be big
Method 2: do a linear regression. delete the points that have the biggest distance from the line. and do a linear regression again Method 2: do a linear regression. delete the points that have the biggest distance from the line. and do a linear regression again
''' '''
#method 1 #method 1
mini=sys.float_info.min mini=sys.float_info.min
print(mini) print(mini)
#define the voltage differences #define the voltage differences
derivative=[] derivative=[]
for i in range(1,len(voltage)): for i in range(1,len(voltage)):
element = 0.001*(abs(voltage[i]-voltage[i-1])+mini)**(-1) #0,001 is the delta I element = 0.001*(abs(voltage[i]-voltage[i-1])+mini)**(-1) #0,001 is the delta I
derivative.append(element) derivative.append(element)
print(len(derivative)) print(len(derivative))
print(derivative) print(derivative)
``` ```
%% Output
2.2250738585072014e-308
100
[35.71428571382516, 100.00000000378577, 499.999999930111, 14.285714285530066, 19.230769231365876, 22.727272726848575, 55.5555555532727, 49.999999997451994, 45.45454545553223, 49.999999997451994, 71.42857143671338, 83.33333332168517, 35.71428571382516, 38.46153846141788, 16.129032258120592, 15.624999999984373, 499.999999930111, 22.727272727766113, 4.49423283715579e+304, 24.99999999983622, 27.777777778006993, 249.9999999650555, 166.6666666927136, 21.73913043510191, 22.727272726848575, 41.66666666701049, 49.999999997451994, 50.000000001892886, 38.46153846141788, 0.008025166923471992, 0.0018085668193097094, 0.0018650731481688694, 0.001868579095084515, 0.001905930875699001, 0.0019544534175572467, 0.0019530410802660826, 0.0019884589840565335, 0.0020313147481575977, 0.0020316696667655437, 0.002056462226901814, 0.0020915513873260363, 0.002088607066175425, 0.0021276595744680864, 0.002116464826471049, 0.002129344928326249, 0.002162059318259456, 0.0021478678116234012, 0.0021543273974432443, 0.002178886588953045, 0.0021767901922541094, 0.0021599111844520956, 0.002180150168743623, 0.0021534923184928995, 0.002148763171918245, 0.002161741498951554, 0.00212762335960239, 0.0021195960897691353, 0.002104775736145312, 0.0021074460283072166, 0.0020732005655691157, 0.002048407977320025, 0.002052840104284279, 0.002005776636713734, 0.001979147699834543, 0.001972814614610664, 0.0019203883793458385, 0.0018918612130614106, 0.0018545650117764857, 0.0018368981403243262, 0.0017850640123954827, 0.011172435367461389, 35.71428571382516, 45.45454545553223, 499.999999930111, 22.727272727766113, 499.999999930111, 45.45454545553223, 17.241379310575105, 50.000000001892886, 50.000000001892886, 24.99999999983622, 4.49423283715579e+304, 71.42857142765033, 55.55555555875528, 38.461538464045624, 24.99999999983622, 38.46153846141788, 249.9999999650555, 499.999999930111, 41.66666666701049, 55.55555555875528, 4.49423283715579e+304, 23.809523809216778, 45.45454545553223, 45.45454545553223, 250.00000007607784, 41.66666666701049, 49.999999997451994, 22.727272727766113, 49.999999997451994]
%% Cell type:code id:d7ee8038-f343-4660-99ea-63bd13291c42 tags: %% Cell type:code id:d7ee8038-f343-4660-99ea-63bd13291c42 tags:
``` python ``` python
second_derivative=[] second_derivative=[]
for i in range(1,len(derivative)): for i in range(1,len(derivative)):
element= abs(derivative[i]-derivative[i-1])*(abs(voltage[i]-voltage[i-1])+mini)**(-1) element= abs(derivative[i]-derivative[i-1])*(abs(voltage[i]-voltage[i-1])+mini)**(-1)
second_derivative.append(element) second_derivative.append(element)
print(len(second_derivative)) print(len(second_derivative))
print(second_derivative) print(second_derivative)
``` ```
%% Output
99
[2295918.367468986, 39999999.99414683, 242857142.78834438, 70643.64208245775, 67240.45183829192, 746097.3369502604, 308641.9753106898, 227272.72708440654, 206611.57009174748, 1071428.5719084693, 850340.1347377702, 3968253.9667669935, 98116.16955561754, 858942.5462779719, 8129.552550612436, 7568359.373900659, 238636363.56781635, inf, inf, 69444.44445381437, 6172839.505246729, 20833333.315173436, 24154589.38004353, 21481.354168721293, 430440.77135928307, 347222.2221045946, 0.0002220446049137159, 576923.0770455912, 1478981.2805528403, 0.04988925353237568, 0.00010219547145567937, 6.538847451275899e-06, 6.979475642041189e-05, 9.24806106950275e-05, 2.760347445459181e-06, 6.917262107966249e-05, 8.521692914536852e-05, 7.209514027161509e-07, 5.0370292390321e-05, 7.215953298611137e-05, 6.158198987294379e-06, 8.15653447719269e-05, 2.3818612759654342e-05, 2.7260282537895074e-05, 6.966022028756236e-05, 3.068287916262347e-05, 1.387433645885984e-05, 5.2908539128618934e-05, 4.567810652435912e-06, 3.674205863840431e-05, 4.371440853322009e-05, 5.811811672245678e-05, 1.0184180821545506e-05, 2.7887351162285667e-05, 7.37545976981003e-05, 1.707900681106398e-05, 3.141316359005189e-05, 5.620366150795954e-06, 7.217046443495352e-05, 5.1400007979936715e-05, 9.078804230072549e-06, 9.661377367549686e-05, 5.341169945280666e-05, 1.2534111053696311e-05, 0.00010342724311946446, 5.4783238628281605e-05, 7.055923660548066e-05, 3.276436166272787e-05, 9.521401319782578e-05, 0.016757058776920363, 398.89072552072116, 347866.419342195, 20661157.022020295, 238636363.56781635, 10847107.43665241, 227272727.20552164, 1282416.6429804363, 564803.805030264, 0.0, 1250000.0001501555, inf, inf, 1133786.8477635968, 949667.6164274549, 517751.47942641965, 336538.46153733676, 8136094.673191323, 62499999.98252775, 229166666.59951782, 578703.703827475, inf, inf, 515357.65823896026, 0.0, 9297520.664772095, 52083333.36811638, 347222.2221045946, 1363636.363414803, 619834.7106881351]
%% Cell type:code id:580b815f-3b7d-495f-b7b0-adb064811540 tags: %% Cell type:code id:580b815f-3b7d-495f-b7b0-adb064811540 tags:
``` python ``` python
#we have two less values, we delete also the last 2 voltages #we have two less values, we delete also the last 2 voltages
voltage_new = voltage.copy() voltage_new = voltage.copy()
current_new = current.copy() current_new = current.copy()
voltage_new.pop() voltage_new.pop()
voltage_new.pop() voltage_new.pop()
current_new.pop() current_new.pop()
current_new.pop() current_new.pop()
print(len(voltage_new)) print(len(voltage_new))
print(len(current_new)) print(len(current_new))
``` ```
%% Output
99
99
%% Cell type:code id:a15d75eb-a1a7-4d51-a0ac-43151974a038 tags: %% Cell type:code id:a15d75eb-a1a7-4d51-a0ac-43151974a038 tags:
``` python ``` python
indexes=[] indexes=[]
for i in range(len(second_derivative)): for i in range(len(second_derivative)):
if second_derivative[i]>10: if second_derivative[i]>10:
indexes.append(i) indexes.append(i)
print(list(reversed(indexes))) print(list(reversed(indexes)))
for i in list(reversed(indexes)): for i in list(reversed(indexes)):
voltage_new.pop(i) voltage_new.pop(i)
current_new.pop(i) current_new.pop(i)
print(voltage_new) print(voltage_new)
print(current_new) print(current_new)
print(len(voltage_new)) print(len(voltage_new))
print(len(current_new)) print(len(current_new))
``` ```
%% Output
[98, 97, 96, 95, 94, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 77, 76, 75, 74, 73, 72, 71, 70, 28, 27, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[-9.953676, -9.95369, -9.829082, -9.276158, -8.739986, -8.20482, -7.680142, -7.16849, -6.656468, -6.153566, -5.661274, -5.169068, -4.682796, -4.204682, -3.725894, -3.255894, -2.783408, -2.31378, -1.851258, -1.38568, -0.921498, -0.462548, -0.003156, 0.459826, 0.91851, 1.382872, 1.848256, 2.310846, 2.780854, 3.252642, 3.727752, 4.20226, 4.684606, 5.17279, 5.65992, 6.15848, 6.663748, 7.170638, 7.691366, 8.219946, 8.759156, 9.303552, 9.953324, 9.953434]
[-0.024, -0.021, -0.02, -0.019, -0.018, -0.017, -0.016, -0.015, -0.014, -0.013, -0.012, -0.011, -0.01, -0.009, -0.008, -0.007, -0.006, -0.005, -0.004, -0.003, -0.002, -0.001, 0.0, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.011, 0.012, 0.013, 0.014, 0.015, 0.016, 0.017, 0.018, 0.019, 0.028, 0.043]
44
44
%% Cell type:code id:60c467d7-aa6a-4703-b81e-ddcb0097b88b tags: %% Cell type:code id:60c467d7-aa6a-4703-b81e-ddcb0097b88b tags:
``` python ``` python
#Here we plot the current and the resistance #Here we plot the current and the resistance
fig, (ax1, ax2) = plt.subplots(2,sharex=True) #the plots share the same x axis fig, (ax1, ax2) = plt.subplots(2,sharex=True) #the plots share the same x axis
fig.suptitle('CTLM plot') fig.suptitle('CTLM plot')
ax1.set_title('I(V)') ax1.set_title('I(V)')
ax1.set(xlabel='Voltage(V)',ylabel='Current(A)') ax1.set(xlabel='Voltage(V)',ylabel='Current(A)')
ax1.plot(voltage, current,label="initial") ax1.plot(voltage, current,label="initial")
ax1.plot(voltage_new, current_new,label="linear") ax1.plot(voltage_new, current_new,label="linear")
ax2.set_title('R(V)') ax2.set_title('R(V)')
ax2.set(xlabel='Voltage(V)',ylabel='Resistance(Ohm)') ax2.set(xlabel='Voltage(V)',ylabel='Resistance(Ohm)')
ax2.plot(voltage,resistance) ax2.plot(voltage,resistance)
fig.tight_layout() #leave space between plots fig.tight_layout() #leave space between plots
ax1.legend() ax1.legend()
plt.show() plt.show()
``` ```
%% Output
%% Cell type:code id:188ea879-4cf9-419c-8198-1ed48852804e tags: %% Cell type:code id:188ea879-4cf9-419c-8198-1ed48852804e tags:
``` python ``` python
#now we have to do the linear regression #now we have to do the linear regression
x=np.array(voltage_new).reshape((-1,1)) #column matrix x=np.array(voltage_new).reshape((-1,1)) #column matrix
y = np.array(current_new) y = np.array(current_new)
model = LinearRegression() model = LinearRegression()
model.fit(x, y) model.fit(x, y)
r_sq = model.score(x, y) r_sq = model.score(x, y)
print(f"coefficient of determination: {r_sq}") print(f"coefficient of determination: {r_sq}")
``` ```
%% Output
coefficient of determination: 0.9471925862102195
%% Cell type:code id:8cc96899-4cf2-4de4-85e4-3d1cc4ce8f21 tags: %% Cell type:code id:8cc96899-4cf2-4de4-85e4-3d1cc4ce8f21 tags:
``` python ``` python
# method 2 is multiple linear regression # method 2 is multiple linear regression
x=np.array(voltage).reshape((-1,1)) #column matrix x=np.array(voltage).reshape((-1,1)) #column matrix
y = np.array(current) y = np.array(current)
print(x) print(x)
print(y) print(y)
model = LinearRegression() model = LinearRegression()
model.fit(x, y) model.fit(x, y)
r_sq = model.score(x, y) r_sq = model.score(x, y)
print(f"coefficient of determination: {r_sq}") print(f"coefficient of determination: {r_sq}")
print(f"intercept: {model.intercept_}") print(f"intercept: {model.intercept_}")
print(f"slope: {model.coef_}") print(f"slope: {model.coef_}")
``` ```
%% Output
[[-9.953434e+00]
[-9.953406e+00]
[-9.953396e+00]
[-9.953398e+00]
[-9.953468e+00]
[-9.953520e+00]
[-9.953564e+00]
[-9.953582e+00]
[-9.953562e+00]
[-9.953540e+00]
[-9.953520e+00]
[-9.953506e+00]
[-9.953494e+00]
[-9.953522e+00]
[-9.953548e+00]
[-9.953610e+00]
[-9.953674e+00]
[-9.953672e+00]
[-9.953628e+00]
[-9.953628e+00]
[-9.953588e+00]
[-9.953552e+00]
[-9.953556e+00]
[-9.953562e+00]
[-9.953608e+00]
[-9.953652e+00]
[-9.953676e+00]
[-9.953696e+00]
[-9.953716e+00]
[-9.953690e+00]
[-9.829082e+00]
[-9.276158e+00]
[-8.739986e+00]
[-8.204820e+00]
[-7.680142e+00]
[-7.168490e+00]
[-6.656468e+00]
[-6.153566e+00]
[-5.661274e+00]
[-5.169068e+00]
[-4.682796e+00]
[-4.204682e+00]
[-3.725894e+00]
[-3.255894e+00]
[-2.783408e+00]
[-2.313780e+00]
[-1.851258e+00]
[-1.385680e+00]
[-9.214980e-01]
[-4.625480e-01]
[-3.156000e-03]
[ 4.598260e-01]
[ 9.185100e-01]
[ 1.382872e+00]
[ 1.848256e+00]
[ 2.310846e+00]
[ 2.780854e+00]
[ 3.252642e+00]
[ 3.727752e+00]
[ 4.202260e+00]
[ 4.684606e+00]
[ 5.172790e+00]
[ 5.659920e+00]
[ 6.158480e+00]
[ 6.663748e+00]
[ 7.170638e+00]
[ 7.691366e+00]
[ 8.219946e+00]
[ 8.759156e+00]
[ 9.303552e+00]
[ 9.863756e+00]
[ 9.953262e+00]
[ 9.953290e+00]
[ 9.953312e+00]
[ 9.953314e+00]
[ 9.953358e+00]
[ 9.953360e+00]
[ 9.953382e+00]
[ 9.953324e+00]
[ 9.953344e+00]
[ 9.953324e+00]
[ 9.953284e+00]
[ 9.953284e+00]
[ 9.953270e+00]
[ 9.953252e+00]
[ 9.953278e+00]
[ 9.953318e+00]
[ 9.953344e+00]
[ 9.953348e+00]
[ 9.953350e+00]
[ 9.953374e+00]
[ 9.953392e+00]
[ 9.953392e+00]
[ 9.953434e+00]
[ 9.953456e+00]
[ 9.953434e+00]
[ 9.953430e+00]
[ 9.953454e+00]
[ 9.953434e+00]
[ 9.953390e+00]
[ 9.953370e+00]]
[-0.05 -0.049 -0.048 -0.047 -0.046 -0.045 -0.044 -0.043 -0.042 -0.041
-0.04 -0.039 -0.038 -0.037 -0.036 -0.035 -0.034 -0.033 -0.032 -0.031
-0.03 -0.029 -0.028 -0.027 -0.026 -0.025 -0.024 -0.023 -0.022 -0.021
-0.02 -0.019 -0.018 -0.017 -0.016 -0.015 -0.014 -0.013 -0.012 -0.011
-0.01 -0.009 -0.008 -0.007 -0.006 -0.005 -0.004 -0.003 -0.002 -0.001
0. 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009
0.01 0.011 0.012 0.013 0.014 0.015 0.016 0.017 0.018 0.019
0.02 0.021 0.022 0.023 0.024 0.025 0.026 0.027 0.028 0.029
0.03 0.031 0.032 0.033 0.034 0.035 0.036 0.037 0.038 0.039
0.04 0.041 0.042 0.043 0.044 0.045 0.046 0.047 0.048 0.049
0.05 ]
coefficient of determination: 0.9197815254258749
intercept: -3.1235551699275468e-06
slope: [0.00329592]
%% Cell type:code id:621a7181-9e30-44ed-a658-e07592d134d0 tags: %% Cell type:code id:621a7181-9e30-44ed-a658-e07592d134d0 tags:
``` python ``` python
#here is the second linear regression #here is the second linear regression
y_pred = model.predict(x) y_pred = model.predict(x)
print(y_pred) print(y_pred)
plt.figure() plt.figure()
plt.plot(voltage,current,label="real curve") plt.plot(voltage,current,label="real curve")
plt.plot(x,y_pred,label="linear") plt.plot(x,y_pred,label="linear")
plt.legend() plt.legend()
``` ```
%% Output
[-3.28088667e-02 -3.28087744e-02 -3.28087415e-02 -3.28087481e-02
-3.28089788e-02 -3.28091502e-02 -3.28092952e-02 -3.28093545e-02
-3.28092886e-02 -3.28092161e-02 -3.28091502e-02 -3.28091040e-02
-3.28090645e-02 -3.28091567e-02 -3.28092424e-02 -3.28094468e-02
-3.28096577e-02 -3.28096511e-02 -3.28095061e-02 -3.28095061e-02
-3.28093743e-02 -3.28092556e-02 -3.28092688e-02 -3.28092886e-02
-3.28094402e-02 -3.28095852e-02 -3.28096643e-02 -3.28097302e-02
-3.28097962e-02 -3.28097105e-02 -3.23990122e-02 -3.05766178e-02
-2.88094366e-02 -2.70455712e-02 -2.53162734e-02 -2.36299082e-02
-2.19423236e-02 -2.02847978e-02 -1.86622417e-02 -1.70399690e-02
-1.54372544e-02 -1.38614279e-02 -1.22833800e-02 -1.07342966e-02
-9.17701951e-03 -7.62916221e-03 -6.10472572e-03 -4.57021690e-03
-3.04030918e-03 -1.52764573e-03 -1.35254853e-05 1.51242712e-03
3.02421386e-03 4.55471484e-03 6.08858425e-03 7.61324486e-03
9.16235462e-03 1.07173311e-02 1.22832567e-02 1.38471981e-02
1.54369729e-02 1.70459894e-02 1.86515319e-02 2.02947468e-02
2.19600708e-02 2.36307407e-02 2.53470197e-02 2.70891782e-02
2.88663723e-02 3.06606591e-02 3.25070479e-02 3.28020527e-02
3.28021450e-02 3.28022175e-02 3.28022241e-02 3.28023691e-02
3.28023757e-02 3.28024482e-02 3.28022570e-02 3.28023230e-02
3.28022570e-02 3.28021252e-02 3.28021252e-02 3.28020791e-02
3.28020197e-02 3.28021054e-02 3.28022373e-02 3.28023230e-02
3.28023361e-02 3.28023427e-02 3.28024218e-02 3.28024812e-02
3.28024812e-02 3.28026196e-02 3.28026921e-02 3.28026196e-02
3.28026064e-02 3.28026855e-02 3.28026196e-02 3.28024746e-02
3.28024087e-02]
<matplotlib.legend.Legend at 0x7f8dbad2f220>
%% Cell type:code id:02e69af1-785f-43e7-8263-067687ecac42 tags: %% Cell type:code id:02e69af1-785f-43e7-8263-067687ecac42 tags:
``` python ``` python
residuals=(y-y_pred) residuals=(y-y_pred)
print(residuals) print(residuals)
threshold = 0.001 #adjust this threshold as needed threshold = 0.001 #adjust this threshold as needed
outliers = abs(residuals) > threshold outliers = abs(residuals) > threshold
print(outliers) print(outliers)
``` ```
%% Output
[-1.71911333e-02 -1.61912256e-02 -1.51912585e-02 -1.41912519e-02
-1.31910212e-02 -1.21908498e-02 -1.11907048e-02 -1.01906455e-02
-9.19071141e-03 -8.19078392e-03 -7.19084984e-03 -6.19089599e-03
-5.19093554e-03 -4.19084325e-03 -3.19075756e-03 -2.19055321e-03
-1.19034227e-03 -1.90348862e-04 8.09506117e-04 1.80950612e-03
2.80937428e-03 3.80925563e-03 4.80926881e-03 5.80928859e-03
6.80944020e-03 7.80958522e-03 8.80966432e-03 9.80973024e-03
1.08097962e-02 1.18097105e-02 1.23990122e-02 1.15766178e-02
1.08094366e-02 1.00455712e-02 9.31627335e-03 8.62990821e-03
7.94232359e-03 7.28479777e-03 6.66224168e-03 6.03996904e-03
5.43725441e-03 4.86142791e-03 4.28337996e-03 3.73429657e-03
3.17701951e-03 2.62916221e-03 2.10472572e-03 1.57021690e-03
1.04030918e-03 5.27645734e-04 1.35254853e-05 -5.12427123e-04
-1.02421386e-03 -1.55471484e-03 -2.08858425e-03 -2.61324486e-03
-3.16235462e-03 -3.71733112e-03 -4.28325667e-03 -4.84719807e-03
-5.43697292e-03 -6.04598936e-03 -6.65153189e-03 -7.29474682e-03
-7.96007079e-03 -8.63074074e-03 -9.34701967e-03 -1.00891782e-02
-1.08663723e-02 -1.16606591e-02 -1.25070479e-02 -1.18020527e-02
-1.08021450e-02 -9.80221750e-03 -8.80222409e-03 -7.80236911e-03
-6.80237570e-03 -5.80244821e-03 -4.80225705e-03 -3.80232297e-03
-2.80225705e-03 -1.80212521e-03 -8.02125210e-04 1.97920933e-04
1.19798026e-03 2.19789457e-03 3.19776273e-03 4.19767703e-03
5.19766385e-03 6.19765726e-03 7.19757816e-03 8.19751883e-03
9.19751883e-03 1.01973804e-02 1.11973079e-02 1.21973804e-02
1.31973936e-02 1.41973145e-02 1.51973804e-02 1.61975254e-02
1.71975913e-02]
[ True True True True True True True True True True True True
True True True True True False False True True True True True
True True True True True True True True True True True True
True True True True True True True True True True True True
True False False False True True True True True True True True
True True True True True True True True True True True True
True True True True True True True True True True False False
True True True True True True True True True True True True
True True True True True]
%% Cell type:code id:8d8e0281-5b75-45a0-8717-43d204a8c2c3 tags: %% Cell type:code id:8d8e0281-5b75-45a0-8717-43d204a8c2c3 tags:
``` python ``` python
x_clean = x[~outliers] x_clean = x[~outliers]
y_clean = y[~outliers] y_clean = y[~outliers]
print(x_clean) print(x_clean)
print(y_clean) print(y_clean)
``` ```
%% Output
[[-9.953672e+00]
[-9.953628e+00]
[-4.625480e-01]
[-3.156000e-03]
[ 4.598260e-01]
[ 9.953284e+00]
[ 9.953270e+00]]
[-0.033 -0.032 -0.001 0. 0.001 0.032 0.033]
%% Cell type:code id:a1e3f89d-8b58-420b-bcd4-ab684883e366 tags: %% Cell type:code id:a1e3f89d-8b58-420b-bcd4-ab684883e366 tags:
``` python ``` python
model_clean = LinearRegression() model_clean = LinearRegression()
model_clean.fit(x_clean, y_clean) model_clean.fit(x_clean, y_clean)
y_pred_clean = model_clean.predict(x_clean) y_pred_clean = model_clean.predict(x_clean)
r_sq = model.score(x_clean, y_clean) r_sq = model.score(x_clean, y_clean)
print(r_sq) print(r_sq)
``` ```
%% Output
0.9995469997854816
%% Cell type:code id:549ba62d-cb21-405d-9005-96d3300ede9c tags: %% Cell type:code id:549ba62d-cb21-405d-9005-96d3300ede9c tags:
``` python ``` python
#here is the second linear regression #here is the second linear regression
plt.figure() plt.figure()
plt.plot(voltage,current,label="real curve") plt.plot(voltage,current,label="real curve")
plt.plot(x,y_pred,label="linear") plt.plot(x,y_pred,label="linear")
plt.plot(x_clean,y_pred_clean,label="second regression") plt.plot(x_clean,y_pred_clean,label="second regression")
plt.legend() plt.legend()
plt.show() plt.show()
``` ```
%% Output
%% Cell type:code id:9dfc34a4-6396-42bd-b67b-b6da1fd58d27 tags: %% Cell type:code id:9dfc34a4-6396-42bd-b67b-b6da1fd58d27 tags:
``` python ``` python
admitance=[] admitance=[]
for R in resistance: for R in resistance:
admitance.append(1/R) admitance.append(1/R)
print(admitance) print(admitance)
plt.figure() plt.figure()
plt.plot(voltage,admitance) plt.plot(voltage,admitance)
``` ```
%% Output
[0.005023391422157276, 0.004922938777841065, 0.004822475048514099, 0.004722006059278175, 0.004621505621830513, 0.004521014352864266, 0.0044205264493359046, 0.0043200519097437474, 0.004219594446338574, 0.004119136974897568, 0.0040186788191514155, 0.003918217399157819, 0.0038177541603021522, 0.0037172772728269634, 0.003616801344292724, 0.003516311642261706, 0.0034158243528464574, 0.0033153596634777328, 0.003214907913785171, 0.0031144420414793853, 0.003013988220731236, 0.0029135330404844156, 0.0028130653382246913, 0.002712597083849631, 0.0026121179810104245, 0.002511640827324442, 0.0024111695015992082, 0.002310699416363541, 0.0022102300053752793, 0.002109770516041429, 0.0020347780189441905, 0.0020482619677898517, 0.002059499772219325, 0.002071952701463731, 0.0020832947055773343, 0.0020924910331528, 0.0021032175021347657, 0.002112596313265922, 0.0021196643808205984, 0.002128043074996707, 0.002135476326536539, 0.002140470976390819, 0.002147135924654424, 0.0021499472080463065, 0.0021556309499456026, 0.0021609660382577427, 0.0021606928909962846, 0.002165002176909689, 0.0021703791001174175, 0.0021619377880782104, nan, 0.00217473566088042, 0.0021774395488345253, 0.002169398336895847, 0.002164202361577617, 0.0021637097409347053, 0.002157610420567896, 0.002152096863297301, 0.002146065510795783, 0.0021417045998246374, 0.002134651238545995, 0.0021265121627989663, 0.002120171309841835, 0.0021109106215111925, 0.0021009197826808578, 0.0020918642170936683, 0.002080254556589581, 0.0020681402579632705, 0.002054992419132966, 0.002042230884004145, 0.002027625176454081, 0.0021098608778835734, 0.0022103242921491712, 0.0023107885866454443, 0.002411257292244866, 0.002511715267938482, 0.002612183484992353, 0.0027126456487264945, 0.002813130229081634, 0.0029135933113221073, 0.00301406816315151, 0.0031145497124180524, 0.003215019542496289, 0.0033154937665401695, 0.003415969040389393, 0.0035164291084375313, 0.0036168837580579645, 0.0037173436011320054, 0.003817811004611152, 0.003918278809830648, 0.004018738574223891, 0.0041191980580452675, 0.004219667448008423, 0.004320117230701173, 0.004420575302511019, 0.00452105318838234, 0.004621522708545057, 0.004721979302620321, 0.004822456443573401, 0.004922946048449665, 0.0050234242271713]
[<matplotlib.lines.Line2D at 0x7f8dbac382e0>]
%% Cell type:code id:ef8cd500-683e-406b-89bd-14c7823f29ea tags: %% Cell type:code id:ef8cd500-683e-406b-89bd-14c7823f29ea tags:
``` python ``` python
plt.figure() plt.figure()
plt.plot(current,voltage) plt.plot(current,voltage)
``` ```
%% Output
[<matplotlib.lines.Line2D at 0x7f8dbab9ed30>]
%% Cell type:code id:32e12cf6-a51e-46ef-83c5-7c87505ef6ec tags: %% Cell type:code id:32e12cf6-a51e-46ef-83c5-7c87505ef6ec tags:
``` python ``` python
#define the voltage differences #define the voltage differences
derivative=[] derivative=[]
for i in range(1,len(voltage)): for i in range(1,len(voltage)):
element = (voltage[i]-voltage[i-1])/0.001 #0,001 is the delta I element = (voltage[i]-voltage[i-1])/0.001 #0,001 is the delta I
derivative.append(element) derivative.append(element)
print(len(derivative)) print(len(derivative))
print(derivative) print(derivative)
``` ```
%% Cell type:code id:0be31f60-d8fd-4c0f-956b-524cf4a9f078 tags: %% Output
100
[0.02800000000036107, 0.009999999999621423, -0.002000000000279556, -0.07000000000090267, -0.05199999999838667, -0.04400000000082116, -0.018000000000739647, 0.020000000001019203, 0.0219999999995224, 0.020000000001019203, 0.013999999998404178, 0.012000000001677336, -0.02800000000036107, -0.026000000000081513, -0.06199999999978445, -0.064000000000064, 0.002000000000279556, 0.0439999999990448, 0.0, 0.04000000000026205, 0.035999999999702936, -0.004000000000559112, -0.005999999999062311, -0.04599999999932436, -0.04400000000082116, -0.023999999999801958, -0.020000000001019203, -0.019999999999242846, 0.026000000000081513, 124.60800000000027, 552.9239999999991, 536.1720000000005, 535.1660000000003, 524.6779999999998, 511.65199999999976, 512.0219999999999, 502.9020000000006, 492.2919999999999, 492.20599999999945, 486.27200000000045, 478.1139999999997, 478.7880000000002, 469.9999999999997, 472.48599999999993, 469.62800000000016, 462.5219999999999, 465.57800000000003, 464.18199999999996, 458.95, 459.392, 462.98199999999997, 458.684, 464.362, 465.3839999999998, 462.59000000000026, 470.008, 471.78799999999967, 475.11000000000035, 474.5079999999997, 482.3459999999997, 488.18400000000037, 487.1299999999996, 498.56000000000034, 505.26800000000003, 506.89000000000027, 520.7280000000001, 528.5799999999998, 539.2100000000006, 544.3959999999989, 560.2040000000006, 89.50600000000009, 0.02800000000036107, 0.0219999999995224, 0.002000000000279556, 0.0439999999990448, 0.002000000000279556, 0.0219999999995224, -0.05799999999922534, 0.019999999999242846, -0.019999999999242846, -0.04000000000026205, 0.0, -0.014000000000180535, -0.01799999999896329, 0.025999999998305157, 0.04000000000026205, 0.026000000000081513, 0.004000000000559112, 0.002000000000279556, 0.023999999999801958, 0.01799999999896329, 0.0, 0.042000000000541604, 0.0219999999995224, -0.0219999999995224, -0.003999999998782755, 0.023999999999801958, -0.020000000001019203, -0.0439999999990448, -0.020000000001019203]
%% Cell type:code id:da6324fd-ec15-48d3-91a7-98bd6bc5486c tags:
``` python ``` python
second_derivative=[] second_derivative=[]
for i in range(1,len(derivative)): for i in range(1,len(derivative)):
element = (derivative[i]-derivative[i-1]) #0,001 is the delta I element = (derivative[i]-derivative[i-1])/0.001 #0,001 is the delta I
second_derivative.append(element) second_derivative.append(element)
print(len(second_derivative)) print(len(second_derivative))
print(second_derivative) print(second_derivative)
``` ```
%% Output
99
[-18.000000000739647, -11.999999999900979, -68.00000000062312, 18.000000002516003, 7.99999999756551, 26.000000000081513, 38.00000000175885, 1.999999998503199, -1.999999998503199, -6.000000002615025, -1.9999999967268423, -40.000000002038405, 2.000000000279556, -35.999999999702936, -2.000000000279556, 66.00000000034356, 41.99999999876525, -43.9999999990448, 40.00000000026205, -4.000000000559112, -40.00000000026205, -1.999999998503199, -40.00000000026205, 1.999999998503199, 20.000000001019203, 3.999999998782755, 1.7763568394002505e-09, 45.99999999932436, 124582.00000000019, 428315.9999999988, -16751.99999999859, -1006.0000000001992, -10488.000000000511, -13026.000000000011, 370.0000000001751, -9119.999999999322, -10610.000000000695, -86.00000000046748, -5933.999999999002, -8158.000000000754, 674.0000000004898, -8788.000000000466, 2486.0000000002174, -2857.9999999997767, -7106.000000000278, 3056.0000000001537, -1396.0000000000719, -5231.999999999971, 442.0000000000073, 3589.999999999975, -4297.9999999999445, 5677.999999999997, 1021.999999999764, -2793.999999999528, 7417.999999999722, 1779.9999999996885, 3322.000000000685, -602.0000000006576, 7838.000000000022, 5838.000000000648, -1054.0000000007694, 11430.000000000746, 6707.999999999685, 1622.0000000002415, 13837.999999999794, 7851.999999999748, 10630.000000000791, 5185.99999999833, 15808.000000001697, -470698.0000000005, -89477.99999999972, -6.000000000838668, -19.999999999242846, 41.99999999876525, -41.99999999876525, 19.999999999242846, -79.99999999874774, 77.99999999846818, -39.99999999848569, -20.000000001019203, 40.00000000026205, -14.000000000180535, -3.999999998782755, 43.999999997268446, 14.000000001956892, -14.000000000180535, -21.9999999995224, -2.000000000279556, 21.9999999995224, -6.000000000838668, -17.99999999896329, 42.000000000541604, -20.000000001019203, -43.9999999990448, 18.000000000739647, 27.999999998584713, -44.00000000082116, -23.9999999980256, 23.9999999980256]
%% Cell type:code id:2957e4d3-d416-4794-84b7-d3d943703790 tags:
``` python
```
%% Cell type:code id:40915d35-183c-4d2b-8a67-d62ecebafba6 tags: %% Cell type:code id:40915d35-183c-4d2b-8a67-d62ecebafba6 tags:
``` python ``` python
#we have two less values, we delete also the last 2 voltages #we have two less values, we delete also the last 2 voltages
voltage_new = voltage.copy() voltage_new = voltage.copy()
current_new = current.copy() current_new = current.copy()
voltage_new.pop() voltage_new.pop()
voltage_new.pop() voltage_new.pop()
current_new.pop() current_new.pop()
current_new.pop() current_new.pop()
print(len(voltage_new)) print(len(voltage_new))
print(len(current_new)) print(len(current_new))
indexes=[] indexes=[]
for i in range(len(second_derivative)): for i in range(len(second_derivative)):
if abs(second_derivative[i])<1 or abs(second_derivative[i])>100 : if abs(second_derivative[i])<1 or abs(second_derivative[i])>100 :
indexes.append(i) indexes.append(i)
print(list(reversed(indexes))) print(list(reversed(indexes)))
for i in list(reversed(indexes)): for i in list(reversed(indexes)):
voltage_new.pop(i) voltage_new.pop(i)
current_new.pop(i) current_new.pop(i)
print(voltage_new) print(voltage_new)
print(current_new) print(current_new)
print(len(voltage_new)) print(len(voltage_new))
print(len(current_new)) print(len(current_new))
# method 2 is multiple linear regression # method 2 is multiple linear regression
x=np.array(voltage_new).reshape((-1,1)) #column matrix x=np.array(voltage_new).reshape((-1,1)) #column matrix
y = np.array(current_new) y = np.array(current_new)
print(x) print(x)
print(y) print(y)
model = LinearRegression() model = LinearRegression()
model.fit(x, y) model.fit(x, y)
r_sq = model.score(x, y) r_sq = model.score(x, y)
current_new_pred=model.predict(x) current_new_pred=model.predict(x)
print(f"coefficient of determination: {r_sq}") print(f"coefficient of determination: {r_sq}")
print(f"intercept: {model.intercept_}") print(f"intercept: {model.intercept_}")
print(f"slope: {model.coef_}") print(f"slope: {model.coef_}")
plt.figure() plt.figure()
plt.plot(voltage_new,current_new,label="linear part") plt.plot(voltage_new,current_new,label="linear part")
plt.plot(voltage_new,current_new_pred,label='after regression') plt.plot(voltage_new,current_new_pred,label='after regression')
plt.legend() plt.legend()
plt.show() plt.show()
``` ```
%% Output
99
99
[70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 36, 35, 34, 33, 32, 31, 30, 29, 28, 26]
[-9.953434, -9.953406, -9.953396, -9.953398, -9.953468, -9.95352, -9.953564, -9.953582, -9.953562, -9.95354, -9.95352, -9.953506, -9.953494, -9.953522, -9.953548, -9.95361, -9.953674, -9.953672, -9.953628, -9.953628, -9.953588, -9.953552, -9.953556, -9.953562, -9.953608, -9.953652, -9.953696, -6.153566, 9.953262, 9.95329, 9.953312, 9.953314, 9.953358, 9.95336, 9.953382, 9.953324, 9.953344, 9.953324, 9.953284, 9.953284, 9.95327, 9.953252, 9.953278, 9.953318, 9.953344, 9.953348, 9.95335, 9.953374, 9.953392, 9.953392, 9.953434, 9.953456, 9.953434, 9.95343, 9.953454, 9.953434]
[-0.05, -0.049, -0.048, -0.047, -0.046, -0.045, -0.044, -0.043, -0.042, -0.041, -0.04, -0.039, -0.038, -0.037, -0.036, -0.035, -0.034, -0.033, -0.032, -0.031, -0.03, -0.029, -0.028, -0.027, -0.026, -0.025, -0.023, -0.013, 0.021, 0.022, 0.023, 0.024, 0.025, 0.026, 0.027, 0.028, 0.029, 0.03, 0.031, 0.032, 0.033, 0.034, 0.035, 0.036, 0.037, 0.038, 0.039, 0.04, 0.041, 0.042, 0.043, 0.044, 0.045, 0.046, 0.047, 0.048]
56
56
[[-9.953434]
[-9.953406]
[-9.953396]
[-9.953398]
[-9.953468]
[-9.95352 ]
[-9.953564]
[-9.953582]
[-9.953562]
[-9.95354 ]
[-9.95352 ]
[-9.953506]
[-9.953494]
[-9.953522]
[-9.953548]
[-9.95361 ]
[-9.953674]
[-9.953672]
[-9.953628]
[-9.953628]
[-9.953588]
[-9.953552]
[-9.953556]
[-9.953562]
[-9.953608]
[-9.953652]
[-9.953696]
[-6.153566]
[ 9.953262]
[ 9.95329 ]
[ 9.953312]
[ 9.953314]
[ 9.953358]
[ 9.95336 ]
[ 9.953382]
[ 9.953324]
[ 9.953344]
[ 9.953324]
[ 9.953284]
[ 9.953284]
[ 9.95327 ]
[ 9.953252]
[ 9.953278]
[ 9.953318]
[ 9.953344]
[ 9.953348]
[ 9.95335 ]
[ 9.953374]
[ 9.953392]
[ 9.953392]
[ 9.953434]
[ 9.953456]
[ 9.953434]
[ 9.95343 ]
[ 9.953454]
[ 9.953434]]
[-0.05 -0.049 -0.048 -0.047 -0.046 -0.045 -0.044 -0.043 -0.042 -0.041
-0.04 -0.039 -0.038 -0.037 -0.036 -0.035 -0.034 -0.033 -0.032 -0.031
-0.03 -0.029 -0.028 -0.027 -0.026 -0.025 -0.023 -0.013 0.021 0.022
0.023 0.024 0.025 0.026 0.027 0.028 0.029 0.03 0.031 0.032
0.033 0.034 0.035 0.036 0.037 0.038 0.039 0.04 0.041 0.042
0.043 0.044 0.045 0.046 0.047 0.048]
coefficient of determination: 0.9513024191985002
intercept: -0.0010460134789201804
slope: [0.00357815]
%% Cell type:code id:84abbb6d-5542-45d1-9648-a90869445ee6 tags: %% Cell type:code id:84abbb6d-5542-45d1-9648-a90869445ee6 tags:
``` python ``` python
#we have two less values, we delete also the last 2 voltages #we have two less values, we delete also the last 2 voltages
voltage_new = voltage.copy() voltage_new = voltage.copy()
current_new = current.copy() current_new = current.copy()
voltage_new.pop() voltage_new.pop()
voltage_new.pop() voltage_new.pop()
current_new.pop() current_new.pop()
current_new.pop() current_new.pop()
print(len(voltage_new)) print(len(voltage_new))
print(len(current_new)) print(len(current_new))
indexes=[] indexes=[]
for i in range(len(second_derivative)): for i in range(len(second_derivative)):
if abs(derivative[i])>100: if abs(derivative[i])<1:
indexes.append(i) indexes.append(i)
print(list(reversed(indexes))) print(list(reversed(indexes)))
for i in list(reversed(indexes)): for i in list(reversed(indexes)):
voltage_new.pop(i) voltage_new.pop(i)
current_new.pop(i) current_new.pop(i)
print(voltage_new) print(voltage_new)
print(current_new) print(current_new)
print(len(voltage_new)) print(len(voltage_new))
print(len(current_new)) print(len(current_new))
x=np.array(voltage_new).reshape((-1,1)) #column matrix
y = np.array(current_new)
print(x)
print(y)
model = LinearRegression()
model.fit(x, y)
r_sq = model.score(x, y)
current_new_pred=model.predict(x)
print(f"coefficient of determination: {r_sq}")
print(f"intercept: {model.intercept_}")
print(f"slope: {model.coef_}")
plt.figure()
plt.plot(voltage_new,current_new,label="linear part")
plt.plot(voltage_new,current_new_pred,label='after regression')
plt.legend()
plt.show()
``` ```
%% Output
99
99
[98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[-9.95369, -9.829082, -9.276158, -8.739986, -8.20482, -7.680142, -7.16849, -6.656468, -6.153566, -5.661274, -5.169068, -4.682796, -4.204682, -3.725894, -3.255894, -2.783408, -2.31378, -1.851258, -1.38568, -0.921498, -0.462548, -0.003156, 0.459826, 0.91851, 1.382872, 1.848256, 2.310846, 2.780854, 3.252642, 3.727752, 4.20226, 4.684606, 5.17279, 5.65992, 6.15848, 6.663748, 7.170638, 7.691366, 8.219946, 8.759156, 9.303552, 9.863756]
[-0.021, -0.02, -0.019, -0.018, -0.017, -0.016, -0.015, -0.014, -0.013, -0.012, -0.011, -0.01, -0.009, -0.008, -0.007, -0.006, -0.005, -0.004, -0.003, -0.002, -0.001, 0.0, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.011, 0.012, 0.013, 0.014, 0.015, 0.016, 0.017, 0.018, 0.019, 0.02]
42
42
[[-9.953690e+00]
[-9.829082e+00]
[-9.276158e+00]
[-8.739986e+00]
[-8.204820e+00]
[-7.680142e+00]
[-7.168490e+00]
[-6.656468e+00]
[-6.153566e+00]
[-5.661274e+00]
[-5.169068e+00]
[-4.682796e+00]
[-4.204682e+00]
[-3.725894e+00]
[-3.255894e+00]
[-2.783408e+00]
[-2.313780e+00]
[-1.851258e+00]
[-1.385680e+00]
[-9.214980e-01]
[-4.625480e-01]
[-3.156000e-03]
[ 4.598260e-01]
[ 9.185100e-01]
[ 1.382872e+00]
[ 1.848256e+00]
[ 2.310846e+00]
[ 2.780854e+00]
[ 3.252642e+00]
[ 3.727752e+00]
[ 4.202260e+00]
[ 4.684606e+00]
[ 5.172790e+00]
[ 5.659920e+00]
[ 6.158480e+00]
[ 6.663748e+00]
[ 7.170638e+00]
[ 7.691366e+00]
[ 8.219946e+00]
[ 8.759156e+00]
[ 9.303552e+00]
[ 9.863756e+00]]
[-0.021 -0.02 -0.019 -0.018 -0.017 -0.016 -0.015 -0.014 -0.013 -0.012
-0.011 -0.01 -0.009 -0.008 -0.007 -0.006 -0.005 -0.004 -0.003 -0.002
-0.001 0. 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008
0.009 0.01 0.011 0.012 0.013 0.014 0.015 0.016 0.017 0.018
0.019 0.02 ]
coefficient of determination: 0.9996847110042263
intercept: -1.1363930831026318e-05
slope: [0.00208319]
%% Cell type:code id:92f48ccd-4c58-431f-bb6a-0769f10223da tags: %% Cell type:code id:92f48ccd-4c58-431f-bb6a-0769f10223da tags:
``` python ``` python
``` ```
%% Cell type:code id:478cc779-ccb0-4f26-a28f-8b295d0e213d tags:
``` python
```
......
...@@ -8,6 +8,7 @@ import os ...@@ -8,6 +8,7 @@ import os
from sklearn.linear_model import LinearRegression from sklearn.linear_model import LinearRegression
import sys import sys
import numpy as np import numpy as np
from IPython.display import display, clear_output
def I_V_Measurement(start,stop,step): def I_V_Measurement(start,stop,step):
...@@ -206,7 +207,7 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com ...@@ -206,7 +207,7 @@ def ctlm(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3),com
print(device.error()) print(device.error())
device.user_function('V','V','V4-V2') device.user_function('V','V','V4-V2')
print(device.error()) print(device.error())
device.user_function('R','OHM','V/I') device.user_function('R','OHM','DIFF(V,I)')
print(device.error()) print(device.error())
#integration time #integration time
...@@ -278,12 +279,9 @@ def tlm_final(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3 ...@@ -278,12 +279,9 @@ def tlm_final(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3
#connect to the device #connect to the device
device = module.HP4155a('GPIB0::17::INSTR') device = module.HP4155a('GPIB0::17::INSTR')
date = str(datetime.today().replace(microsecond=0)) date = str(datetime.today().replace(microsecond=0))
voltage=[]
current=[]
restistance=[]
#initialize figure #initialize figure
fig, (ax1, ax2) = plt.subplots(2,sharex=True) #the plots share the same x axis fig, (ax1, ax2) = plt.subplots(2,sharex=True,figsize=(8,6)) #the plots share the same x axis
fig.suptitle('CTLM plot') fig.suptitle('CTLM plot')
ax1.set_title('I(V)') ax1.set_title('I(V)')
ax1.set(xlabel='Voltage(V)',ylabel='Current(A)') ax1.set(xlabel='Voltage(V)',ylabel='Current(A)')
...@@ -331,7 +329,7 @@ def tlm_final(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3 ...@@ -331,7 +329,7 @@ def tlm_final(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3
#define user functions #define user functions
device.user_function('I','A','I3') device.user_function('I','A','I3')
device.user_function('V','V','V4-V2') device.user_function('V','V','V4-V2')
device.user_function('R','OHM','V/I') device.user_function('R','OHM','DIFF(V,I)')
device.user_function('VS','V','V3') device.user_function('VS','V','V3')
...@@ -371,42 +369,91 @@ def tlm_final(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3 ...@@ -371,42 +369,91 @@ def tlm_final(field_name ='M00',start=-50*10**(-3),stop=50*10**(-3),step=10**(-3
if R[i]>10**6: if R[i]>10**6:
R[i]=float('NAN') R[i]=float('NAN')
#plot data # plot the results
ax1.plot(V) ax1.plot(V,I,label=f"distance={distances[j]}")
ax2.plot(V,R,label=f"distance={distances[j]}")
ax1.legend(loc='best')
ax2.legend(loc="best")
clear_output(wait=True)
fig.tight_layout()
display(fig)
#export data frame to csv(for evaluation) and txt
header = ['Voltage(V)', 'Current(A)','Resistance(Ohm)']
data = {header[0]:V,header[1]:I,header[2]:R}
df = pd.DataFrame(data)
print(df)
#export to txt
#check tlm or ctlm
if(innen==0):
#specify path and file_name
file_name = field_name+"_TLM_"+str(j+1)+".txt"
location =r"\\fileserver.cst.rwth-aachen.de\public\Datentransfer\Asonitis, Alexandros"
path= os.path.join(location,file_name)
#check if file name exists
i=1
while os.path.exists(path):
file_name = field_name+"_TLM_"+str(j+1)+"_"str(i)+".txt"
path= os.path.join(location,file_name)
i=i+1
else:
#specify path and file_name
file_name = field_name+"_CTLM_"+str(j+1)+".txt"
location =r"\\fileserver.cst.rwth-aachen.de\public\Datentransfer\Asonitis, Alexandros"
path= os.path.join(location,file_name)
#check if file name exists
i=1
while os.path.exists(path):
file_name = field_name+"_CTLM_"+str(j+1)+"_"str(i)+".txt"
path= os.path.join(location,file_name)
i=i+1
title = "measured field:"+field_name+"\ndistance:"+str(distances[j])+"\nI:"+str(start)+"A to "+str(stop)+"A with step:"+str(step)+"\nDate:"+date+"\n"
f=open(path, 'a')
f.write(title)
df_string = df.to_string()
f.write(df_string)
f.close()
#export to csv for evaluataion
if(innen==0):
#specify path and file_name
file_name = field_name+"_TLM_"+str(j+1)+".csv"
location =r"\\fileserver.cst.rwth-aachen.de\public\Datentransfer\Asonitis, Alexandros"
path= os.path.join(location,file_name)
#check if file name exists
i=1
while os.path.exists(path):
file_name = field_name+"_TLM_"+str(j+1)+"_"str(i)+".csv"
path= os.path.join(location,file_name)
i=i+1
else:
#specify path and file_name
file_name = field_name+"_CTLM_"+str(j+1)+".csv"
location =r"\\fileserver.cst.rwth-aachen.de\public\Datentransfer\Asonitis, Alexandros"
path= os.path.join(location,file_name)
#check if file name exists
i=1
while os.path.exists(path):
file_name = field_name+"_CTLM_"+str(j+1)+"_"str(i)+".csv"
path= os.path.join(location,file_name)
i=i+1
df.to_csv(path)
# give user confirmation to do the next measurement
while True:
answer=input("Press enter to continue or anything else to stop the programm:")
if answer=="":
break
else:
sys.exit()
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
File moved
File moved
%% Cell type:code id:61ed7a95-1234-470b-918a-adfcbf186de9 tags:
``` python
#!pip install pandas
#!pip install -U scikit-learn
```
%% Cell type:code id:e80894aa-21c6-4864-99c9-fc1e644aea96 tags:
``` python
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from datetime import datetime
import os
from sklearn.linear_model import LinearRegression
import sys
from IPython.display import display, clear_output
```
%% Cell type:code id:442dbcdb-262f-4e2c-9165-c74eeb5a6707 tags:
``` python
resistance = [199.0687, 203.1307, 207.3624, 211.7744, 216.3797, 221.1893, 226.2174, 231.4787, 236.9896, 242.7693, 248.838, 255.2181, 261.9341, 269.0141, 276.4874, 284.3889, 292.7551, 301.6264, 311.0509, 321.0848, 331.7863, 343.2259, 355.4841, 368.6504, 382.8311, 398.1461, 414.7365, 432.7694, 452.4416, 473.9852, 491.4541, 488.2188, 485.5548, 482.6365, 480.0089, 477.8993, 475.462, 473.3512, 471.7728, 469.9153, 468.2796, 467.1869, 465.7367, 465.1277, 463.9013, 462.756, 462.8145, 461.8933, 460.749, 462.548, -9.9e+307, 459.826, 459.255, 460.9573, 462.064, 462.1692, 463.4757, 464.6631, 465.969, 466.9178, 468.4606, 470.2536, 471.66, 473.7292, 475.982, 478.0425, 480.7104, 483.5262, 486.6198, 489.6606, 493.1878, 473.9649, 452.4223, 432.7527, 414.7214, 398.1343, 382.8215, 368.6438, 355.4759, 343.2188, 331.7775, 321.0737, 311.0401, 301.6142, 292.7427, 284.3794, 276.4811, 269.0093, 261.9302, 255.2141, 248.8343, 242.7657, 236.9855, 231.4752, 226.2149, 221.1874, 216.3789, 211.7756, 207.3632, 203.1304, 199.0674]
print(len(resistance))
```
%% Output
101
%% Cell type:code id:e2cc3ac6-0f01-4012-b62a-820286b264a0 tags:
``` python
voltage = [-9.953434, -9.953406, -9.953396, -9.953398, -9.953468, -9.95352, -9.953564, -9.953582, -9.953562, -9.95354, -9.95352, -9.953506, -9.953494, -9.953522, -9.953548, -9.95361, -9.953674, -9.953672, -9.953628, -9.953628, -9.953588, -9.953552, -9.953556, -9.953562, -9.953608, -9.953652, -9.953676, -9.953696, -9.953716, -9.95369, -9.829082, -9.276158, -8.739986, -8.20482, -7.680142, -7.16849, -6.656468, -6.153566, -5.661274, -5.169068, -4.682796, -4.204682, -3.725894, -3.255894, -2.783408, -2.31378, -1.851258, -1.38568, -0.921498, -0.462548, -0.003156, 0.459826, 0.91851, 1.382872, 1.848256, 2.310846, 2.780854, 3.252642, 3.727752, 4.20226, 4.684606, 5.17279, 5.65992, 6.15848, 6.663748, 7.170638, 7.691366, 8.219946, 8.759156, 9.303552, 9.863756, 9.953262, 9.95329, 9.953312, 9.953314, 9.953358, 9.95336, 9.953382, 9.953324, 9.953344, 9.953324, 9.953284, 9.953284, 9.95327, 9.953252, 9.953278, 9.953318, 9.953344, 9.953348, 9.95335, 9.953374, 9.953392, 9.953392, 9.953434, 9.953456, 9.953434, 9.95343, 9.953454, 9.953434, 9.95339, 9.95337]
print(len(voltage))
```
%% Output
101
%% Cell type:code id:e4bda2ac-3256-40b3-8893-ed13c08d487a tags:
``` python
current=[-0.05, -0.049, -0.048, -0.047, -0.046, -0.045, -0.044, -0.043, -0.042, -0.041, -0.04, -0.039, -0.038, -0.037, -0.036, -0.035, -0.034, -0.033, -0.032, -0.031, -0.03, -0.029, -0.028, -0.027, -0.026, -0.025, -0.024, -0.023, -0.022, -0.021, -0.02, -0.019, -0.018, -0.017, -0.016, -0.015, -0.014, -0.013, -0.012, -0.011, -0.01, -0.009, -0.008, -0.007, -0.006, -0.005, -0.004, -0.003, -0.002, -0.001, 0.0, 0.001, 0.002, 0.003, 0.004, 0.005, 0.006, 0.007, 0.008, 0.009, 0.01, 0.011, 0.012, 0.013, 0.014, 0.015, 0.016, 0.017, 0.018, 0.019, 0.02, 0.021, 0.022, 0.023, 0.024, 0.025, 0.026, 0.027, 0.028, 0.029, 0.03, 0.031, 0.032, 0.033, 0.034, 0.035, 0.036, 0.037, 0.038, 0.039, 0.04, 0.041, 0.042, 0.043, 0.044, 0.045, 0.046, 0.047, 0.048, 0.049, 0.05]
print(len(current))
resistance[current.index(0)]=float('NAN')
print(resistance[current.index(0)])
```
%% Output
101
nan
%% Cell type:code id:a5746d39-afb4-493f-a56c-20a34a775052 tags:
``` python
derivative=np.abs(np.diff(voltage)/np.diff(current))
derivative=np.around(derivative)
print(derivative)
```
%% Output
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 125. 553. 536. 535. 525. 512. 512. 503. 492. 492. 486. 478. 479.
470. 472. 470. 463. 466. 464. 459. 459. 463. 459. 464. 465. 463. 470.
472. 475. 475. 482. 488. 487. 499. 505. 507. 521. 529. 539. 544. 560.
90. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0.]
%% Cell type:code id:47dbf7e2-bfde-46c3-a949-f44c27ba7c5c tags:
``` python
indexes=np.where(derivative>1)
print(indexes)
```
%% Output
(array([29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
63, 64, 65, 66, 67, 68, 69, 70]),)
%% Cell type:code id:f8cfc3a7-fb93-47be-a033-7aac03b1b1ea tags:
``` python
voltage_new=[voltage[index] for index in indexes[0]]
current_new=[current[index] for index in indexes[0]]
voltage_new.pop()
voltage_new.pop(0)
current_new.pop(0)
current_new.pop()
```
%% Output
0.02
%% Cell type:code id:17c70e37-532e-4c2b-a6a0-1d748b034358 tags:
``` python
plt.plot(voltage_new,current_new)
```
%% Output
[<matplotlib.lines.Line2D at 0x7fafcbeda310>]
%% Cell type:code id:23a0ebf1-c8d9-47fc-b81e-df7bd2fe4571 tags:
``` python
x=np.array(voltage_new).reshape((-1,1)) #column matrix
y = np.array(current_new)
model = LinearRegression()
model.fit(x, y)
r_sq = model.score(x, y)
current_new_pred=model.predict(x)
print(f"coefficient of determination: {r_sq}")
print(f"intercept: {model.intercept_}")
print(f"slope: {model.coef_}")
plt.figure()
plt.plot(voltage_new,current_new,label="linear part")
plt.plot(voltage_new,current_new_pred,label='after regression')
plt.legend()
plt.show()
```
%% Output
coefficient of determination: 0.9997053460919436
intercept: 8.975250494780559e-06
slope: [0.00208562]
%% Cell type:code id:24f0de1e-9677-4855-bbec-ad6be5608771 tags:
``` python
fig, (ax1, ax2) = plt.subplots(2,sharex=True,figsize=(10,10)) #the plots share the same x axis
fig.suptitle('CTLM plot')
ax1.set_title('I(V)')
ax1.set(xlabel='Voltage(V)',ylabel='Current(A)')
ax2.set_title('R(V)')
ax2.set(xlabel='Voltage(V)',ylabel='Resistance(Ohm)')
for i in range(5):
ax1.plot(voltage, current,label=f"{i}")
ax2.plot(voltage,resistance,label=f"{i}")
ax1.legend(loc='best')
ax2.legend(loc="best")
clear_output(wait=True)
fig.tight_layout()
display(fig)
while True:
answer=input("press enter to continue:")
if answer=="":
break
```
%% Output
press enter to continue:
%% Cell type:code id:4121a876-63d5-46db-9c59-634c46587233 tags:
``` python
```
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.11.4" "version": "3.9.7"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment