Challenge

Week 5 - Curve fitting

#case 1
# Linier Curve fitting

import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit

def read_file():
    temperature=[]
    cp=[]

    for line in open('data','r'):
        a=line.split(',')
        temperature.append(float(a[0]))
        cp.append(float(a[1]))
    return[temperature,cp]

temperature,cp=read_file()

def linear(T,a,b):
    return(a*T+b)

popt1,pcov1=curve_fit(linear,temperature,cp)
linear_fit_cp=linear(np.array(temperature),*popt1)


plt.figure(1)
plt.plot(temperature,cp,color='r',linewidth=3,label='orignal data')
plt.plot(temperature,linear_fit_cp,color='b',linewidth=3,label='Curve fit')
plt.legend(['Actual Data','Curve Fit'])
plt.xlabel('temperature [K]')
plt.ylabel('cp')
plt.savefig('curve fit using linear polynomial.png')
plt.show()

      





# case 2
#cubic ploynomial function


import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit

def read_file():
    temperature=[]
    cp=[]

    for line in open('data','r'):
        a=line.split(',')
        temperature.append(float(a[0]))
        cp.append(float(a[1]))
    return[temperature,cp]

temperature,cp=read_file()


#cubic polynomial
def cubic(T,a,b,c,d):
    return a*pow(T,3)+b*pow(T,2)+c*T+d

popt2,pcov2=curve_fit(cubic,temperature,cp)
cubic_fit_cp=cubic(np.array(temperature),*popt2)


plt.figure('cubic polynomial')
plt.plot(temperature,cp,color='r',linewidth=3,label='orignal data')

plt.plot(temperature,cubic_fit_cp,color='b',linewidth=3,label='Curve fit')
plt.legend(['Actual data','Curve fit'])
plt.xlabel('temperature [K]')
plt.ylabel('Curve fit')
plt.savefig('cubic ploynomial curve fit')
plt.show()

Question 1

popt means an array of optimum values for the given parameter which minimizes the sum of squres of the remaining

pcov means the second array the estimated covariance of popt. the diagonals provides the variance of the parameter estimated

Question 2

Np.array(temperature) mean the grid of values,all values in the grid are of same type are place in a same tuple.

Question3

*popt meant to know the uncertainty of the cure fit we determine the uncertainty of the temperature

Question4

linear polynomial curvefit it is the curve fit in which the we find the optimal set of parameters for a define function that best fits a given set of observation

cubic polynomial curve fit - in this type of curve fit we are finding the optimal set by increasing the value of the temperature and also making changes in the given function

Question 5

the perfect curvefit means the curve should match the orignal curve without showing any difference in the curve for the respective degree of polynomial

Question 6

As the data is given in the callenge question the curve fit for the linear polynomial is not much accurate to the real values but in cubic polynomial the actual and estimated curve are having more accuracy

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.