Challenge

Week 5 - Curve fitting

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# curve fit function
def func(t, a, b, c):
    return a*pow(t,2)+b*t+c
# reading thermodynamics file
def read_file():
    temperature=[]
    cp=[]
    for line in open('data','r'):
        values=line.split(',')
        temperature.append(float(values[0]))
        cp.append(float(values[1]))
    return [temperature,cp]
# Main program
temperature,cp =read_file()
popt, pcov=curve_fit(func, temperature, cp)
fit_cp=func(np.array(temperature),*popt)
plt.plot(temperature,cp,color='blue',linewidth=3)
plt.plot(temperature,fit_cp,color='red',linewidth=4)
plt.legend(['actual data','curve fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('cp')
plt.show()

# Polynomial equation cube
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# curve fit function
def func(t, a, b, c,d):
    return a*pow(t,3)+b*pow(t,2)+c*t+d
# reading thermodynamics file
def read_file():
    temperature=[]
    cp=[]
    for line in open('data','r'):
        values=line.split(',')
        temperature.append(float(values[0]))
        cp.append(float(values[1]))
    return [temperature,cp]
# Main program
temperature,cp =read_file()
popt, pcov=curve_fit(func, temperature, cp)
fit_cp=func(np.array(temperature),*popt)
plt.plot(temperature,cp,color='blue',linewidth=3)
plt.plot(temperature,fit_cp,color='red',linewidth=4)
plt.legend(['actual data','curve fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('cp')
plt.show()

# Polynomial equation power four
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# curve fit function
def func(t, a, b, c,d,e):
    return a*pow(t,4)+b*pow(t,3)+c*pow(t,2)+d*t+e
# reading thermodynamics file
def read_file():
    temperature=[]
    cp=[]
    for line in open('data','r'):
        values=line.split(',')
        temperature.append(float(values[0]))
        cp.append(float(values[1]))
    return [temperature,cp]
# Main program
temperature,cp =read_file()
popt, pcov=curve_fit(func, temperature, cp)
fit_cp=func(np.array(temperature),*popt)
plt.plot(temperature,cp,color='blue',linewidth=3)
plt.plot(temperature,fit_cp,color='red',linewidth=4)
plt.legend(['actual data','curve fit'])
plt.xlabel('Temperature [K]')
plt.ylabel('cp')
plt.show()


Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.