curve fitting
clear all
close all
clc
%importing the data
data = load('data');
temp = data(:,1);
cp = data(:,2);
%plotting the original data
figure(1)
plot(temp,cp,'linewidth',3,'color','b')
hold on
xlabel('temperature')
ylabel('specific heat')
%curve fitting
%assuming 2nd order equation. predicted_cp = aT + c
co_efficients_1 = polyfit(temp,cp,1)
predicted_cp_1 = polyval(co_efficients_1,temp);
%plotting 2nd order curve fit
plot(temp,predicted_cp_1,'linewidth',3,'color','r')
%curve fitting
%assuming 3rd order equation. predicted_cp = aT^3 + bT^2 + cT + d
co_efficients_2 = polyfit(temp,cp,3)
predicted_cp_2 = polyval(co_efficients_2,temp);
%plotting 3rd order curve fit
plot(temp,predicted_cp_2,'linewidth',3,'color','g')
legend('original data','linear fit','cubic fit')
title('temperature vs specific heat of oxygen')
grid on
hold off
%split wise curve fitting
co_efficients_3 = polyfit(temp(1:1600),cp(1:1600),2)
predicted_cp_3 = polyval(co_efficients_3,temp(1:1600));
co_efficients_4 = polyfit(temp(1600:3200),cp(1600:3200),3)
predicted_cp_4 = polyval(co_efficients_4,temp(1600:3200));
%plotting and comparing the original curve and split wise curve
figure(2)
plot(temp,cp,'linewidth',3,'color','b')
hold on
plot(temp(1:1600),predicted_cp_3,'linewidth',3,'color','r')
plot(temp(1600:3200),predicted_cp_4,'linewidth',3,'color','g')
legend('original data','quadratic split curve','cubic split curve')
title('temperature vs specific heat of oxygen')
grid on
hold off
The code which fits the original set of data is shown above
The graph comparing the original data curve, linear curve fit and cubic curve fit is shown above.
The graph comparing the original data curve and split wise curve is shown above
The parameters used in the codes are
temp = all the temperature data sets contained in the variable matrix 'data'
cp = all the specific heat data sets contained in the variable matrix 'data'
co_efficient_1 = contains the 2 values of the co efficients present in linear equation
co_efficient_2 = contains the 4 values of the co efficients present in cubic equation
co_efficient_3 = contains the 3 values of the co efficients present in quadratic equation
co_efficient_4 = contains the 4 values of the co efficients present in cubic equation
predicted_cp_1 = contains all the values of specif heat obtained using the co_efficient_1 and all the temperature data
predicted_cp_2 = contains all the values of specif heat obtained using the co_efficient_2 and all the temperature data
predicted_cp_3 = contains all the values of specif heat obtained using the co_efficient_3 and all the temperature data
predicted_cp_4 = contains all the values of specif heat obtained using the co_efficient_4 and all the temperature data
Perfect curve :
A curve is said to be perfect if there is no error between the original data and curve. it means that there is no distance between any data set (`underset(i)(y)`) and f(`underset(i)(x)`)
Best curve :
A curve is best fit if there is minimum error between the original data and curve. If the R square is equal to 1 then it is best curve fit.
To improve the cubic fit I have used split wise function by making half the data set quadratic and the other half cubic to avoid having the polynominal badly conditioned.
I have referred to KHAN ACADEMY for more inforation and official mathworks
program explanation
1. The data set is first imported into the program using load command
2. Data set containing temperature is stored in temp and data set containing specific temperature is stored in cp
3. The co efficients of the linear equation and cubic equation is calculated using polyfit command and stored in co_efficient_1 and co_efficient_2.
4. After calculating the co efficients of a curve the specific heat is calculated for the given co efficient of the respective curve for all the temperatures in the data set and is stored in predicted_cp_1 and predicted_cp_2
5. A graph is plotted to compare the original data set, linear curve and cubic curve.
6. Since cubic curve is badly conditioned, it is overcome by using split wise function and making one half of the data quadratic and the other half of the data cubic.
7. A comparision is made between the original data set and the split wise curve by plotting a graph