CURVE FITTING
AIM
To write a code to fit linear and cubic polynomial for the given data and to plot linear and cubic fit curves.
CONTENT
Curve fitting is a process of constructing curves that has a best fit towards the series of data points. It can also be used to describe how a data change mathematically.
In this program we are going to compare different values of temperature and specific heat for Oxygen.
The temperature and specific heat values are given in document as "DATA". The 'LOAD' command in MATALAB is used to load the given file.
The following is the code to perform the desired operation.
clear all
close all
clc
% Loading the given data file
cp_data = load('data') % load command is used to load the 'data' file.
temperature = cp_data(:,1) % Temperature values are taken from the first column
cp = cp_data(:,2) % Specific heat values are taken from the second column
% Curve fit for linear polynomial
co_efficient_linear = polyfit(temperature,cp,1) % polyfit command is used to generate coefficient for the given polynomial
predicted_cp_linear = polyval(co_efficient_linear,temperature) % polyval command is used to evaluate a polynomial for a given set of values.
% Plotitng
figure(1)
plot(temperature,cp,'linewidth',3) % used to plot the graph between two variables
hold on
plot(temperature,predicted_cp_linear,'linewidth',3,'color','r')
xlabel('Temperature K') % to label x-axis
ylabel('Specific heat KJ/Kg-K') % to label y-axis
legend('Original Dataset','Curve Fit') % to turn on the legend
title('CURVE FIT FOR LINEAR POLYNOMIAL') % to give the title for the graph
% Curve fit for cubic polynomial
co_efficient_cubic = polyfit(temperature,cp,3) % "3" denotes the order of the polynomial
predicted_cp_cubic = polyval(co_efficient_cubic,temperature)
% Plotitng
figure(2)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp_cubic,'linewidth',3,'color','r')
xlabel('Temperature K')
ylabel('Specific heat KJ/Kg-K')
legend('Original Dataset','Curve Fit Cubic')
title('CURVE FIT FOR CUBIC POLYNOMIAL')
Graph for linear polynomial
Graph for cubic polynomial
Graph for 5th order polynomial
Graph for 7th order polynomial
How to make a curve fit perfectly?
One of the ways to make a curve fit perfect is to increase the order of polynomial. As the order increases the error reduces.
RESULT
The graph for linear and cubic curve fit is successfully plotted.