Project

CURVE FITTING(linear and cubic)

AIM: Write a matlab program for curve fitting.

GOVERNING EQUATIONS:

sum of square(SSE)=`∑_(i=1)^n err (i)^2`

err=`|y(i)-value of f(x) at x(i)|`

sum of the square of the regression(SSR) =`∑_(i=1)^n (f(x(i))- mean)^2`

SST=SSE+SSR.

`R^2= (SSR)/(SST)`

`RMSE=sqrt((SSE)/(n)` root mean square error

THEORY:

CURVE FITTING:Curve fitting is essentially taking data and builiding approximate model for the data,producing a curve which is just a function.

curve fitting is nothing but deriving the equation from the set of available data points on the graph such that root mean square error of all predicted points is minimum.(that means we try to derive an equation such that it passes as closely as possible through given points)

PERFECT FIT:

Means the curve should fit the original curve without showing any errors .And also we can get perfect curve by R-SQUARE=1.(That means the two data points coincide or merge to get a perfect curve).

BEST FIT:

1)By reducing errors.

2)By increasing the order of the polynomial.

MATLAB CODE:

clear all;
close all;
clc;

%loading cp and temperature data
cp_data=load('data');    
temperature=cp_data(:,1)   % temperature data
cp=cp_data(:,2)    %cp data
  

%curve fit

coeffs=polyfit(temperature,cp,1)   %polyfit finds coeffs
predicted_cp_l=polyval(coeffs,temperature)    % polyval evaluates the polynomial 


%compare my curve fit with original data for linear 
%cp=a*temp+b   %general form
figure(1)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp_l,'linewidth',3)
xlabel('temperature[k]')
ylabel('cp(kj/kmol-k)')
legend('original dataset','curve fit')
title('Linear','fontsize',20)


%compare my curve fit with original data for cubic
figure(2)
coeffs=polyfit(temperature,cp,3)
predicted_cp_c=polyval(coeffs,temperature)

plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp_c,'linewidth',3)
xlabel('temperature[k]')
ylabel('cp(kj/kmol-k)')
legend('original dataset','curve fit')
title('CUBIC','fontsize',20)

%finding the error for linear curve fit or best curve fit
sum=0;
for i=1:length(temperature)
  sum=sum+cp(i);
end
mean=sum/3200;
sse=0; %sum of square errors
ssr=0; %sum of square of regression
for j= 1:length(temperature)
  sse=sse+(cp(j)-predicted_cp_l(j))^2;
  ssr=ssr+(predicted_cp_l(j)-mean)^2;
end
sst=sse+ssr;  %TOTAL SUM SQUARE
R_SQR=ssr/sst; %r-square
RMSE=(sse/3200)^0.5  %root mean square error.

%for cubic
sum_c=0;
for i=1:length(temperature)
  sum_c=sum_c+cp(i);
end
mean=sum/3200;
sse_c=0; %sum of square errors
ssr_c=0; %sum of square of regression
for j= 1:length(temperature)
  sse_c=sse_c+(cp(j)-predicted_cp_c(j))^2;
  ssr_c=ssr_c+(predicted_cp_c(j)-mean)^2;
end
sst_c=sse_c+ssr_c;  %TOTAL SUM SQUARE
R_SQR_c=ssr_c/sst_c; %r-square
RMSE_c=(sse_c/3200)^0.5  %root mean square error.










PROCEDURE:

1)Load the datapoints that is(temperature , cp)

2) evaluatimg the linear and curve fit using polyval function.

3)finding of errors.

ERRORS:

The above errors are due

1) i forget to linfewidth paremeter.

2) to iterate the cp, istead of 'i' using i used 'data'.

RESULTS:

from the above result it is clear that as the degree of polynomial increses we get best curve.

CONCLUSION:

IF R_SQUARE VALUE IS ONE , THEN WE CAN CONCLUDE THAT THE CURVE IS PERFECT.

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.