Project

Aim:To Program Linear and Polynomial Curve Fitting using Matlab

Introduction to Curve Fitting

Curve fitting is a process of formation of curve or a mathematical function,that has best fit for the set of Data points.This process can predict future trends

How to get the best fit-

To get the best fit it should have four quality

1.The sum of squares due to the error (SEE)

The one line or curve that provides a minimum error is then the ‘best’ straight line or curve fit

Error = |y(i)-f(x(i))|

Note :error can be negative or positive and it can cancel each other so to avoid this situation we square them

Sum of squared Error (SSE)= `sum_(i=1)^n er(i)^2`

For a polynomial of nth order

F(x)=a1+a2x+a3x2+…….+anxn-1

When we differentiate SEE with coefficient of equation we get the minima and value of coefficient,This is called least square method

2.R-square-

  • R-Square=`(SSR)/(SST)` (SST=SSR+SSE)

SSR=sum of square of regration

SSR= `sum_(i=1)^n (f(x(i))-mean)^2`

Square always lies between 0-1, For best it should be close to 1 (greater than 0.95 is considered as good fit

3.Adjusting R-square-

Adjusting R-square is introduced because there is one problem with R-Square ie.

Let y=a+a1x1

y=a+a1x1+a2x2+…………..+anx2

( if the number of independent feature increases the value of R square also increases but it can be significant or may be insignificant)so to keep check on this we use Adjusting R-Square

Adjusting R-Square= `1-((1-R^2)(N-1))/(N-P-1)`

N=Number of data point

P=Number of independent variable

Case 1- Added variable is insignificant-the value of Adjusting R-square decreases

Case 2-Added variable is significant-the value of Adjusting R-square Increases

4.Root mean square Error (RMSE)

RMSE=`sqrt((SSE)/n`

Program for linear Fit

clear all
close all  
clc
% Preparation of data
 
cp_data=load('data')                  %Loading the File of data set
temperature=cp_data(:,1);             %Storing all the temperature value 
cp=cp_data(:,2);                      %Storing all the specific heat value
 
 
 
%Cp=b*t+c
co_eff=polyfit(temperature,cp,1);               %For finding the cofficient
predicted_cp=polyval(co_eff,temperature);       %Predicted Value of Specific heat
 
%Plot
plot(temperature,cp,'linewidth',3)              %Plotting the original Dataset
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r') %Plotting the predicted Dataset
title('Linear Fit of Data')
xlabel('Temperature [K]');
ylabel('Specific Heat ');
legend('originaldataset','curvefit')
grid on;

Explationation of code-

Step 1- Load the file having data points using ‘load’ command

Step 2- store all the points of temperature in the variable ‘temperature’ And spesepic heat in the variable’cp’.

Step 3-Use ‘polyfit’ command to find the cofficients

Step 4-use ‘polyval’command to find the pridected value

Step 5-plot original data sets and pridected data set use legend and compare both the plots

Fig 2- Output Fig 3- Result from Curve fitting tool box

How to make a curve fit perfectly-

To make a perfect curve we should minimize the error,the error shoud be

estimated which can be done by ‘Polyfit’

[p,s]=polyfit(x,y,n)

S is a structure which can be input to ‘polyval’ to estimate error

Also remove Centering and scaling error which can done by ‘polyfit’

[p,s,mu]=polyfit(x,y,n)

mu is a two element vector with centering and scaling value

The centering and scaling transformation improves the numerical properties of both polynomial and the fitting algorithm

Program for polynomial cruve fit

clear all
close all 
clc
% Preparation of data
 
cp_data=load('data')                  %Loading the File of data set
temperature=cp_data(:,1);             %Storing all the temperature value 
cp=cp_data(:,2);                      %Storing all the specific heat value
 
 
%linear
%Cp=b*t+c
%s= error estimate
%mu=centering and scaling value
[co_eff,s,mu]=polyfit(temperature,cp,1);               %For finding the cofficient
predicted_cp=polyval(co_eff,temperature,s,mu);         %Predicted Value of Specific heat
 
%Polynomial
%Cp=a*t^2+b*t+c
[co_eff1,s,mu]=polyfit(temperature,cp,2);
predicted_cp1=polyval(co_eff1,temperature,s,mu);
 
 
 
%Plot 
plot(temperature,cp,'linewidth',3)              %Plotting the original Dataset
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r') %Plotting the predicted Dataset linear
hold on
plot(temperature,predicted_cp1,'--','color','b') %Plotting the predicted Dataset polynomial
title('Linear Fit of Data')
xlabel('Temperature [K]');
ylabel('Specific Heat ');
legend('originaldataset','curvefit linear','curvefit polynomial')
grid on;

Explationation of code-

Step 1- Load the file having data points using ‘load’ command

Step 2- store all the points of temperature in the variable ‘temperature’ And spesepic heat in the variable’cp’.

Step 3-Use ‘polyfit’ command to find the cofficients for linear fit

Step 4-use ‘polyval’command to find the pridected value of linear fit

Step 5-Similarly find the cofficient for polynomial this time ‘polyfit’ command

will also return

S=error

mu=Scaling and centering

Step 6=s,mu will be input to ‘polyval command’ so that error can be estimated.

Step 7-plot original data sets and pridected data set use legend and compare all the the plots

Fig 4-Output Polynomial curve fit Fig 5- Results for Polynomial Curve Fit

How to improve cubic curve fit

It is very important to improve curve fit and which can be done by ’Splitting the data points’ by splitting the data points in small small segment and with the help of those data points we prepare our curve fitting by this way we can improve our curve fitting.

Splitting the Data points

clear all
close all 
clc
% Preparation of data
 
cp_data=load('data');                  %Loading the File of data set
temperature=cp_data(:,1);             %Storing all the temperature value 
cp=cp_data(:,2);                      %Storing all the specific heat value
 
%Splitting the data points
%First range
temperature_1=cp_data(1:600,1);
cp_1=cp_data(1:600,2);
[co_eff_1,s,mu]=polyfit(temperature_1,cp_1,3);               %For finding the cofficient
predicted_cp_1=polyval(co_eff_1,temperature_1,s,mu);         %Predicted Value of Specific heat
 
 
%Second Range
temperature_2=cp_data(601:3200,1);
cp_2=cp_data(601:3200,2);
[co_eff_2,s,mu]=polyfit(temperature_2,cp_2,3);               %For finding the cofficient
predicted_cp_2=polyval(co_eff_2,temperature_2,s,mu);         %Predicted Value of Specific heat
 
 
 
%Plot 
plot(temperature,cp,'linewidth',10)              %Plotting the original Dataset
hold on
plot(temperature_1,predicted_cp_1,'linewidth',3,'color','r') %Plotting the predicted Dataset linear
hold on
plot(temperature_2,predicted_cp_2,'--','color','y') %Plotting the predicted Dataset polynomial
title('Curve fitting by splitting data points')
xlabel('Temperature [K]');
ylabel('Specific Heat ');
legend('originaldataset','curvefit 1','curvefit 2')
grid on;

Explationation of code-

Step 1- Load the file having data points using ‘load’ command

Step 2- store all the points of temperature in the variable ‘temperature’ And spesepic heat in the variable’cp’.

Step 3-split the whole data sets in two parts

Range 1:-1 to 600 data points Range 2:- 601 to 3200

Step 3-Use ‘polyfit’ command to find the cofficients of both the range of data sets

Step 4-use ‘polyval’command to find the pridected value of both the range of data sets

Step 5-plot original data sets and pridected data set use legend and compare both the plots

Fig 6- Output Spliting the data points Fig 7- Results From Curve fitting tool box

Conclusion:-Curve and Linear fitting of given data-set is done by using various method to make make curve fit perfect and best and incoprates all the quality of goodness in the curve fit.

References-

  1. https://in.mathworks.com/help/matlab/ref/polyfit.html
  2. https://en.m.wikipedia.org/wiki/Curve_fitting
  3. https://www.google.com/url?sa=t&source=web&rct=j&url=https://www.iiserpune.ac.in/~bhasbapat/phy221_files/curvefitting.pdf&ved=2ahUKEwi8iofK98LpAhVSzzgGHYl6CG0QFjAAegQIBBAB&usg=AOvVaw1bp_W_7FwqHYM8YvBqhRQk
  4. https://www.google.com/url?sa=t&source=web&rct=j&url=http://www.synergy.com/Tools/curvefitting.pdf&ved=2ahUKEwiZ3srk8cLpAhW1yzgGHXpBCBEQFjABegQIDRAG&usg=AOvVaw34jCX6wSwDEfhs8vfaviUJ
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.