Project

To write a program in MATLAB/OCTAVE to fit a linear, quadratic and cubic polynomial for the Cp data.

AIM:

To write a program in MATLAB/OCTAVE to fit a linear, quadratic and cubic polynomial for the Cp data.

EQUATION/FORMULA USED:

Linear equation for linear curve fit:

`y = a.x + b`

Quadratic equation for quadratic curve fit:

`y = a.x^2 + b.x + c`

Cubic equation for cubic curve fit:

`y = a.x^3 + b.x^2 + c.x + d`

OBJECTIVE:

  1. To plot the linear, quadratic and cubic fit curves along with the raw data points.
  2. All the plots must be plotted with proper title, axes labelled and legends.
  3. To explain the parameters used to measure the fitness characteristics for all the curves.

THEORY:

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.

Matlab has two functions, polyfit and polyval, which can quickly and easily fit a set of data points with a polynomial.

Polyfit is a Matlab function that computes the least squares polynomial for a given set of data. Polyfit generates the coefficients of the polynomial, which can be used to model a curve to fit the data.

Polyval evaluates a polynomial for a given set of x values. So, Polyval generates a curve to fit the data based on the coefficients found using polyfit.

In this program, we will observe that specific heat is a function of temperature. Since we assume that specific heat is constant but in this, we will get to know that specific heat is a function of temperature. Hence, specific heat varies with an increase in temperature. Specific Heat is in (KJ/Kg-K) and the temperature is in Kelvin(K).

BODY:

Code:

clear all
close all
clc

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

% curve fit

% Linear fit: cp = a*T + b
co_effs = polyfit(temperature,cp,1);
predicted_cp_1 = polyval(co_effs, temperature);

% Quadratic fit: cp = a*T^2 + b*T + c
co_effs = polyfit(temperature,cp,2);
predicted_cp_2 = polyval(co_effs, temperature);

% Cubic fit: cp = a*T^3 + b*T^2 + c*T + d
co_effs = polyfit(temperature,cp,3);
predicted_cp_3 = polyval(co_effs, temperature);

%sse
 
 sse1=0;
 sse2=0;
 sse3=0;
 for i =1:length(temperature)
     sse1=sse1+(cp(i)-predicted_cp_1(i))^2;
     sse2=sse2+(cp(i)-predicted_cp_2(i))^2;
     sse3=sse3+(cp(i)-predicted_cp_3(i))^2;
 end
 disp(sse1)
 disp(sse2)
 disp(sse3)
 sum1=0;
 
for i =1:length(temperature)
     sum1=sum1+cp(i);
   
end

%rsquare
mean=sum1/length(temperature);
ssr1=0;
ssr2=0;
ssr3=0;

for i =1:length(temperature)
     temp1=(predicted_cp_1(i)-mean)^2;
     ssr1=ssr1+temp1;
     
     temp2=(predicted_cp_2(i)-mean)^2;
     ssr2=ssr2+temp2;
     
     temp3=(predicted_cp_3(i)-mean)^2;
     ssr3=ssr3+temp3;
end

r1=ssr1/(ssr1+sse1)
r2=ssr2/(ssr2+sse2)
r3=ssr3/(ssr3+sse3)

%rsme
rmse1=(sse1/length(cp))^.5
rmse2=(sse2/length(cp))^.5
rmse3=(sse3/length(cp))^.5

% Plots
% Compare my curve fit with the original data

figure(1)
plot(temperature,cp,'linewidth', 1)
hold on
plot(temperature,predicted_cp_1,'--','linewidth', 2.5)
str1 = strcat('R square  =  ', num2str(r1), '     RMSE  =  ',num2str(rmse1));
text(1000,1000,str1,'Color','r','FontSize', 12)
legend('Original dataset', ' Linear curve fit')
xlabel('Temperature [K]')
ylabel('Specific Heat [Kj/Kmol-K]')
title('Actual VS Liner Relation prediction')

figure(2)
plot(temperature,cp,'linewidth',(1))
hold on
plot(temperature,predicted_cp_2,'--','linewidth', 2.5)
str1 = strcat('R square  =  ', num2str(r2),'      RMSE  =  ',num2str(rmse2));
text(1000,1000,str1,'Color', 'r', 'FontSize', 12)
legend('Original dataset', 'Quadratic curve fit')
xlabel('Temperature [K]')
ylabel('Specific Heat [Kj/Kmol-K]')
title('Actual VS Quadratic Relation Prediction')

figure(3) 
plot(temperature,cp,'linewidth',(1))
hold on
plot(temperature,predicted_cp_3,'--','linewidth', 2.5)
str1 = strcat('R square  =  ',num2str(r3),'     RMSE  =  ',num2str(rmse3));
text(1000,1000, str1,'Color', 'r', 'FontSize', 12)
legend('Original dataset', 'Cubic curve fit')
xlabel('Temperature [K]')
ylabel('Specific Heat [Kj/Kmol-K]')
title('Actual VS cubic Relation Prediction')

figure(4) 
plot(temperature,cp,'linewidth', 2.5)
hold on
plot(temperature,predicted_cp_1,':','linewidth', 2.5)
plot(temperature,predicted_cp_2,'--','linewidth', 2.5)
plot(temperature,predicted_cp_3,'-.','linewidth', 2.5)
xlabel('Temperature [K]')
ylabel('Specific Heat [Kj/Kmol-K]')
legend('Original dataset', ' Linear curve fit', 'Quadratic curve fit', 'Cubic curve fit')
title('Actual VS All relation predictions')

STEP-WISE EXPLANATION OF THE CODE:

  • This program is written and executed using octave.
  • Starting with all the general command e. clear all, close all & clc.
  • Inputting all the code comment at all the required location starting with %.
  • Using the given data file, executing it in the program and also mentioning its correct position.
  • Now writing the code to fit a linear, quadratic and cubic polynomial for the Cp data.
  • Then write the code for sse, followed by rsquare and rsme.
  • Now the plotting of the graph starts for the comparison of the predicted curve fit with the original data.
  • The graph is plotted, keeping in mind all the necessary requirements. e.g. labels, title, legends, etc.

ERROR FACED WHILE PROGRAMMING:

  • First error: Written the wrong command.
  • Second error: Here I made some typing error in command.

STEP TAKEN TO OVERCOME THOSE ERROR:

  • First error: Rectified and written the correct command.
  • Second error: Rectified my typing error in command.

SCREENSHOT OF ERROR:

First error:

Second error:

OUTPUT:

  1. ACTUAL V/S LINEAR RELATION PREDICTION

2. ACTUAL V/S QUADRATIC RELATION PREDICTION

3. ACTUAL V/S CUBIC RELATION PREDICTION

4. ACTUAL V/S ALL RELATION PREDICTIONS

5. THEORETICAL VALUES

RESULT:

Hence, we have successfully calculated the required curve fit and also successfully plotted the graph.

Questions:

1. How to make a curve fit perfectly?

ANS. The perfect fit means that the curve should fit the original without showing any errors or very minimal error.

2. How to get the best fit?

ANS. The sum of square due to error (SSE), R-square, Adjusted R-square, Root mean square error.

3. What can be done to improve the cubic fit?

ANS. Division method can minimize the error up to a high extent and hence this method is used to improve the cubic fit.

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.