monte.py

Created by vnap0v

Created on May 17, 2025

1.15 KB

This uses Monte Carlo method to calculate the effect of resistor spread on the output voltage of a 2 resistor voltage divider. The function montecarlo can be used with or without arguments. Numpy and matplotlib are used, a histogram is displayed at the end. Tested on the calculator using software version 23.2.6.


import random
from matplotlib.pyplot import *
import numpy as np
# output voltage of a voltage divider
def volt_div(r1,r2,v):
  return v*r2/(r1+r2)
# function optionally accepts arguments
def montecarlo(r1_n=None,r2_n=None,tol=None,Vin=None):
    N=1000 # number of data points
    if Vin==None:
        print("Monte Carlo - spread of voltage divider")
        Vin = float(input("What is the input voltage? "))
        r1_n = float(input("Value of upper resistor? "))
        r2_n = float(input("Value of lower resistor? "))
        tol = float(input("Tolerance of resistors in %? "))
    random.seed()
    Vout=np.zeros(N)
    for i in range(N):
      r1=random.uniform(r1_n*(1-tol/100),r1_n*(1+tol/100))
      r2=random.uniform(r2_n*(1-tol/100),r2_n*(1+tol/100))
      Vout[i]=volt_div(r1,r2,Vin)
    print("Calculated {} data points".format(N))
    print("mean of Vout {:.4f} V".format(np.mean(Vout)))
    print("max, min values of Vout\n","{:.4f} V".format(np.min(Vout)), "{:.4f} V".format(np.max(Vout)))
    print("std dev of Vout {:.2e} V".format(np.std(Vout)))
    input("Hit any Exe for histogram")
    hist(Vout,N//15)
    grid()
    show() 
montecarlo()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.