'My graph is printing too many lines. I think its due to an error in my while loop that keeps creating arrays

This is a look at the code involved and the resulting graph.

I am trying to set up a graph with the x-value being a number decreasing at a constant pace and the y-value being determined by an equation relating to the x-value. I ended up using a .insert to get the result calculated into the array.


Here's the code:

import math
import numpy as np
import scipy.optimize 
from functools import reduce

## Input weight and gravity
m = eval(input("Please input molecular weight:"))
y = eval(input("Please input Specific Gravity:"))
t = eval(input("Please input resevoir temperature"))
pressure = eval(input("Please input resevoir pressure"))
print("-------------------------------------------")

#Calculate Tc

tc = -1247.4 + 0.792*m + 1971*y - 27000/m + 707.4/y
print("Tc is equal to:",tc)

#Calculate Pc

pc = math.exp(0.01901 - 0.0048442*m + 0.13239*y + 227/m - 1.1663/y + 
1.2702*np.log(m))

print("Pc is equal to:",pc)

w = -0.64235 + 0.00014667*m + 0.021876*y - 4.559/m + 0.21699 * np.log(m)
print("w is equal to:",w)

# mwa = sum of yi1
ppc = 0 #sum of yi2
tpc = 0 #sum of yi3
i = eval(input("Please input the number of componets"))
while i > 0:
   name = input("Input the componet:")
   mole_fraction = eval(input("Input the component mole fraction:"))
   molecular_weight = eval(input("Input the molecular weight:"))
   spg = eval(input("Please input the specific gravity"))

   pc2 = math.exp(0.01901 - 0.0048442*mole_fraction + 0.13239*spg + 227/mole_fraction - 1.1663/spg + 1.2702*np.log(mole_fraction))
   print("Pc:",pc2)

   tc2 = -1247.4 + 0.792*mole_fraction + 1971*spg - 27000/mole_fraction + 707.4/spg
   print("Tc:",tc2)

   yi1 = mole_fraction * molecular_weight
   print("MW*Yi=",yi1)
   mwa += yi1 
   yi2 = mole_fraction * pc
   print("Pc*Yi",yi2)
   ppc += yi2 
   yi3 = mole_fraction * tc
   print("Tc*Yi",yi3)
   tpc += yi3 
   print("-------------------------------------------")
   i-= 1

print("MWA:", mwa)
print("PPC:", ppc)
print("TPC:", tpc)

z = eval(input())
r = eval(input())

import matplotlib.pyplot as plt
a.clear()
pres = list(range(0,4000,200))
pres_new = np.array(pres)
a = []
pr=0.0

while pr<4000:

    gas = pr*mwa/(z*r*t)
    a.insert(b,gas)
    b+=1
    pr+=200

print(len(pres),len(a))
plt.plot(pres,a)
plt.show()

There is probably a much more efficient way to accomplish this but I don't know it. This is my first time posting a question so please forgive me if it's not clear enough. Also, I have been putting this together in juypter notebook.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source