Least Squares Fitting for ax²+bx+c with Given Points: Homework Solution

In summary: The fact is I didn't find the inverse of A^T A but rather reduced it to a row echelon form and so my answer was not correct. Thanks a lot for your help!In summary, the conversation discusses finding the best fitting function of the form ax²+bx+c using least squares. The points given are used to create a matrix A, and the system to solve is equivalent to A^{T}A \vec x = A^{T} \vec b. The method of reducing A^{T}A into a row echelon form is discussed, but it is noted that this may lead to an incorrect solution. The importance of using A^T.A instead of directly solving Ax=b is also explained.
  • #1
fluidistic
Gold Member
3,923
261

Homework Statement


I must find the best fitting function of the form ax²+bx+c using least squares.
The points are (-1,6.1), (0,2.8), (1,2.2), (3,6) and (6,26.9).

2. Homework Equations + attempt at a solution
[tex]A\vec x= \vec b[/tex], I'm looking for [tex]\vec x =\begin {pmatrix} a \\ b \\ c \end {pmatrix}[/tex]. I know that [tex]\vec b = \begin {pmatrix} 6.1 \\ 2.8 \\ 2.2 \\ 6 \\ 26.9 \end {pmatrix}[/tex].
With the use of some theory, the system I want to solve is equivalent to [tex]A^{T}A \vec x = A^{T} \vec b[/tex].
My matrix A is [tex]\begin {bmatrix} 1 & -1 & 1 \\ 0 & 0 & 1 \\ 1 & 1 & 1 \\ 9 & 3 & 1 \\ 36 & 6 & 1 \end {bmatrix}[/tex].
[tex]A^{T}A=\begin {bmatrix} 1379 & 243 & 47 \\ 243 & 47 & 9 \\ 47 & 9 & 5 \end {bmatrix}[/tex].
And [tex]A^{T}\vec b = \begin {bmatrix} 1030.7 \\ 175.5 \\ 44 \end {bmatrix}[/tex].
This gave me [tex]\vec x = \begin {bmatrix} 2.669598408 \\ -9.505782817 \\ 0.6247662773 \end {bmatrix}[/tex]. Thus [tex]f(x) \approx 2.67 x^2-9.51 x +0.62[/tex].
I've plotted it in my calculator and it doesn't seem to fit well at all. My friend put these points in a program and he got a much better fitting ([tex]a \approx 3[/tex], [tex]b \approx -2[/tex], [tex]c \approx 1[/tex]).
I've redone the algebra and I don't see my error(s).
I've followed the method of this page: http://tutorial.math.lamar.edu/Classes/LinAlg/LeastSquares.aspx.
I got right all problems involving a linear fit, but failed all other fits (exponentials, quadratic, etc.). What am I doing wrong?!

Another question that really matters to me is... why don't we solve Ax=b directly since we know both A and b, we would get 3 equations with 3 unknowns... why bother with the transpose of A?

Thank you very much for any help.
 
Physics news on Phys.org
  • #2
the reason you use A^T.A is written in theorem 2

Theorem 2 Suppose that A is an matrix with linearly independent columns. Then, is an invertible matrix.
 
  • #3
now everything on that page is to do with simple linear squares regression

say your first point is
(x1, b1)

when you do quadratic regression, in effect you consider the triplet
(x1, y1, b1) where y1 = x1^2

then you regress on the parameters a,b,c in the fit equation
ay+bx+c = ax^2+bx+c

note the equation is linear in a,b,c, hence still linear regression (y could easily be another functional form eg. exponential y = e^x)

as there are now in effect 2 variables (x & y=x^2) you need to consider multivariate linear regression

hopefully this helps, my tex isn't displaying correctly so i can't properly QC what you've written
 
  • #4
lanedance said:
the reason you use A^T.A is written in theorem 2

Thanks a lot.. My bad, I said something wrong :/
I said I'd get 3 equations with 3 unknowns when A isn't even square. This would do in fact 4 equations with 3 unknowns.
Question solved. Now I'm waiting for help on the first part.
 
  • #5
Ok: Latex problem; I'll solve that for you. :D
 

Attachments

  • print1.jpg
    print1.jpg
    22.6 KB · Views: 383
  • print2.jpg
    print2.jpg
    24.1 KB · Views: 396
  • #6
fluidistic said:
Thanks a lot.. My bad, I said something wrong :/
I said I'd get 3 equations with 3 unknowns when A isn't even square. This would do in fact 4 equations with 3 unknowns.
Question solved. Now I'm waiting for help on the first part.

the other thing to note is even if it was 3 and 3, there is not guaranteed a consistent unique solution. Hence least squares finds the solution with minimum squre error
 
  • #7
