Project

Curve Fitting Using MATLAB

Aim: Program for curve fitting and to determine the way through which we can reduce the error while curve fitting using MATLAB.

Minimizing error:

There are two ways to decrease the error and it includes:

1. Increasing the degree of polynomial.

2. Dividing the temperature value in small divisions.

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.

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 increase in temperature. Specific Heat is in (KJ/KgK) and temperature is in Kelvin(K).

i.e: cp = f(t).

Equations: Basic equation of polynomial are used are as followed:

`y=a*x+b`

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

`y=a*x^3+b*x^2+c`

The following equation are of the linear, square and cubic polynomial equations where a, b & c are coefficients.

Steps:

Step 1: At the start of the program we will load the ‘data’ file that is saved in the directory folder.

Step 2: Next we will arrange ‘cp’ and ‘temp’.

Step 3: Now, we will start to calculate the linear equation coefficients and calculating the predicted value of cp. The same step is applied for calculating cp in case of squared and cubic.

Step 4: Now, we will plot each of the equation with respect to the actual curve using the plot command.

Step 5: Now, we got the plots but error is still unknown. Now, we will calculate the error by subtracting the actual value from the predicted value for all of the equations. Next we will calculate the percentage error using the ‘abs’ command.

Step 6: Now, we will use the division method to calculate the predicted value of cp. In our method we will use the 6 division to calculate predicted cp for some specific division.

Note: for the 6 division we have to specify the actual cp and temperature for that case.

Step 7: Now, since we have got the predicted cp value for the specific divisions now we will again plot it against the newly calculated value of cp and temp.

Step 8: Now our final step is to predict the maximum error in all the four cases. We will use ‘max’ command to calculate the maximum error.

MATLAB code:

close all
clear
clc

%Preparing Data
cp_data = load('data');
temp = cp_data(:,1);
cp = cp_data(:,2);

%Plot
plot(temp,cp,'linewidth',2);
xlabel('temperature in K');
ylabel('cp in KJ/KgK');

%Curve fit
%cp = a*t + b
co_eff1 = polyfit(temp,cp,1);
predicted_cp1 = polyval(co_eff1,temp);

%cp = a*t^2 + b*t + c
co_eff2 = polyfit(temp,cp,2);
predicted_cp2 = polyval(co_eff2,temp);

%cp = a*t^3 + b*t^2 + c*t + d
co_eff3 = polyfit(temp,cp,3);
predicted_cp3 = polyval(co_eff3,temp);

%Compare the Actual curve with predicted curve
figure(1)
hold on
plot(temp,predicted_cp1,'linewidth',2)
xlabel('temperature in K');
ylabel('cp in KJ/KgK');
legend('Actual Curve','Predicted Curve 1')
title('Curve fitting using linear method')
hold off

%Finding error corresponding to no division
Error_cp1 = (cp - predicted_cp1);
err1 = max(Error_cp1);
disp('The error corresponding to no division for linear');
disp(err1)

figure(2)
plot(temp,cp,'linewidth',2);
hold on
plot(temp,predicted_cp2,'linewidth',2)
xlabel('temperature in K');
ylabel('cp in KJ/KgK');
legend('Actual Curve','Predicted Curve 2')
title('Curve fitting using squared method')
hold off

%Finding error corresponding to no division
Error_cp3 = (cp - predicted_cp2);
err2 = max(Error_cp3);
disp('The error corresponding to no division for squared');
disp(err2)


figure(3)
hold on
plot(temp,cp,'linewidth',2);
plot(temp,predicted_cp3,'linewidth',2)
xlabel('temperature in K');
ylabel('cp in KJ/KgK');
legend('Actual Curve','Predicted Curve 3')
title('Curve fitting using cubic method')
hold off

%Finding error corresponding to no division
Error_cp3 = (cp - predicted_cp3);
err3 = max(Error_cp3);
disp('The error corresponding to no division for cubic');
disp(err3)

%Finding the Error

Error1 = cp - predicted_cp1;
Error2 = cp - predicted_cp2;
Error3 = cp - predicted_cp3;

%Percentage Error

Error_linear = abs(Error1./cp)*100;
Error_squared = abs(Error2./cp)*100;
Error_cubic = abs(Error3./cp)*100;

%Table
T1 = table(cp,predicted_cp1,Error1,Error_linear);
T2 = table(cp,predicted_cp2,Error2,Error_squared);
T3 = table(cp,predicted_cp3,Error3,Error_cubic);

%Division
cp2 = cp(900:1500);
temp2 = temp(900:1500);

co_eff2 = polyfit(temp2,cp2,1);
predicted2 = polyval(co_eff2,temp2);

figure(4)
hold on
plot(temp2,cp2,'linewidth',3);
plot(temp2,predicted2,'linewidth',3);
xlabel('temperature in K');
ylabel('cp in KJ/KgK');
legend('original value','predicted val 2')
title('Curve fitting using number of division')
hold off

%Finding error corresponding to no division
error = (cp2-predicted2);
err = max(error);
disp('The error corresponding for number of division');
disp(err)

Results:

Linear Curve Fitting

Squared Curve Fitting

Cubic Curve Fitting

Fitting Corresponding to number of division

Errors:

The error corresponding to no division for linear

27.9151

The error corresponding to no division for squared

20.9318

The error corresponding to no division for cubic

9.6525

The error corresponding for number of division

0.8868

Errors or warning in the program:

Questions:

1.How to make a curve fit perfectly?

Ans: In order to make a curve fir perfectly we have to consider error estimation as well.Perfect fit means that the curve should fit the original without showing any errors. Second way is to divide into divisions so as to minimise the error.

2.How to get the best fit?

Ans: Upon finding a curve which satisfies the data points there are 4 quantities which help us measure the goodness of fit criteria or how well the equation is representing the datapoints. There are,

  • The sum of square due to error (SSE)
  • R-square
  • Adjusted R-square
  • Root mean squre error

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

Ans: The best way to improve the cubic fit is to solve it using divison method which can minimize the error upto a high extent.

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.