Curve Fitting with a high order polynomial equation using MATLAB
The aim of this project is to take a temperature data file and plot the variation of Cp value with respect to temperature. Using the resultant curve we try to make a curve fit to define a polynomial equation to represent our data and minimize the error in our equation.
`sum(cp(i) - f(t))^2`
We started off with loading the data file and assigning arrays to different data types. Once done with that we plotted a graph to see the variation. According to this variation, we decided to use a linear curve fit (polynomial of degree 1) and a cubic curve fit (polynomial of degree 3).
To do that we approached the problem by defining the Cp value into a third-order polynomial by using the ‘polyfit’ command, after that we used that array to define our predicted variation by using ‘polyval’ command.
How this process works is, ‘polyfit’ command take out array and defines its variation with respect to a value and then turns that variation into a polynomial with degree of out choice (here we have used degrees 1 and 3).
After that we use that polynomial to define a new array and then we measure its response with respect to out temperature.
We do this by simply plotting our curves.
As we can see in the plot, the cubic polynomial has a curve quite similar to our original data.
But for a data set with 2 variables if we need to define a cubic polynomial we need to do scaling and centering which have been defined under Errors faced.
To check the goodness of our curve, we move on to the next step.
Now we use ‘fit’ command to define the curve that we have plotted for both our 1st and 3rd degree polynomials. We assign these values to arrays and using those arrays we find the value of the function of our curve at each point individually. We start a loop and we define our SSE (Sum of Squared Errors) equation and we add them all up for both our curves.
This returns us with values of SSE for both our curves but the criteria to measure the goodness of a curve is having a low SSE but low is a relative term.
So to solve this problem we use the MATLAB curvefit Toolbox in which we define out variables and what type of equation we want and it returns us with a curve and the goodness of the curve. After running this process, it was noticed that our resultant values were lesser than the ones in the toolbox which means after scaling and centering, we defined a curve more refine than the toolbox standard.
Errors Faced:
While defining the new curve we defined a 3rd order polynomial using an equation with only 2 variables and 2 end conditions. This raised an error saying equation is badly conditioned. Which means that coefficient values are not accurate due to lack of date. Since there can’t be three end conditions in out data, we used a function of the ‘polyfit’ command which allows us to do scaling and centering.
What this does is that it replaces our variables x and y with (x-mean(x))/std(x) and
(y-mean(y)/std(y). this doesn’t change our curve theoretically but it makes the result better conditioned and defines a finite precision.
Using this we were able to form a smother more accurate curve and prevent the error.
There was also an error in the loop as the length of the variable given was wrong, but it was dealt with.
FAQ:
- Curve fits are made to fit perfectly by reducing the error down to zero, the closer the curve is to the actual data plot, the closer it is to perfection. To make the absolutely perfect fit, the errors will have to be reduced down to zero and the SSE value should be zero as well.
- To make the best fit (like we did in our problem) we need to make our curve more detailed or define it with more precision i.e. use a higher order polynomial or use an interpolated equation to provide more detail to our curve which brings it closer to our actual plot.
- To improve a cubic fit we could either increase the order but it won’t be a cubic fit anymore, the other way to do it is by splitting our polynomial which is essentially giving set of ranges to different polynomials which define our curve in different sets of ranges.
the file for the code can be found here: https://github.com/maynkarora/skill-lync/tree/master/curve%20fit%20using%20MATLAB
%perform a curve fit on temperature data and find the best fit
clc
clear all
close all
%taking values
data=load("data");
%assign data
temp=data(:,1);
cp=data(:,2);
%third order polynomial
[cf,s,mu]=polyfit(temp,cp,3);
pred_cp=polyval(cf,temp,[],mu);
%first order polynomial
cf1=polyfit(temp,cp,1);
pred_cp1=polyval(cf1,temp);
%plotting
hold on
plot(temp,cp,'color','b','linewidth',3)
plot(temp,pred_cp,'color','r','linewidth',3)
plot(temp,pred_cp1,'color','g','linewidth',3)
%labels and legends
xlabel("temperature")
ylabel("Cp")
legend('original data set','third order curve fit','first order curve fit')
%finding goodness
curvefit1=fit(temp,pred_cp1,'poly1');
sse_poly1=0;
for i=1:length(cf1)
a=curvefit1(i);
x=(cp(i)-a)^2;
sse_poly1=sse_poly1+x;
end
sse_poly3=0;
curvefit3=fit(temp,pred_cp,'poly3','normalize','on');
for j=1:length(cf)
b=curvefit3(j);
y=(cp(i)-b)^2;
sse_poly3=sse_poly3+y;
end
%print sse values
sse_poly1
sse_poly3