Project

Curve Fitting using Matlab

Introduction:

Curve fitting is the method of constructing a curve to co-relate the real series of data and the mathematical function representing the curve fit. 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.

Fitting function to data points,

Most commonly, one fits a function of the form y=f(x).

1) The first degree polynomial equation is, y = ax + b.

2) The second degree polynomial equation is, y = `ax^2+bx+c`.

3) The third degree polynomial equation is, y = `ax^3 +bx^2 +cx+d`.

and so on.

Code to plot Linear Fit,

close all
clear all 
clc

%Setting up the inputs
cp_data = load('data'); %Loads the file data and stores the values in array cp_data.
temperature = cp_data(:,1); %stores the first column of data file in temperature array.
specific_heat = cp_data(:,2); %stores the second column of data file in specific_heat array.

%Curve Fit
%cp = a*T + b %General linear curve fit equation.
coeffs = polyfit(temperature, specific_heat,1); %Calculates the value of a and b coefficients and stores them in 'coeffs' array.
predicted_cp = polyval(coeffs,temperature); %Calculates the value of cp based on values of coefficients and different temperature.

%Comparing both the curves
hold on %Plots both the curve in same window
plot(temperature, specific_heat,'linewidth',3,'color','b') %Plots the temp vs cp curve.
plot(temperature,predicted_cp,'linewidth',3,'color','r') %Plots the temp vs predicted cp curve.
xlabel('Temperature(K)') %Labels the x axes 
ylabel('Specific Heat(KJ/Kmol-K)') %Labels the y axes 
legend('Original Dataset', 'CurveFit') %States the original data set and curvefit based on colours.

The code is explained by creating comments.

Output:

Errors Faced:

A simple parenthisis error was encountered in load command which was overcomed by adding single inverted comma.

Code to plot quadratic fit,

close all
clear all 
clc

%Setting up the inputs
cp_data = load('data'); %Loads the file data and stores the values in array cp_data.
temperature = cp_data(:,1); %stores the first column of data file in temperature array.
specific_heat = cp_data(:,2); %stores the second column of data file in specific_heat array.

%Curve Fit
%cp = a*T^2 + b*T + c %General quadratic curve fit equation.
coeffs = polyfit(temperature, specific_heat,2); %Calculates the value of a, b and c coefficients and stores them in 'coeffs' array.
predicted_cp = polyval(coeffs,temperature); %Calculates the value of cp based on values of coefficients and different temperature.

%Comparing both the curves
hold on %Plots both the curve in same window
plot(temperature, specific_heat,'linewidth',3,'color','b') %Plots the temp vs cp curve.
plot(temperature,predicted_cp,'linewidth',3,'color','r') %Plots the temp vs predicted cp curve.
xlabel('Temperature(K)') %Labels the x axes 
ylabel('Specific Heat(KJ/Kmol-K)') %Labels the y axes 
legend('Original Dataset', 'CurveFit') %States the original data set and curvefit based on colours.




The code is explained by creating comments.

Output,

Code to Plot Cubic Fit,

close all
clear all 
clc

%Setting up the inputs
cp_data = load('data'); %Loads the file data and stores the values in array cp_data.
temperature = cp_data(:,1); %stores the first column of data file in temperature array.
specific_heat = cp_data(:,2); %stores the second column of data file in specific_heat array.

%Curve Fit
%cp = a*T^3 + b*T^2 + c*T + d %General cubic curve fit equation.
coeffs = polyfit(temperature, specific_heat,3); %Calculates the value of a, b, c and d coefficients and stores them in 'coeffs' array.
predicted_cp = polyval(coeffs,temperature); %Calculates the value of cp based on values of coefficients and different temperature.

%Comparing both the curves
hold on %Plots both the curve in same window
plot(temperature, specific_heat,'linewidth',3,'color','b') %Plots the temp vs cp curve.
plot(temperature,predicted_cp,'linewidth',3,'color','r') %Plots the temp vs predicted cp curve.
xlabel('Temperature(K)') %Labels the x axes 
ylabel('Specific Heat(KJ/Kmol-K)') %Labels the y axes 
legend('Original Dataset', 'CurveFit') %States the original data set and curvefit based on colours.

The code is explained by creating comments.

Output :

From the above 3 curvefits it can be observed that by increasing the order of polynomial the error between the curvefit and the original dataset is reduced.

Questions,

1) How to make a curve fit perfectly?

Curve fit can be made perfect by interpolating the datasets. By interpolation the errors can be minimized and it is also possible to reduce the errors to zero.

2) How to get the best fit?

The best fit can be obtained by increasing the order of polynomial and also by splitting the curve fits into multiple number of polynomials for different sets of temperature values. The best fit can also be chosen by using 4 different methods,

They are,

  • The sum of squares due to error (SSE).
  • R-square.
  • Adjusted R-square.
  • Root mean squared error (RMSE).

3) What could be done to improve the cubic fit?

The cubic fit can be improved by splitting the curve fits into multiple number of polynomials for different sets of temperature values. It can also be improved by reducing the error between the original dataset and curve fit value.

References:

https://en.wikipedia.org/wiki/Curve_fitting

https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860

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.