Created
June 25, 2022 20:20
-
-
Save unalfaruk/de5a181f13fddcd38bf9ae8d2cc0c022 to your computer and use it in GitHub Desktop.
ESTIMATION OF A CONSTANT | A basic simulation code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Created on Thu Jun 23 21:45:31 2022 | |
@author: personF | |
""" | |
import numpy as np | |
import matplotlib.pyplot as plt | |
class Resistor: | |
def __init__(self,ohm): | |
self.ohm = ohm | |
class Sensor: | |
def __init__(self,mean,std): | |
self.inputVal = [] | |
self.outputVal = [] | |
self.err_std = std | |
self.mean = mean | |
def measure(self,realVal): | |
self.inputVal.append(realVal) | |
self.outputVal.append(realVal+np.random.normal(self.mean,self.err_std)) | |
def meanOfResult(self): | |
return np.sum(self.outputVal)/len(self.outputVal) | |
resistor = Resistor(355) | |
sensorCheap = Sensor(0,25) | |
sensorExpensive = Sensor(0,4) | |
for i in range(1,501): | |
sensorCheap.measure(resistor.ohm) | |
sensorExpensive.measure(resistor.ohm) | |
# The histograph of the measurements | |
fig, axs = plt.subplots(2) | |
axs[0].hist(sensorCheap.outputVal) | |
axs[0].set_title("Cheap Multimeter") | |
axs[1].hist(sensorExpensive.outputVal) | |
axs[1].set_title("Expensive Multimeter") | |
plt.setp(axs, xlim=axs[0].get_xlim()) | |
plt.show() | |
# The mean of the measurements | |
cheapRes = sensorCheap.meanOfResult() | |
expensiveRes = sensorExpensive.meanOfResult() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment