Project

Curve Fitting in MATLAB

In this program we fit a curve using the polyfit function. First we load data from file and store in variables. Then using a loop,we invoke polyfit and polyval functions to get coefficients and predicted Cp values repectively which are used to plot two graphs for linear curve fit and cubic curve fit respectively. Finally suitable title, colour,x and y labels are entered and output is compared.

From the graphs we see that as the order of equation increases, the accuracy of the curve fit increases. Thus for a perfect curve fit we should use an equation of higher order.

clear all
close all
clc
%data input
data=load(\'data\');
cp=data(:,1);
temp=data(:,2);
t=1;
for n=[1 3]
    coef=polyfit(temp,cp,n);
    cp1=polyval(coef,temp);
    figure(t)
    plot(temp,cp,\'linewidth\',2,\'color\',\'b\');
    xlabel(\'Temperature in K\');
    ylabel(\'Cp in KJ/kg K\');
    hold on
    plot(temp,cp1,\'linewidth\',2,\'color\',\'r\');
    legend(\'original\',\'curve fit\');
    t=t+1;
    if n==1
        title(\'Linear curve\');
    end
    if n==3
        title(\'Cubic curve\');
    end
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.