ZeroDivisionError: float division by zero for Mathematical Calculation

Dear Fellow Members,

I tried to develop a formula based calculator for Solar Photovoltaic application using streamlit. It looks like this:

But the code gives me ‘ZeroDivisionError: float division by zero’ as the denominator is always zero untill and unless the user puts value in the form. please help me to solve this problem:

import numpy as np
import math
from scipy.optimize import fsolve
import pvlib
import streamlit as st
import matplotlib.pyplot as plt 
import pandas as pd
import plotly.express as px



#--------------------------------------------------


#Input section design for PV Module parameter extraction using Module Datasheet Data


st.header('PVSYSTEM DESIGN_Part1')
Voc = st.number_input('Open Circuit Voltage (Voc)')
Vmp = st.number_input('Voltage at MPP (Vmp)')
Isc = st.number_input('Short Circuit Current (Isc)')
Imp = st.number_input('Current at MPP (Imp)')
Ncell = st.number_input('Number of Cells in PV Module')
        
        


        
        

st.write('PV Array parameters are', Voc,Isc,Imp)


#-------------------------------------------------------------

print(f'Short Current is {Isc}')
#Main function to calculate the parameters









k = 1.38 * (10**-23)
q = 1.6 * (10**-19)
T = 298.15 # Temp in kelvin 25degree
Vt = (k * T) / q
print ("Vt=",Vt)

def eta_initial():
    n1 = 2*Vmp - Voc
    n2 = Ncell * Vt
    n3 = np.log((Isc - Imp) / Isc)
    n4 = Imp / (Isc - Imp)
    n5 = n1 / (n2 * (n3 + n4))
    #print ( "inieta = ", n5)
    return n5

eta_initial()
eta_ini = eta_initial()
#x= int(ini1)
print (eta_ini)
#print (x)

The error I am getting is:

runfile('C:/Users/Amrit/Desktop/Excel_Webapp/PAN_File_Creator_v01.1.py', wdir='C:/Users/Amrit/Desktop/Excel_Webapp')
Vmp = {Vmp}
Short Current is 0.0
Vt= 0.02571543749999999
Traceback (most recent call last):

  File "C:\Users\Amrit\Desktop\Excel_Webapp\PAN_File_Creator_v01.1.py", line 73, in <module>
    eta_initial()

  File "C:\Users\Amrit\Desktop\Excel_Webapp\PAN_File_Creator_v01.1.py", line 67, in eta_initial
    n3 = np.log((Isc - Imp) / Isc)

ZeroDivisionError: float division by zero


Hi amrit,
You can use the ‘value’ argument in your number_inputs to set the default value to something else than 0, something like :

Voc = st.number_input('Open Circuit Voltage (Voc)')
Vmp = st.number_input('Voltage at MPP (Vmp)')
Isc = st.number_input('Short Circuit Current (Isc)', value = 1)
Imp = st.number_input('Current at MPP (Imp)')
Ncell = st.number_input('Number of Cells in PV Module', value = 1)

That would solve your problem when launching the app (I set Ncell to 1 to avoid getting a ZeroDivisionError when computing n5), if you also want to avoid getting a red error message when your user inputs “wrong” values you could use a try/except syntax in eta_initial() and handle that situation yourself.

2 Likes

Dear Victor_gcd,

Thanks a lot. You solved my problem. It was so easy but I never thought of it.

Thanks again
Amrit