Project

Linear and cubic Curve fitting in matlab

AIM : Coding in Matlab to fit a linear and cubic polynomial for the Cp data.

CODING IN MATLAB :

clear all
clc
%preparation of data 
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2)

%original fit
plot(temperature,cp,'linewidth',3,'color','r')
xlabel('temperature (K)')
ylabel('specific heat (kj / Kmol-K)')

%liner fit
co_eff = polyfit(temperature,cp,1);
predicted= polyval(co_eff,temperature);
hold on
plot(temperature,predicted,'linewidth',3,'color','b')

%cube fit
co_eff = polyfit(temperature,cp,3);
predicted= polyval(co_eff,temperature);
hold on
plot(temperature,predicted,'linewidth',3,'color','g')
axis([250 3800 900 1500])

legend('original','liner','cube')

OUTPUT :

At the starting of the coding we have written “clear all” ,due to his the store vale or data of the previous codding is been clear.

Then we right “clc” due to his the command window get clear including the error of the previous codding.

Since we have give data of temperature and specific heat, using this data we have to fit the curve . thus to load data or tell the matlab to take the value from data , we have to pass the command “cp_data = load('data')”. After passing this command we take the first column value from data and store in “ temperature” , to process future similarly we have to do for specific value cp and the command is given such as temperature = cp_data(:,1) & cp = cp_data(:,2)

After loading data from data folder we have to plot the original graph and label them x-axis as “temperature” and y-axix as “specific heat”

Now for liner fit we pass the command “polyfit” and store that value in “co_eff” and since we doing for liner we right as 1 , i.e “co_eff = polyfit(temperature,cp,1);”

Now we get the polynomial value and store in “predicted” by passing command as “predicted= polyval(co_eff,temperature);” and now plot the graph of temperature and predicated by passing the command “plot(temperature,predicted,'linewidth',3,'color','b')”

Now for cubic fit we pass the command “polyfit” and store that value in “co_eff” and since we doing for cubic we right as 3 , i.e “co_eff = polyfit(temperature,cp,3);”

Now we get the polynomial value and store in “predicted” by passing command as “predicted= polyval(co_eff,temperature);” and now plot the graph of temperature and predicated by passing the command “plot(temperature,predicted,'linewidth',3,'color','g')” .Then pass the command for giving range of axis & lagend of original , liner , cubic

eg : In cubic to get proper fit we must increase or decreases the degree of the polynomial.

  • Now let increse degree of cubic to 4 ans se the output(GREEN) , and how campare to original curve(RED)
  • Now let increse degree of cubic to 6 ans se the output(GREEN) , and how campare to original curve(RED)
  • Now let increse degree of cubic to 6 ans se the output(GREEN) , and how campare to original curve(RED)

When degree is increases to " 10 " then cubic fitting to origina curve .

ERROR :

  • To pass the command of " polyfit " , we have to give 3 value. but i have not given degree of polynomial
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.