Project

PERFORMING CURVE FITTING

AIM: To fit a linear and cubic polynomial for the Cp data and plotting the linear and cubic fit curves along with the raw data points.

OBJECTIVE:

1. Writing code to fit a linear and cubic polynomial for the Cp data.
2. Plotting the linear and cubic fit curves along with the raw data points.
3. Explaining the parameters used to measure the fitness characteristics for both the curves.

MEASURING THE FITNESS OF THE CURVE:

Here "residue" and "mean square error" are the mathematical values that are used to measure the goodness of the curve fit used.The differnece between predicted cp value and original cp value is the Residue and the mean square error is the mean of the entire errors in fitted curve which determines whether the fit is correct to data with less error.

CODE: The main code used is as below

clear all
close all
clc

% data taken from data file
%initial values of temeprature and cp 

f_data = load('data');
temp = f_data(:,1);
cp = f_data(:,2);



%linear curve fitting
co_effs_linear = polyfit(temp,cp,1);
predicted_cp_linear = polyval(co_effs_linear,temp);


%cubic curve fitting 
co_effs_cubic = polyfit(temp,cp,3);
predicted_cp_cubic = polyval(co_effs_cubic,temp);



%plotting of original and curve fitted data

plot(temp,cp,'linewidth',2,'color','k')
hold on

plot(temp,predicted_cp_linear,'linewidth',2,'color','r')
hold on

plot(temp,predicted_cp_cubic,'linewidth',2,'color','b')

title('comparesion of original data and curve fittings');
xlabel('T Temperature(in Kelvin)');
ylabel('Cp Specific heat[in KJ/Kmol-K]');
legend('Actual value','linear curve fit','cubic curve fit');


%error calculation from the in_curve function
% plotting of error

figure

error_in_fit(1) = err_fit(temp,predicted_cp_linear,cp);
hold on

error_in_fit(2) = err_fit(temp,predicted_cp_cubic,cp);

title('Residue vs Temperature Curve')
legend('linear curve fit error','cubic curve fit error')
disp('Mean square error(SME):')
disp(error_in_fit)



FUNCTION USED: Here "err_fit" function was used whose code is as below.

function error = err_fit(temp,actual,predicted)

%Residue
Residue = actual-predicted;

%Residue Plot
plot(temp,Residue)
xlabel('Temperature (K)')
ylabel('Residue')

%SquareErrors
error = 0;
for i=1:length(actual)
error = error + Residue(i)^2;
end

% mean square error
error = error/length(actual);
end

STEPS EXPLANATION:

STEP 1:

"clear all close all and clc " commands here are used clear all the previous contents and close the files and clearing the content in command window present previously respectively.

Commands " f_data = load('data');"
" temp = f_data(:,1);"
"cp = f_data(:,2);"
These commands are the initial values from the file 'data'.

STEP 2: Here "polyfit" is in built commond that gives the coefficients for "n" degree polynomial fit curve and"polyval" gives the value of the polynomial at the given temp value(here). So "predicted_cp_linear" and "predicted_cp_cubic" is giving the value of the fitted polynomial at different points.

STEP 3: After getting the predicted curve fitting values for both the linear and cubic curve fitting these are plotted with the real data on the axes using "plot" commands.

STEP 4: In this part the error in each curve fit is calculated by using self made function "err_fit" whose explanation is in the next paragraph.Here the errors in linear and cubic curve fit are stored in the array "error_in_fit".

STEP FUNCTION: The command "function error = err_fit(temp,actual,predicted)" finds the error.

"Residue = orginal-predicted;"
This commands does a simple arthimetic operation called subraction to find the residue.The "plot" command plot a graph between temperature and residue which helps to determine the goodness of the the fit.

The "for" loop command is used find the errors in the fit at the points at predicted and actual value points and the last command is used to calculate the mean square errorand this mean square error is used to find the fitting goodness of the fitting.

OUTPUT AND PLOTTED GRAPHS:

ANSWERS FOR THE QUESTIONS ASKED:
1.The best fit can be achievd by increasing the order of the polynomial by making less differnce with original data and making it smoother.But it also makes difficult to handling them.
2.The best curve fit can be achieved by having both less residue value error and having less mean square error.
3.The fit can be improved by reducing the residue error in the fit.

REFERENCES:

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

2.https://statisticsbyjim.com/regression/curve-fitting-linear-nonlinear-regression/

3.https://projects.skilllync. com/projects/Curve-Fitting-and-cri(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.