Curve Fitting
AIM
To write a code for linear and cubit curve fit.
THEORY
To draw best linear and cubic fit, I use the polyfit command. with the help of this command I can able to solve the parameters of our required fit by providing raw data. Now using this coefficient into Ployval Command I solve the required eqn. and plot it.
OBJECTIVE
To draw and explain about the curve fitting
BODY
Code
- raw=original data
- temp=Temperature
- Cp=specific heat
- coeff,coeff1=coeffficient of linear and cubic fit eqn
- lin_fit_Cp=solution of linear fit
- cub_fit_Cp=solution of cubic fit
clc;
close all;
clear all;
%loading of raw data and assigning
raw=load('data');
temp=raw(:,1);
Cp=raw(:,2);
%Linear fit curve
coeff=polyfit(temp,Cp,1);
lin_fit_Cp=polyval(coeff,temp);
%cubic fit curve
coeff1=polyfit(temp,Cp,3);
cub_fit_Cp=polyval(coeff1,temp);
%plotting
%plot of raw data
plot(temp,Cp,'m','linewidth',2)
hold on
%plot of linear fit curve
plot(temp,lin_fit_Cp,'LineWidth',2,'Color','k')
%plot of cubic fit curve
plot(temp,cub_fit_Cp,'LineWidth',2,'Color','b')
xlabel('Temperature');
ylabel('Specific heat');
title('Temp. vs specific heat');
legend('raw','linear fit','cubic fit','location','northwest')
Output
EXPLANATION
As we can see that as we increase the degree of polynomial fit it fitted more closely to perfect fit i.e the error start decreasing. But the problem of high degree polynomial is that instead of least error they gives unpredicted wiggle in the graph which is insignificant. Therefore we cannot use the higher degree polynomial for curve fit.
Answers
Q)How to make a curve fit perfectly?
A)To make a curve fit perfectly we have to use local function for every interval instead of global function. By doing this we can make our curve fit perfectly. The one method of doing this is Splines. We can use spline command in matlab to make our curve fit perfectly.
Q)How to get the best fit?
A)To get the best fit we have to minimise the error. The most preferable error method is Root Mean Square error. We can minimise this error by equating its derivative to zero. By doing so we can able to find the parameters for our required fit.
Reference-https://www.youtube.com/watch?v=5bCM5hcr5k4
Q)What could be done to improve the cubic fit?
A)To improve the cubic fit, We can provide more constrained like double derivatives is equal to zero at end point or We can try to reduce the root mean square error or our guessing for initial values for many solution curve must be close to correct values fit.