![]() |
Polyfit Error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Polyfit Error (/thread-25977.html) |
Polyfit Error - Tobes28 - Apr-17-2020 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: 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! RE: Polyfit Error - Larz60+ - Apr-17-2020 the error occurs on line 19. refer to documentation here: https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html#numpy.polyfit there are two required x array, y array, and degree of fit. what is dens? RE: Polyfit Error - Tobes28 - Apr-18-2020 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? RE: Polyfit Error - Larz60+ - Apr-18-2020 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 |