Project

Simulation of Curve fitting using MATLAB

Aim: To simulate a curve which best/perfectly fits for the given data using MATLAB

Introduction:

Curve fitting

Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subject to constraints. Curve fitting can involve either interpolation, where an exact fit to the data is required, or smoothing in which a "smooth" function is constructed that approximately fits the data.

Fig1: Example of curve fitting

NOTE: In the above graph, a data of a noisy curve creating by the points given is represented by a smooth curve which best fits all the data for representation which is knowm as 'Curve fitting'

Formulae Used

To find the error ratio between the given curve from the data and the curve fitting/best fit, following are used.

For a given curve,

R represents how successfully, the curve fits /variton from given data

  • 0 - gives worst fit for given data
  • 1 - gives best fit for fiven data

SSR - sum of squares of regression

(the square of the difference between the value of the fitting function at a given point and the arithmetic mean of the given data)

Program : To plot a curve which best fits the data and measure how well it fits.

%Curve fit
clear all
close all
clc

%Data defining
data=load('data');
temperature=data(:,1);
cp=data(:,2);

%Ploting
plot(temperature,cp,'linewidth',3,'color',[0 0.3 0.8])
xlabel('Temperature (K)')
ylabel('Specific Heat')


%Curvefit

%Cp= a*T^2+b*T+c
co_ef1=polyfit(temperature,cp,1);  %( 1 represnts single order eqn which makes a linear curve)
predict_cp1=polyval(co_ef1,temperature);

co_ef2=polyfit(temperature,cp,3);  %(3 represents high order eqn,gives a best fit curve)
predict_cp2=polyval(co_ef2,temperature);


%comparing given and predicted data set

hold
plot(temperature,predict_cp1,'linewidth',2,'color','r')
plot(temperature,predict_cp2,'linewidth',2,'color','g')
title('Curve Fitting')
legend('Given data','Linear','Best fit')

%to find error variation:
%R^2= SSR/SST (from the formula)
mean=sum(cp)/length(cp)

SSR1=sum((predict_cp1-mean).^2)
SSR2=sum((predict_cp2-mean).^2)


%SST=SSE+SSR

SSE1= sum((cp-predict_cp1).^2)
SSE2= sum((cp-predict_cp2).^2)

SST1= SSR1+SSE1
SST2= SSR2+SSE2

R_1= SSR1/SST1
R_2= SSR2/SST1

How to make a perfect fit?

For making a perfect curve , we split the data into number of small sector of values ('split-wise') and curve fitting for each sector which gives a pefectly fitted curve for the data.

Program: To plot a perfect fit curve for the given data

%perfect fit
clear all 
close all
clc

% to make a split wise execution to plot a perfect curve

data=load('data');
temp=data(:,1);
cp=data(:,2);

%plotting given data
plot(temp,cp,'linewidth',3,'color','b')
xlabel('Temperature (K)')
ylabel('Specific Heat')
hold on

x=1;
y=800

%creating for loop for series of iteration in spliting
for i=1:4
    co_eff(i,:)=polyfit(temp(x:y),cp(x:y),2);  (%(:,i) assigning Array for defining values into the ouput)
    cp_predict(i,:)=polyval(co_eff(i,:),temp(x:y));
    
     plot(temp(x:y),cp_predict(i,:),'linewidth',3) 
    legend('Given data','Perfect fit')
    x=x+800; %(condition for telling the loop to proceed to next action)
    y=y+800;
end 

OUTPUT:

  • For a Best fit curve

  • Measure of quantity for how well it fits (R)

  • For a Perfect fit curve

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.