Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Polyfit Error
#1
Hi everyone,

I am writing a bit of code whose purpose, essentially, is to take a data set (density varying with 10 different temperatures) from a CSV file using Pandas and to then polyfit the data to either cubic or quadratic form. Then define a function that returns the density for any given temperature within the range by using the coefficients obtained from the polyfit and then plot a smooth curve for density variation with temperature by adding many more additional points to the plot.

I have been able to read the data set and recall it using pandas and iloc function, however whenever I try and run the code past this stage, the program gives me the error below:

Error:
Traceback (most recent call last): File "/Users/tobyhallitt/Documents/Code/Individual Design/Thermophysical Properties/density.py", line 19, in <module> d = polyfit(T_d, dens, 3) File "<__array_function__ internals>", line 5, in polyfit File "/Users/tobyhallitt/Documents/Code/Individual Design/Thermophysical Properties/.venv/lib/python3.8/site-packages/numpy/lib/polynomial.py", line 592, in polyfit x = NX.asarray(x) + 0.0 TypeError: can only concatenate str (not "float") to str
I have tried troubleshooting myself but can't find a solution, if anyone could please help me out that would be very much appreciated. My code current is presented below (using Visual Studio Code):

### 16 / 04 / 2020
### Toby Hallitt
### Density property calculation for shell side fluid

# Import Pandas
import pandas as pd

# Import matplot and numpy
import matplotlib.pyplot as plt
from numpy import polyfit
from numpy import linspace

# Read Aspen Plus properties CSV file
data = pd.read_csv("denref.csv")

# Locate temperature and density variables and polyfit to find polynomial coefficients
T_d = data.iloc[2:12,0]
dens = data.iloc[2:12,2]
d = polyfit(T_d, dens, 3)

# Define function used to find density at given temperature
def density(T):
    """Outputs density as a polynomial function of a given temperature"""
    return ((d[0]) * (T**3)) + ((d[1]) * (T**2)) + ((d[2]) * T) + (d[3])

# Create additional temperature points for the curve
Tt = linspace(290.15, 340.15, 20)

# Calculate volumetric flow for every point on the curve
mass_flow = 0.03639 # kg s^-1
volumetric_flow = [mass_flow/density(t) for t in Tt]

print(volumetric_flow)

#####################
Please contact me if you have any questions. Thanks in advance!
Reply
#2
the error occurs on line 19.
refer to documentation here: https://docs.scipy.org/doc/numpy/referen...py.polyfit
there are two required x array, y array, and degree of fit.
what is dens?
Reply
#3
Dens is the set density data extracted from the CSV file. I have supplied an x array (T_d), y array (dens) and degree of fit (3). Therefore I don't see where the problem is?
Reply
#4
check contents of your arrays, that's about the limit of what I can suggest
for me, it's been a long time since I used polynomials.
I worked in spectrochemical engineering for a decade, (1980's),
for most of the (plasma) instruments that we built, for sample analysis I usually used least squares fit.
which I wrote using integer math in assembly language. Then moved into telecommunications (89) and never did any curve fitting since then, so quite rusty
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020