cheers for photos

so how do you get your final x?

shouldn't it be
(A^T.A)^(-1).(A^T.b)

if i do that i get different values to you...
 
  • #8
i then get
0.99
-1.99
3.03

also be careful to check the order is correct (ie corresponding a,b,c to x^2,x,1)
 
  • #9
Oh thanks a lot. You get the same values as the program of my friend...
I did get my x as follows: write down [itex]A^TA[/itex] and amplify this matrix with the column [tex]\vec b[/tex]. Then reduce [itex]A^TA[/itex] into a row echelon form and look at what happened in the augmented part of the matrix.
So in a sense, I didn't even calculated the inverse of [itex]A^TA[/itex], which might be where my error lies?
 

Attachments

  • again.jpg
    again.jpg
    33.3 KB · Views: 370
  • #10
fluidistic said:
Oh thanks a lot. You get the same values as the program of my friend...
I did get my x as follows: write down [itex]A^TA[/itex] and amplify this matrix with the column [tex]\vec b[/tex]. Then reduce [itex]A^TA[/itex] into a row echelon form and look at what happened in the augmented part of the matrix.
So in a sense, I didn't even calculated the inverse of [itex]A^TA[/itex], which might be where my error lies?

should lead to the same result, maybe you're missing the A^T on the right side?
[itex]A.x = b [/itex]
[itex]A^T.A.x = A^T.b [/itex]
then
[itex]((A^T.A)^-1)(A^T.A).x = ((A^T.A)^-1)(A^T.b) [/itex]
[itex]x = ((A^T.A)^-1)(A^T.b) [/itex]
 
  • #11
lanedance said:
should lead to the same result, maybe you're missing the A^T on the right side?
[itex]A.x = b [/itex]
[itex]A^T.A.x = A^T.b [/itex]
then
[itex]((A^T.A)^-1)(A^T.A).x = ((A^T.A)^-1)(A^T.b) [/itex]
[itex]x = ((A^T.A)^-1)(A^T.b) [/itex]

Yeah I understand your method. No, I do not miss the A^T b term. I wrote what I get as my matrix A^T b in post #2, see picture 2.

So basically my method is right but it seems I made at least an algebraic (or arithmetic, I don't know how to call it) error when computing x.
I'll re-recheck that...
Thanks a lot anyway. At least I know I made some error(s) while my method is OK.
 
  • #12
If you substitute your x vector back into your matrix equation, you will find that the equation is not satisfied numerically. Your friend's solutions are much closer to the actual values which satisfy your matrix equation.
 
  • #13
SteamKing said:
If you substitute your x vector back into your matrix equation, you will find that the equation is not satisfied numerically. Your friend's solutions are much closer to the actual values which satisfy your matrix equation.

Yeah I know I made an algebraic error. Though I've been confirmed my method is correct.
 

What is least squares fitting?

Least squares fitting is a statistical method used to find the best fit for a set of data points. It minimizes the sum of the squares of the differences between the observed data and the predicted values from a mathematical model.

What are the applications of least squares fitting?

Least squares fitting is commonly used in various fields such as engineering, physics, economics, and social sciences to analyze and interpret data. It is used to determine the relationship between variables, make predictions, and estimate unknown parameters.

What is the difference between linear and non-linear least squares fitting?

In linear least squares fitting, the mathematical model used to fit the data is a linear function, such as a straight line. In non-linear least squares fitting, the mathematical model is non-linear, meaning it cannot be expressed as a straight line or a simple polynomial function.

How is the quality of the fit determined in least squares fitting?

The quality of the fit is determined by calculating the sum of the squares of the differences between the observed data and the predicted values, also known as the residual sum of squares (RSS). A lower RSS indicates a better fit.

What are the limitations of least squares fitting?

Least squares fitting assumes that the data is normally distributed and that there is a linear relationship between the variables. It may not be suitable for data with outliers or when the relationship between the variables is non-linear. Additionally, it cannot account for measurement errors or other sources of uncertainty in the data.

Similar threads

  • Calculus and Beyond Homework Help
Replies
4
Views
2K
  • Calculus and Beyond Homework Help
Replies
12
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
972
  • Calculus and Beyond Homework Help
Replies
3
Views
894
  • Calculus and Beyond Homework Help
Replies
4
Views
4K
  • Calculus and Beyond Homework Help
Replies
5
Views
1K
  • Calculus and Beyond Homework Help
Replies
4
Views
1K
  • Calculus and Beyond Homework Help
Replies
7
Views
2K
  • Calculus and Beyond Homework Help
Replies
5
Views
1K
  • Calculus and Beyond Homework Help
Replies
9
Views
1K
Back
Top