Project

Curve Fitting program

For the curve fitting task, we plot a curve from a data available regarding Temperature and Specific Heat(cp). So our code begins with few usual command for clearing the previously written data and clearing space in command window. Now with the data available in form of a file we load/read the data into our program using \"load\" command providing within the software, the prime requisite is that the file must have been save at same location of that of the program. As the data provided is regarding temp. and specific heat, we save them into seperate vectors with their resp. names. For fitting purpose we take two polynomials viz. Linear(^1) and Cubic(^3). To make this happen, we\'re using a for loop which takes the value as 1 during first iteration and 3 during second iteration i.e. the degree of the fitting polynomial. \"polyfit\" is used get the coefficient of the polynomial in terms of temperature(in this case) of degree \'n\'. The coefficient are stored in a variable \'coeff\', the o/p of the polyfit command along with the coefficient returns a structure array(s) i.e. a group for related data, which later may be used to obtain error estimates. Also it also returns \'mu\' which is a two-element vector with centering and scaling values. This centering and scaling transformation improves the numerical properties of both the polynomial and the fitting algorithm.

To get the values of new/predicted cp, \"polyval\" command helps us to get these values of cp. The i/p for the polyval are coefficients of the polynomial, point for which the polynomial is to be evaluated (temperature), s and mu. Now s and mu may not be used when the degree of this polynomial is 1 because simply there are only two variables, one is the i/p and other needs to be evaluated. But for a cubic polynomial or higher s and mu serves as an i/p as in these cases the no. of variable are more. After we evalute the values for predicted cp at different temperature values, we generate a plot which represents comparision between the original data and the predicted one.

\"\"

\"\"

As we observe both the plots, we can say that Linear polynomial fits poorly for the curve fitting purpose and Cubic polynomial is much better but still does not give a perfect fit. That may ahve occured due the degree of the polynomials used and the difference between the values of cp in original and predicted data set.

To get a perfect fit what we can do is either increase the degree of the polynomial, or use some other fitting methods. One such technique is the Curve fitting toolbox, in which we\'ve to provide the xdata and the ydata, and select the degree of the polynomial required. This app generates appropriate plot according to the data and the selection. Also we can generate a code in the form of a function file.

\"\"

main code:

clear all
close all
clc

cp_data = load(\'data\');
temperature = cp_data(:,1);
cp = cp_data(:,2);

% Curve Fitting
% Polynomial Equation for cp 
ct = 1;
for n = [1 3]
[coeff,s,mu] = polyfit(temperature,cp,n);
predicted_cp = polyval(coeff,temperature,s,mu);

% Plotting data
figure(ct)
plot(temperature,cp,\'linewidth\',2,\'color\',\'k\')
xlabel(\'Temperature [K]\')
ylabel(\'Specific heat [KJ/Mol.K]\') 
ct = ct+1;
% Comparing 
hold on
plot(temperature,predicted_cp,\'linewidth\',3)
pause(0.03)
legend(\'Regular\',\'Fitted Curve\',\'location\',\'northwest\')
if i == 1
title(\'Linear Polynomial\')
else if i == 3
        title(\'Cubic Polynomial\')
    end
end
end
perfect_fit = createFit(temperature, predicted_cp)


function generated using Curve fitting tool:

function [fitresult, gof] = createFit(temperature, predicted_cp)
%CREATEFIT1(TEMPERATURE,PREDICTED_CP)
%  Create a fit.
%
%  Data for \'Curve Fitting\' fit:
%      X Input : temperature
%      Y Output: predicted_cp
%  Output:
%      fitresult : a fit object representing the fit.
%      gof : structure with goodness-of fit info.
%
%  See also FIT, CFIT, SFIT.




%% Fit: \'Curve Fitting\'.
[xData, yData] = prepareCurveData( temperature, predicted_cp );

% Set up fittype and options.
ft = fittype( \'poly4\' );

% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, \'Normalize\', \'on\' );

% Plot fit with data.
figure( \'Name\', \'Curve Fitting\' );
h = plot( fitresult, xData, yData )
legend( h, \'predicted_cp vs. temperature\', \'Curve Fitting\', \'Location\', \'NorthWest\' );
title(\'Curve Fit using fitting tool\')
% Label axes
xlabel temperature[K]
ylabel predicted_cp[KJ/Mol.K]
grid off
end


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.