Project

Curve Fitting

What is a Curve Fit?

Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subject to constraints.

Answers for the questions:

1) How to make a curve fit perfectly?

Curve fit can be made perfect by interpolating the datasets. By interpolating the datas the error will be zero and there are many number of interpolation models. But in order to get a perfect fit we do interpolation, but by doing so we might not get the actual behaviour of the curve or we won't know the physics of the datas.

2) How to get the best fit?

By increasing the polynomial order we can get a best curve fit. But actaully the choice of best fit depends on the data behaviour only. The best fit can be chosen using four different error 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?

A cubic fit can be improved by reducing the error values between the estimated values and actual values.

CODE FOR LINEAR FIT:

clear all
close all
clc

% preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);

% Curve Fit
% cp = a*T + b
co_effs = polyfit(temperature,cp,1);
predicted_cp = polyval(co_effs,temperature);

% compare my curve fit with the original data
plot(temperature, cp, 'linewidth',3)
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original Data', 'Curve Fit')

CODE FOR CUBIC FIT:

clear all
close all
clc

% preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);

% Curve Fit
% cp = a*T^3 + b*T^2 + c*T + d
co_effs = polyfit(temperature,cp,3);
predicted_cp = polyval(co_effs,temperature);

% compare my curve fit with the original data
plot(temperature, cp, 'linewidth',3)
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original Data', 'Curve Fit')

CODE FOR FOURTH ORDER POLYNOMIAL:

clear all
close all
clc

% preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);

% Curve Fit
% cp = a*T^4 + b*T^3 + c*T^2 + d*T + e
co_effs = polyfit(temperature,cp,4);
predicted_cp = polyval(co_effs,temperature);

% compare my curve fit with the original data
plot(temperature, cp, 'linewidth',3)
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r')
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original Data', 'Curve Fit')

STEPWISE EXPLANATION OF THE CODE:

clear all - this command will clear all the variables from the workspace

close all - this command will close all outputs that are opened

clc - this command will clear the command window

cp_data = load ('data') - this command will load the data from a file named ‘data’ using the load command and assigning to the array called 'cp_data'

temperature = cp_data (:,1); - this command will creat an array ‘temperature’ to store the values of the temperatures from the first column of the array ‘cp_data’

cp = cp_data(:,2); - this command will creat an array ‘cp’ to store the values of the specific heat from the second column of the array ‘cp_data’

co_effs = polyfit(temperature,cp,2); - this command will calculate the values of the coefficients using ‘polyfit’ function using a second order or linear polynomial

co_effs = polyfit(temperature,cp,3); - this command will calculate the values of the coefficients using ‘polyfit’ function using a cubic polynomial

co_effs = polyfit(temperature,cp,4); - this command will calculate the values of the coefficients using ‘polyfit’ function using a fourth order polynomial

predicted_cp = polyval(co_effs,temperature); - this command will calculate the predicted values of the Specific Heat for all the values of ‘temperature’ based on the calculated values of the coefficients and the calculated values are stored in the array ‘predicted_cp’

plot(temperature, cp, 'linewidth',3) - this command will plot the graph between 'temperature' and 'cp' with a linewidth of 3

hold on - this command will plot all the cases in one graph

plot(temperature,predicted_cp,'linewidth',3,'color','r') - this command will plot the curve fit between the 'temperature' and 'predicted_cp' with a linewidth of 3 and in red color

xlabel('Temperature [K]') - this command is used to title the x-axis as 'Temperature [K]'

ylabel('Specific Heat [KJ/Kmol-K]') - this command is used to title the y-axis as 'Specific Heat [KJ/Kmol-K]'

legend('Original Data', 'Curve Fit') - and finally legend command is used to display the legend in the figure or graph

REFERENCES:

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

2) https://in.mathworks.com/matlabcentral/answers/349776-how-to-find-the-best-curve-fit-for-a-set-of-data

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.