Project

Curve fitting using MATLAB

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. Curve fitting can involve either interpolation where an exact fit to the data is required, or smoothing in which a "smooth" function is constructed that approximately fits the data.

Explaination of code :

1.First we need to load the temperature and specific heat values using load function

2.Then we need to define the temparature variable and the specific heat
variable and select the column from the data set for temperature and specific heat respectively.

3. Next, we plot the ideal curve, using the data from data set.

4. The we need to fit a curve to the graphical representation. We need to convert the graphical data into mathematical form, providing us a proper
mathematical relation between the values. Consider the equation
cp = aT^2 +bT + c.

5. First, we find the linear curve, hence we keep order of the mathematical equation as 1. Using polyfit and polyval commands, we generate the plot.

6. Then, we find the cubic curve, by keeping the order of the equation as 6, as it has been observed that, the lower orders don't give a fit furve closer to the ideal curve.

7. Errors are found by sqauring the absolute values of the difference between ideal and linear or cubic curves and dividing them with the number of values.

8.The error between ideal and linear curve is more, but the error between, cubic and ideal curve is minimum. Hence, as we increase the order of the polynomial equation, we get a best fit curve.

Code :

% Curve fitting

clear all
close all
clc

%First we need to load the temperature and specific heat values.
cp_data = load('data');

%Now we need to define the temparature variable and the specific heat
%variable.

temperature = cp_data(:,1); %Here we input first column of data into the variable temperature
cp = cp_data(:,2) % Here we input second column of the data into the variable cp

% Plotting,

%Now we can find the graphical relation between the values of tempearature
%and cp
plot(temperature, cp, 'linewidth', 4);
xlabel('Temperature')
ylabel('Specific heat')
hold on


% Now we need to fit a curve to the graphical representation. We need to
% convert the graphical data into mathematical form, providing us a proper
% mathematical relation between the values. Consider the equation
%cp = aT^2 +bT + c
%Inorder to find the cp, we first need to find the value of the coefficients,

%Finding linear curve,
coeff1 = polyfit(temperature, cp, 1);
predicted_cp_linear = polyval(coeff1, temperature)
plot(temperature, predicted_cp_linear, 'linewidth',3, 'color', 'y')
hold on

%Finding the cubic curve.
coeff2 = polyfit(temperature, cp, 6); %Now we have the values of coefficients
predicted_cp_cubic = polyval(coeff2, temperature), % Now we have a predicted value of cp by mathematical form
plot(temperature,predicted_cp_cubic,'linewidth',3,'color','g');
legend('Original dataset','Linear','Cubic');

%Errors
%Finding error between linear and original curve.
Error1 = sum(abs(cp - predicted_cp_linear).^2)/3200;
Error2 = sum(abs(cp - predicted_cp_cubic).^2)/3200;

1. Best fit : Upon finding the curve which satisfies the data point, there are four methods which helps us evaluate the goodness of fit criteria.

1. The sum of sqaures due to errors

2. R- square

3. Adjusted R- Square

4. Root mean square method.

2. Perfect fit : Perfect fit means, the should fit the original curve without showing any errors (such as centering and scaling erros) in that perticular degree of polynomial. Perfect fit can always be a best fit but best fit can not be a perfect fit.

3. Improvement of the cubic curve : It can be obtained by increasing the order of the polynomial equation, hence in the above problem.

Output curves(Ideal, cubic and linear) :

Output value of error between the ideal and the linear curve :

Output value of error between the ideal and the cubic curve of 6th order :

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.