Can anyone please help with this?

4 views (last 30 days)
x=linspace(-5, 5,100);
y = [-ones(1,50) + 0.2*rand(1,50), ones(1,50)-0.2*rand(1,50)];
f(x) = a2*x^ 2 + a1*x + a0;
where ai, i=0,1,2... are real parameters
I have to find ai so that Sum(i=1:100) (yi − f(xi))^2 becomes minimum, using least squares method. Thanks!

Accepted Answer

Image Analyst
Image Analyst on 9 Dec 2018
See attached polyfit() demo and adapt as needed.
0000 Screenshot.png

More Answers (1)

John D'Errico
John D'Errico on 8 Dec 2018
Use the tool designed to solve that problem.
help polyfit
  2 Comments
apostolos georgantopoulos
if i use p=polyfit(x,y,2) i will get the equation of the best fit parabolic curve which will be in the form of f(x)=p(1)*x^2+p(2)*x+p(3). But is this the result im asking for? I dont really get what the meaning of this sum that i want to become minimum is.
John D'Errico
John D'Errico on 9 Dec 2018
I would strongly recommend you do some reading about regression modeling, etc., because I won't spend hours writing an in depth explanation of these concepts from statistics. That is not the scope of Answers, since it is not about MATLAB.
But, yes, polyfit does indeed minimze the sum of squares of the residual errors. And what is that sum? What is
y(i) - f(x(i))
Since f(x(i)) would be the model prediction at point x(i), then y(i) - f(x(i)) is the error at that point, of the model prediction compared to the corresponding value of y. Squaring that error at each point, and then forming the sum of squares of all those residuals, yields the sum you ask about. Why is that a good metric to minimize to fit a model? Again, this starts to delve more deeply into statistics, and I won't teach a statistics course in the comments.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!