Project

Curve Fitting using Matlab.

Objective:

  • To write code to fit a linear and cubic polynomial for the Cp data.
  • To plot the linear and cubic fit curves along with the raw data points.
  • To explain the parameters used to measure the fitness characteristics for both the curves.
  • To find the error and try to reduce that error with necessary steps and explanation.

Introduction:

What is curve fitting?

Curve fitting is the process of constructing a curve or mathematical function, that has the best fit to a series of the data point, 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.

How the best curve fit is achieved?

We use three different estimators to fit the function: linear regression with polynomial features of degree 1, 4 and 15. We see that the first estimator can at best provide only a poor fit to the samples and the true function because it is too simple (high bias), the second estimator approximates it almost perfectly and the last estimator approximates the training data perfectly but does not fit the true function very well, i.e. it is very sensitive to varying training data (high variance).

Bias and variance are inherent properties of estimators and we usually have to select learning algorithms and hyperparameters so that both bias and variance are as low as possible. Another way to reduce the variance of a model is to use more training data. However, you should only collect more training data if the true function is too complex to be approximated by an estimator with a lower variance.

In the simple one-dimensional problem that we have seen in the example, it is easy to see whether the estimator suffers from bias or variance. However, in high-dimensional spaces, models can become very difficult to visualize. For this reason, it is often helpful to use the tools described below.

Code:

clear all
close all
clc

%loading file

cp_data = load('data (1).txt');

% assigning values of temperature and specific heat
T = cp_data(:,1);
cp = cp_data(:,2);

% linear polynomial
% cp = a*T+b

coeff_1 = polyfit(T,cp,1);
predicted_cp_1 = polyval(coeff_1,T);

% cubic polynomial
% cp = a*T^3+b*T^2+c

coeff_2 = polyfit(T,cp,3);
predicted_cp_2 = polyval(coeff_2,T);

% Plotting
figure(1)
hold on
plot(T,cp,'linewidth',3,'color','r')
plot(T,predicted_cp_1,'linewidth',3,'color','b')
plot(T,predicted_cp_2,'linewidth',3,'color','k')
xlabel('Temperature')
ylabel('Specific Heat')
legend('original cp','linear cp','cubic cp')
hold off
n = length(T);
% error calculation
% error in linear polynomial
E1 = sum(abs(cp-predicted_cp_1).^2)/n

% error in cubic plonomial
E2 = sum(abs(cp-predicted_cp_2).^2)/n

% errors resolved
% if order of polynomial is increased to 6 we may get less error
% cp = a*T^6+b*T^5+c*T^4+d*T^3+e*T^2+f

coeff_3 = polyfit(T,cp,6);
predicted_cp_3 = polyval(coeff_3,T);
 
% error calculation for polynomial of order 6
E3 = sum(abs(cp-predicted_cp_3).^2)/n

figure(2)
hold on
plot(T,predicted_cp_3,'linewidth',3,'color','b')
plot(T,cp,'linewidth',3,'color','r')
hold off
title('Best Curve Fit using 6th order Polynomial')
xlabel('Temperature')
ylabel('Specific Heat')
legend('6th order polynomial cp','original cp')

Explanation Of Code:

1. The command 'load(filename)' is used to load particular data. In this code we have loaded data with name 'data (1)'.

2. Now the first column of the data file is assigned to temperature and second column to Specific heat(cp).

3.The command 'polyfit(x,y,n)' returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1 .

4. The command 'polyval(p,x)' evaluates the polynomial p at each point in x. The argument p is a vector of length n+1 whose elements are the coefficients (in descending powers) of an nth-degree polynomial.

5. Predicted cp is calculated for both linear polynomial and cubic polynomial in order to plot curve fitting.

6. Plotting is done for original cp and the predicted cp for both linear and cubic polynomial.

7. Now errors are calculated for both linear and cubic polynomial by substracting predicted cp from original cp and summing up their squares.

8. From analyzing E1 and E2 we can say that as degree of polynomial is increased there is a decrease in errors.

9. Hence in order to minimize the error, we have calculated predicted cp for 6th order polynomial and our error is reduced to 2.6038.

Plots:

Conclusion :

From the above plots, we can say that in order to get the best curve fitting, we have to increase the order of polynomials which reduces error and gives us the best curve fit.

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.