# How can I create a best fit polynomial for my data?

4 views (last 30 days)
Syed Ali Zaryab on 4 Sep 2018
Commented: Titus Edelhofer on 9 Sep 2018
I have a set of data points that I received from a supplier which gives relative efficiency based on values of x and y. I want to create a polynomial similar to the one given below using the data provided Efficiency = a * (x)^2 + b * (x*y) + c * (y)^2 + d * (x)+ e * (y) + f
So basically what I want is a function which optimizes the value of a,b,c,d,e and f for me and gives me results that are almost similar to the one in the data table.
Attached herewith is the picture of the graph that the supplier provided along with the data points. Can you please tell me what functions can I use in Matlab to help me achieve this task.

Titus Edelhofer on 4 Sep 2018
Hi Syed,
if I assume you have three (column) vectors x, y and Efficiency, then it's simply a linear regression problem:
A = [x.^2 x.*y y.^2 x y ones(size(x))];
sol = A \ Efficiency;
Then sol contains the (best approximation) values a,...,f.
Titus

Syed Ali Zaryab on 7 Sep 2018
Dear Titus,
I tried to implement your suggestion and wrote the following bit of code
x = [0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1;
0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1;
0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1;
0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1];
eff = [.9 .9 .9 .9 .9 .9 .9 .9 .9 .9 .9 .9 .9;
.95 .95 .95 .95 .95 .95 .95 .95 .95 .95 .95 .95 .95;
.9667 .9667 .9667 .9667 .9667 .9667 .9667 .9667 .9667 .9667 .9667 .9667 .9667;
.9833 .9833 .9833 .9833 .9833 .9833 .9833 .9833 .9833 .9833 .9833 .9833 .9833];
y= [0.93 0.91 0.89 0.87 0.85 0.84 0.835 0.83 0.83 0.835 0.84 0.85 0.86;
0 0 0 1.27 1.1 1.04 1 0.99 0.98 0.975 0.97 0.98 0.99 ;
0 0 0 0 0 0 0 0 1.07 1.03 1.025 1.025 1.03;
0 0 0 0 0 0 0 0 0 0 0 1.1 1.07 ];
A = [x.^2 x.*y y.^2 x y ones(size(x))];
sol = A \ eff;
sol;
but over here I am getting sol as a matrix. I believe I am making a mistake somewhere.
Also I would like to see the scatter plot as well as the best fit plot. But I dont know how to see them.
Thanks for the help.
Ali
Torsten on 7 Sep 2018
Use "reshape" to change x, eff and y from (4x13) matrices to (52x1) vectors.
Titus Edelhofer on 9 Sep 2018
Or use (:), i.e.,
x = x(:);
y = y(:);
eff = eff(:);
Titus