Curve fitting-Program for curve fitting and to determine the way through which we can reduce the error while curve fitting using MATLAB.
Aim:
Program for curve fitting and to determine the way through which we can reduce the error while curve fitting using MATLAB.
Minimizing error:
There are two ways to decrease the error and it includes:
1.Keep Increasing the degree of the polynomial.
2. Dividing the temperature value into small divisions and re-evaluating.
Theory:
Curve fitting is the process of constructing a curve or mathematical function, that has the best fit to a series of data points, possibly subject to constraints.
In this program, we will observe that specific heat is a function of temperature. Since we assume that specific heat is constant but in this, we will get to know that specific heat is a function of temperature. Hence, specific heat varies with an increase in temperature. Specific Heat is in (KJ/KgK) and the temperature is in Kelvin(K).
i.e: cp = f(t).
Equations:
The basic equation of polynomial is used are as followed:
MATLAB CODE:
clear all
close all
clc
%import
data=load('data');
cp=data(:,2);
tem=data(:,1);
%co-efficients
coef1=polyfit(tem,cp,1);
coef2=polyfit(tem,cp,2);
coef3=polyfit(tem,cp,3);
%predicted values
cp1=polyval(coef1,tem);
cp2=polyval(coef2,tem);
cp3=polyval(coef3,tem);
%sse
sse1=0;
sse2=0;
sse3=0;
for i =1:length(tem)
sse1=sse1+(cp(i)-cp1(i))^2;
sse2=sse2+(cp(i)-cp2(i))^2;
sse3=sse3+(cp(i)-cp3(i))^2;
end
disp(sse1)
disp(sse2)
disp(sse3)
sum1=0;
for i =1:length(tem)
sum1=sum1+cp(i);
end
%rsquare
mean=sum1/length(tem);
ssr1=0;
ssr2=0;
ssr3=0;
for i =1:length(tem)
temp1=(cp1(i)-mean)^2;
ssr1=ssr1+temp1;
temp2=(cp2(i)-mean)^2;
ssr2=ssr2+temp2;
temp3=(cp3(i)-mean)^2;
ssr3=ssr3+temp3;
end
r1=ssr1/(ssr1+sse1)
r2=ssr2/(ssr2+sse2)
r3=ssr3/(ssr3+sse3)
%rsme
rmse1=(sse1/length(cp))^.5
rmse2=(sse2/length(cp))^.5
rmse3=(sse3/length(cp))^.5
%plot
hold off
plot(tem,cp,'linewidth',(1))
hold on
plot(tem,cp1,'--','linewidth',(1))
str1 = strcat('R square = ',num2str(r1),' RMSE = ',num2str(rmse1));
text(1000,1000,str1,'Color','red','FontSize',10)
legend('actual','linear relation pridiction')
xlabel('Temprature')
ylabel('Cp')
title('Actual VS Liner Relation prediction')
pause(5)
hold off
plot(tem,cp,'linewidth',(1))
hold on
plot(tem,cp2,'--','linewidth',(1))
str1 = strcat('R square = ',num2str(r2),' RMSE = ',num2str(rmse2));
text(1000,1000,str1,'Color','red','FontSize',10)
legend('actual','quadretic relation prediction')
xlabel('Temprature')
ylabel('Cp')
title('Actual VS Quad Relation Prediction')
pause(5)
hold off
plot(tem,cp,'linewidth',(1))
hold on
plot(tem,cp3,'--','linewidth',(1))
str1 = strcat('R square = ',num2str(r3),' RMSE = ',num2str(rmse3));
text(1000,1000,str1,'Color','red','FontSize',10)
legend('actual','cubic relation pridiction')
xlabel('Temprature')
ylabel('Cp')
title('Actual VS cubic Relation Prediction')
pause(5)
hold off
plot(tem,cp,'linewidth',(1))
hold on
plot(tem,cp1,':','linewidth',(1))
plot(tem,cp2,'--','linewidth',(1))
plot(tem,cp3,'-.','linewidth',(1))
xlabel('Temprature')
ylabel('Cp')
legend('actual','linear relation','quadretic relation','cubic relation')
title('Actual VS Predictions')
RESULTS:
-
- Actual vs liner Relation prediction:
-
- Actual VS Quad Relation prediction:
-
- Actual VS Cubic Relation prediction:
-
- Actual VS All Relation prediction:
ERRORS & REMEDIES:
-
- Only some typing errors were found.
Questions:
1. How to make a curve fit perfectly?
Ans: In order to make a curve fir perfectly we have to consider error estimation as well. The perfect fit means that the curve should fit the original without showing any errors. The second way is to divide into divisions so as to minimize the error.
2. How to get the best fit?
Ans:by using these:-
-
- The sum of square due to error (SSE)
- R-square
- Adjusted R-square
- Root mean square error
3. What can be done to improve the cubic fit?
Ans: The best way to improve the cubic fit is to solve it using division method which can minimize the error up to a high extent
CONCLUSION:
From the output, it is clear that the curve for cubic function fits most perfectly when compared to the linear functions or quad functions.
-
- The accuracy of the cubic function can be further enhanced by using split curve fitting.
- The R-square value above 0.95 is a characteristic of a good curve.
- From the output, it is clear that that the cubic curve is a great fit having a value of R-square of 0.9967.
REFERENCE: