Challenge

Week 5 - Curve fitting

Aim: curve fitting

  • What do popt and pcov mean?
    =The popt argument are the best-fit parameters for the coefficients
    The pcov variable contains the covariance matrix, which indicates the uncertainties and correlations between parameters. This is most useful when the data has uncertainties.
  • What does np.array(temperature) do?
    =np.array(temperature) complies the temperature list from the data as an array since operation like numpy are allowed on numpy array and not on python list
  • What does the * in *popt mean?
    =It refers to the coefficient values stored in the popt
  • Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
  • #Curve fitting
    
    import matplotlib.pyplot as plt
    import numpy as np
    from scipy.optimize import curve_fit
    
    
    #define the function to read the data 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]
    
    
    
    #define the function for curve fittng
    def fun1(t,a,b,c,d,e):
        return a*t + b
    
    def fun2(t,a,b,c,d,e):
        return a*t**2 + b*t + c
    
    def fun3(t,a,b,c,d,e):
        return a*t**3 + b*t**2 + c*t + d
    
    def fun4(t,a,b,c,d,e):
        return a*t**4 + b*t**3 + c*t**2 + d*t + e
    
    #main program
    i = -1
    temperature,cp = read_file()
    for fun in (fun1,fun2,fun3,fun4):
        popt, pcov = curve_fit(fun, temperature, cp)
        cp_fit_data = fun(np.array(temperature), *popt)
        i = i+1
        #plotting the real data and the curve fitting data
        plt.plot(temperature, cp_fit_data,color = 'red',label='build curve')
        plt.plot(temperature,cp, label = 'real curve')
        plt.grid(True)
        plt.xlabel('Temperature (kelvin)')
        plt.ylabel('C_p')
        plt.title('curve_fitting_practice')
        plt.legend(loc= 'lower center')
        plt.savefig('{}'.format(i))
        plt.show()
  • Explanation: With cubic polynomial, the curve fit tend to fit the better because with higher polynomial the curve fit tend to better and has lower errors

  • What needs to be done in order to make the curve fit perfectly? = To make the curve fit perfectly we have to minimize the error to zero which is hard to achieve so we are trying to best fit the curve by increasing from linear polynomial to cubic polynomial and check the fitness of the curve by using

    The sum of squares of regression is found by using the formula SSR=n∑i=1(f(xi)−mean)2 where f(xi)f(xi) is the predicted Cp values and then

    Then the sum of squared errors is found by using the formula SSE=n∑i=1[y(i)−f(xi)]2 then he R^2 is calculated by SSR/SST where SST is SSR+SSE

    when the R^2 value is greater than 0.95 then the fit is assumed to be good and we can check the values of the value of fitness

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.

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.