Polynomial Regression in MATLAB
Regression estimates the relationship among variables for prediction.
Linear regression requires the relation between the dependent variable and the independent variable to be linear.
But sometime if we have data points which take curved shape , then we should go with polynomial regression.
For example , if we have data points like this–>
It is quite clear that , a straight line can not fit properly in this data set.
For these types of data sets we will go with Polynomial Regression.
MATLAB Code to implement Polynomial Regression:
Step 1:
Take the predictor variable and response variable as inputs from the user.
clc
clear all
close all
x=input(‘Enter the x coordinates’);
y=input(‘Enter the y coordinates’);
clear all
close all
x=input(‘Enter the x coordinates’);
y=input(‘Enter the y coordinates’);
Step 2:
Take the order of the polynomial as user input.
a=input(‘Enter the order of the polynomial’);
Step 3:
For polynomial curve fitting in MATLAB , there is one inbuilt function called polyfit
Check the documentation of the polyfit here–>
[p = polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1 i.e. the first element of p will be coefficient corresponding to x^n , next coefficient of p will be corresponding to x^n-1 and so on]
So we will use this for nonlinear regression in MATLAB–>
coeff=polyfit(x,y,a);
Step 4:
Now we have plot the data points and the fitted curve.
The fitted curve should be continuous. The curve should range from minimum to maximum value of x which was taken as input in the step 1. As the fitted curve should be continuous , so that we have to take very small increment in this case.
So code for the time sample range is –>
So code for the time sample range is –>
t=min(x):0.01:max(x);
Step 5:
Now we have to get the data points for each time sample.
To store the y coordinates, first, we have to create one empty array–>
z=[];
Now one for loop has to be used for traversing in the time sample range.
One more for loop is required to calculate the y coordinate value for the x coordinate .
This code can be simply written as–>
c=0;
temp=a;
z=[];
for i=1:length(t)
t1=t(i);
for j=1:length(coeff)
c=c+coeff(j)*(t1^temp);
temp=temp-1;
end
temp=a;
z=[z c];
c=0;
end
temp=a;
z=[];
for i=1:length(t)
t1=t(i);
for j=1:length(coeff)
c=c+coeff(j)*(t1^temp);
temp=temp-1;
end
temp=a;
z=[z c];
c=0;
end
Step 6:
Plot the data points and fitted curve in a same figure window using hold on command in matlab.
plot(t,z);
hold on;
plot(x,y,’o’);
grid on;
hold on;
plot(x,y,’o’);
grid on;
So the whole code is–>
clc
clear all
close all
x=input(‘Enter the x coordinates’);
y=input(‘Enter the y coordinates’);
a=input(‘Enter the order of the polynomial’);
coeff=polyfit(x,y,a);
t=min(x):0.01:max(x);
c=0;
temp=a;
z=[];
for i=1:length(t)
t1=t(i);
for j=1:length(coeff)
c=c+coeff(j)*(t1^temp);
temp=temp-1;
end
temp=a;
z=[z c];
c=0;
end
plot(t,z);
hold on;
plot(x,y,’o’);
grid on;
clear all
close all
x=input(‘Enter the x coordinates’);
y=input(‘Enter the y coordinates’);
a=input(‘Enter the order of the polynomial’);
coeff=polyfit(x,y,a);
t=min(x):0.01:max(x);
c=0;
temp=a;
z=[];
for i=1:length(t)
t1=t(i);
for j=1:length(coeff)
c=c+coeff(j)*(t1^temp);
temp=temp-1;
end
temp=a;
z=[z c];
c=0;
end
plot(t,z);
hold on;
plot(x,y,’o’);
grid on;
Command Window:-
Enter the x coordinates
[0 20 40 60 80 100]
Enter the y coordinates
[0.0002 0.0012 0.0060 0.0300 0.0900 0.2700]
Enter the order of the polynomial
2
[0 20 40 60 80 100]
Enter the y coordinates
[0.0002 0.0012 0.0060 0.0300 0.0900 0.2700]
Enter the order of the polynomial
2
Output:-
If we increase the order of the polynomial , then the fitting will be more accurate. For example, if instead of giving the order as 2, if we give the order as 3, then the output looks like below figure
No comments