Project

Curve Fitting in MATLAB

Aim: The main goal of this project is to write a code to fit a linear and cubic polynomial for the specific heat data set, to calculate the goodness of fit using different parameters and to find out at different ways to improve the fit. Here, MATLAB code is used to generate the linear and cubic polynomial fits and by using the Curve Fitting app, the goodness of the fit can be calculated.

Introduction:

Curve fitting is the process of building a curve, or a mathematical function, which is best suited to a series of data points, possibly constraints. By curve fitting, we can mathematically construct the functional relationship between the observed data set and the parameter values etc.

Curve fitting may entail either interpolation, where accurate fit to the data is needed or smoothing, where a "smooth" function is developed that suits the data approximately.
It is highly effective in the mathematical modelling of some natural processes.
The goodness of fit of a statistical data determines how well it fits a set of observation data points.
Measures of goodness of a fit typically summarize the discrepancy between observed values and the values expected under the model in question. There are mainly four methods to quantify the goodness of a fit, they are;
1) Sum of Squares due to Error (SSE)
2) R-Square
3)Adjusted R-Square
4)Root mean square error (RMSE)
CODE:
#Curve fitting in Octave
close all
clear all
clc

%Loading the data file from directory
cp_data=load('data');

%Deffining the column for temperature and specific heat
temperature=cp_data(:,1);
cp=cp_data(:,2);


%Defining the polynomial function
%Polynomial function for linear equation
co_eff1=polyfit(temperature,cp,1);
predicted_cp1=polyval(co_eff1,temperature);
%Polynomial function for quadratic equation
%Curve fit
co_eff2=polyfit(temperature,cp,2);
predicted_cp2=polyval(co_eff2,temperature);

%Polynomial function for the cubic equation
co_eff3=polyfit(temperature,cp,3);
predicted_cp3=polyval(co_eff3,temperature);

%Plotting the graphs
plot(temperature,cp,'linewidth',3,'color','r')
hold on
plot(temperature,predicted_cp1,'linewidth',3,'color','g');
hold on
plot(temperature,predicted_cp2,'linewidth',3,'color','b');
hold on
plot(temperature,predicted_cp3,'linewidth',3,'color','y');
legend('Original Data Set','Linear','Quadratic','Cubic',"location","northeastoutside");
ylabel('Specific Heat [KJ/Kmol-K]');
xlabel('Temperature[K]');

%Error for the linear curve
error1=cp-predicted_cp1;
sse_linear=sum(error1.^2);
ssr_linear=sum((cp-mean(cp)).^2);
sst_linear=(sse_linear+ssr_linear);
rsquare_linear=ssr_linear/sst_linear;
rmse_linear=sqrt(sse_linear/3200);

%Error for the quadratic curve
error2=cp-predicted_cp2;
ssr_quadratic=sum((cp-mean(cp)).^2);
sse_quadratic=sum(error2).^2;
sst_quadratic=(ssr_quadratic+sse_quadratic);
rsquare_quadratic=ssr_quadratic/sst_quadratic;
rmse_quadratic=sqrt(sse_quadratic/3200);

%Error for the cubic curve
error3=cp-predicted_cp3;
ssr_linear=sum((cp-mean(cp)).^2);
sse_linear=sum(error3).^2;
sst_linear=(ssr_cubic+sse_cubic);
rsquare_cubic=ssr_cubic/sst_cubic;
rmse_cubic=sqrt(sse_cubic/3200);

RESULT:
PROGRAM OUTPUT:
RSquare Values:
Linear= 0.930
Quadratic=1
Cubic= 1
Rmse values:
Linear = 25.99
Quadratic = 4.26e-12
Cubic= 5.01e-12
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.