Project

Curve fitting Matlab

AIM:- Write code to fit a linear and cubic polynomial for the Cp data.

DISCREPTION:- Curve fitting is the process of constructing a curve or mathematical function that best fits the data points, possibly subjected to constraints. It helps us in understanding the type of relationship between the set of data and helps in predicting the unknown values.

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

1) The first degree , y=a⋅x+b.y=a⋅x+b.

2) The second degree , y = a⋅x2+b⋅x+c⋅a⋅x2+b⋅x+ca⋅x2+b⋅x+c⋅a⋅x2+b⋅x+c.

3) The third degree , y = a⋅x3+b⋅x2+c⋅x+d⋅ax3+b⋅x2+c⋅x+d.a⋅x3+b⋅x2+c⋅x+d⋅ax3+b⋅x2+c⋅x+d.

Questions to Answers

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.

coding:-

% firstly for linear ploting
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','g') %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.

output:-

CODE 2 : -for quatratic plot

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','r') %Plots the temp vs cp curve.
plot(temperature,predicted_cp,'linewidth',3,'color','g') %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.

output 2:-

CODE :- for cubic plot

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','y') %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.

OUTPUT 3:-

CONCLUSION:- here we successfully obersverd that how can we minimize error function by using different types of polynomial equation with given data.

I firstly have done curve fitting using linear polynomial. And the result was, i got straight line as my curve and value of maximum error was much higher as you can see in figure 1.

Secondly, i increased the order of polynomial to three, so i used cubic polynomial for my curve fitting, and that resulted in a very less error as you can see in figure 3.

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.