Skip to content

Instantly share code, notes, and snippets.

@unalfaruk
Created June 25, 2022 20:20
Show Gist options
  • Save unalfaruk/de5a181f13fddcd38bf9ae8d2cc0c022 to your computer and use it in GitHub Desktop.
Save unalfaruk/de5a181f13fddcd38bf9ae8d2cc0c022 to your computer and use it in GitHub Desktop.
ESTIMATION OF A CONSTANT | A basic simulation code
# -*- 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