Linear and Cubic Polynomial Curve Fitting by MATLAB programming
Aim:-
To write a code for the fitting of a linear and cubic polynomial by MATLAB programming
Objectives:-
The main objective of this project is to perform curve fitting using MATLAB programming
Brief Theory:-
Curve Fitting:- It is nothing but a method of making a curve (mathematical function) which gives the best fit to a given data(points).
Methods to make a perfect curve fit:-
- Squares sum method
- R square and adjusted R square method
- Root Mean Square Method
Method of getting the best fit:- A fit is called the best fit when there will be no error for taking into consideration.
The four quantities to make perfect fit helps to measure the goodness of the fit and this helps t get the best fit also(Best fit and the perfect fit are not similar).
Improvement of cubic fit:- It depends upon how we are using the higher-order polynomial equations.
Workflow:-
- Datas are collected to get a curve,
- Inputs values taken and datas are differentiated,
- Fitting of polynomial degree is done and coefficients of variables T is obtained
- Plotting of a curve is done
Brief content of this project:-
This project has been done by MATLAB programming. The code has been attached below:-
- Linear curve fit
%Linear curve fitting
clear all
close all
clc
%Data loading and preparation
cp_temp = load('data');
temp = cp_temp(:,1);
cp = cp_temp(:,2);
%plotting of graph
xlabel('Temperature [K]');
ylabel('cp [Kj/kmol-m]');
legend('original value','curve fit value');
%c_p = a*t^2+b*t+c
coeff_lin = polyfit(temp,cp,1);
predict_cp = polyval(coeff_lin,temp);
%compariton between original and derived
hold on
plot(temp,predict_cp,'linewidth',3,'color','r');
%Finding of error (sse method)
error = cp - predict_cp;
func = error.^2;
L_error = sum(func)
Plot for linear curve fit:-
Result:-
Algorithm of the program:-
- The screen is cleared first
- Firstly, cp values are loaded from the given file
- Temperature vs cp curve is plotted
- Legend command is used for the comparison between the original and linear curve after plotting.
- By polyfit command, the coefficients of the linear and cubic polynomial are generated
- Polyval command is used to evaluate the polynomial value.
- Predicted cp and original cp is compared
- Error is calculated by the SSE method
- Cubic curve fit
%Cubic Curve Fitting
clear all
close all
clc
%Data loading and preparation
cp_temp = load('data');
temp = cp_temp(:,1);
cp = cp_temp(:,2);
%plotting of graph
plot(temp,cp,'linewidth',3);
xlabel('Temperature [k]');
ylabel('cp [kj/kmol-k]');
legend('original value','curve fit value');
%cp = a*t^2+b*t+c
coeff_cubic= polyfit(temp,cp,5);
predict_cp = polyval(coeff_cubic,temp);
%compariton between original and derived
hold on
plot(temp,predict_cp,'linewidth',3,'color','g');
%error finding
error = cp - predict_cp;
func = error.^2;
c_error = sum(func)
Plot for cubic curve fitting:-
Result:-
Algorithm of the program:-
- The screen is cleared first
- Firstly, cp temperature values are loaded from the given file
- Temperature vs cp curve is plotted
- Legend command is used for the comparison between the original and linear curve after plotting.
- By polyfit command, the coefficients of the linear and cubic polynomial are generated
- Polyval command is used to evaluate the polynomial value.
- Predicted cp and original cp is compared
- Error is calculated by the SSE method.
- Here error is less than linear polynomial.
Conclusion:-
Through this whole project we have learnt how to code in MATlAB to fit curve. We have learnt the use of curve fitting i.e how we can predict and see the data visualization where no data are available and finally our aim of this project is fulfilled and this coding is very helpful for industrial works to predict something by a program mathematical model.