Project

Curve fittings

*********** Remarks of the Evaluator ************

Good Job, Your report is good

Also, blindly increasing the degree of the polynomial will not result in a better fit in all cases. The best way to proceed then is to divide the data points in subregions and curve fit for each region seperately.

NOTE : Also, try exploring on the fit( ) command and curve fitting toolbox in matlab if interested, this provides you with a plethora of options in optimizing a fit without using multiple equations and also calculates the fit characteristics for you !!!

SUGGESTION (GENERAL):

The other thing that you can do is to attach the code and images in

projects.skill-lync.com and paste the link over here.

If you have any doubts please ask us on WHATSAPP or raise a ticket on FRESHDESK or even call us to clarify all your doubts.

Please don't submit incomplete work as these challenges are made with an intuition of improving your understanding of the topic.

So, attempt all challenges sincerely.

Thank you.

*****************************************

AIM:

  • To Write code to fit a linear and cubic polynomial for the Cp data.
  • To Plot the linear and cubic fit curves along with the raw data points.
  • To Explain the fitness characteristics for the curves.

PROCEDURE:

CODE:

clear all
close all
clc

%initial values

data = load('data');
temp = data(:,1);
cp = data(:,2);

%curve fitting

co_effs_linear = polyfit(temp,cp,1);
co_effs_cubic = polyfit(temp,cp,3);
predicted_cp_linear = polyval(co_effs_linear,temp);
predicted_cp_cubic = polyval(co_effs_cubic,temp);

%plotting

plot(temp,cp,'linewidth',2)
hold on
plot(temp,predicted_cp_linear,'linewidth',2)
hold on
plot(temp,predicted_cp_cubic,'linewidth',2)
title('comparesion of original data and curve fittings');
xlabel('Temperature[k]');
ylabel('Specific heat[kj/kmol-k]');
legend('origonal value','curvefit linear','curvefit cubic');

%error plotting

figure
error(1) = curve(temp,predicted_cp_linear,cp);
hold on
error(2) = curve(temp,predicted_cp_cubic,cp);
title('Residue vs Temperature')
legend('linear error','cubic error')
disp('Mean square error:')
disp(error)


function error = curve(temp,orginal,predicted)
%Residue
Residue = orginal-predicted;

%Residue Plot
plot(temp,Residue)
xlabel('Temperature (K)')
ylabel('Residue')

%SquareErrors
error = 0;
for i=1:length(orginal)
error = error + Residue(i)^2;
end
% mean square error
error = error/length(orginal);
end

Step 1: commands "close all" ; "clear all" ; "clc" these commands close all the previous files,clear all the previous contents and clears the content in command window respectivly.

Step 2 : commands " data = load('data');"
" temp = data(:,1);"
" cp = data(:,2);"

above commands are the initial values from the file 'data'.

Step 3 : commands "co_effs_linear = polyfit(temp,cp,1);"
"co_effs_cubic = polyfit(temp,cp,3);"

The above commands are the curve fittings command from the matlab database.The command polyfit calculate the arbitary constants for cp with provided order of the polynomial.

Step 4 : commands "predicted_cp_linear = polyval(co_effs_linear,temp);"
"predicted_cp_cubic = polyval(co_effs_cubic,temp);"

the command polyval captures the arbitary constants from the polyfit values with respect to the temp.

Step 5: commands "plot(temp,cp,'linewidth',2)"
"hold on"
"plot(temp,predicted_cp_linear,'linewidth',2)"
"hold on"
"plot(temp,predicted_cp_cubic,'linewidth',2)"
"title('comparesion of original data and curve fittings');"
"xlabel('Temperature[k]');"
"ylabel('Specific heat[kj/kmol-k]');"
"legend('origonal value','curvefit linear','curvefit cubic');"

the above commands are general plotting commands which first plot the original data provided and then it plots the temperature vs predicted cp value for the linear polynomial and then it plots the temperature vs predicted cp value for the cubic polynomial.

Step 6: command "function error = curve(temp,orginal,predicted)"

"Residue = orginal-predicted;"

the commands generally defines a function with name curve and does a simple arthimetic subraction to find the residue

Step 7: command "plot(temp,Residue)"
"xlabel('Temperature (K)')"
"ylabel('Residue')"

the above command plot a graph between temperature and residue which helps to determine whether the fit is a good or not.

Step 8 : command "error = 0;"
"for i=1:length(orginal)"
"error = error + Residue(i)^2;"
"end"

the above commands is a for loop command which is used find the errors in the fit.

Step 9 : command "error = error/length(orginal);"

the above commands is to calculate the mean square error.the mean square error is used to find the fit is best fit or not.

Step 10 : command "figure"
"error(1) = curve(temp,predicted_cp_linear,cp);"
"hold on"
"error(2) = curve(temp,predicted_cp_cubic,cp);"
"title('Residue vs Temperature')"
"disp('Mean square error:')"
"disp(error)"

The above commands plots two curves by calling the function 'curve' which calculates the error in the fit data and display the mean square error.

PARAMETERS USED TO MEASURE THE FITNESS OF THE CURVE:

The residue and mean square error is used to measure the fitness of the curve.the residue is the differnece between predicted cp value and original cp value. whereas the mean square error is the mean of the entire errors which determines whether the fit is correct to data with less error.

ANSWERS FOR THE QUESTIONS:

1.We can make curve fit perfectly by increasing the order of the polynomial.which makes the fit more smother and accuarte with the given data.

2.We can get a best curve fit by having less residue value and having less mean square error.

3.we can improve the fit by reducing the residue.

OUTPUT:

RESULT:

Thus we infer that for given raw datas we can create a curve fit or mathematical relation which is useful for predition of the outcome of the future outputs.The curve fit can be improved by improving the order of the polynomial and the best curve fit and perfect curve fit are chosen based on the residue and mean square if the error is random around the zero then the fit is taken as best fit.

REFERENCE:

https://en.wikipedia.org/wiki/Curve_fitting

https://www.ctr.unican.es/asignaturas/instrumentacion_5_it/curvefit.pdf

http://physics.ucsc.edu/~drip/133/ch3.pdf

MATLAB database

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.