Cramer's rule and first degree polynomial curve fitting

In summary, Scott said that the determinant calculation in Cramer's rule method can be inaccurate, and that using the VPA function may be overkill.
  • #1
PainterGuy
940
69
Homework Statement
Having problem getting the correct answer using Cramer's rule.
Relevant Equations
Please check my work in the posting.
Hi,

I did the first degree curve fitting in MATLAB. Please see below which also shows the output for each code line.

1618697426822.png
But I wasn't able to get the same answer using Cramer's rule method presented below. I'm sure MATLAB answer is correct so where am I going wrong with the Cramer's rule method. Could you please guide me?
1618697921638.png

1618697939527.png
 
Physics news on Phys.org
  • #2
Recheck your determinant calculations. What numbers do you get for det(M), and det(M0) and det(M1) ?

When I did those I ended up with the same results as the polyfit.
 
  • Like
Likes PainterGuy
  • #3
scottdave said:
Recheck your determinant calculations. What numbers do you get for det(M), and det(M0) and det(M1) ?
Thank you!

I did check but still got same numbers. I still cannot see what's going on with this method. Help, please!

det(M) = 229. 76
det(M0) = 6. 695×10⁵
det(M1) = -1220. 8
 
Last edited:
  • #4
Try to go back and do your calculations without rounding off in the intermediate steps. I believe you are losing precision. In calculating these determinants, you have (Large Number) minus (another large number) = small number. 229 is pretty small relative to 8 x 10^5. Dropping a couple of digits at the trailing end will make a difference. 8.0422 x 10^5 = 804220. You have no knowledge about the one's digit, when multiplied by 3 could put you off by a value of close to 30, as just an example. And all of your data has 7 significant figures. Might as well keep (at least) that many digits, till the end.

Since your determinant of M is the denominator in all of these calculations, it can put the final answer off by factors.
 
Last edited:
  • Like
Likes PainterGuy and FactChecker
  • #5
scottdave said:
Try to go back and do your calculations without rounding off in the intermediate steps. I believe you are losing precision. In calculating these determinants, you have (Large Number) minus (another large number) = small number. 229 is pretty small relative to 8 x 10^5. Dropping a couple of digits at the trailing end will make a difference. 8.0422 x 10^5 = 804220. You have no knowledge about the one's digit, when multiplied by 3 could put you off by a value of close to 30, as just an example. And all of your data has 7 significant figures. Might as well keep that many digits, till the end.

Since your determinant of M is the denominator in all of these calculations, it can put the final answer off by factors.
I did the calculations in MATLAB and got the answers. I'd say that my calculations were more precise than those of MATLAB. Seriously, this problem is so baffling. I can't figure out what's wrong with it.
 
  • #6
Your calculations are not precise enough.
The calculations are sensitive to the exact numbers. When the original values of the data are carried through with full precision, the answer matches the MATLAB polyfit result. But calculations like that can be sensitive to the computer accuracy and the final results should be checked to make sure that they work as desired.
 
  • Like
Likes PainterGuy
  • #7
Thank you very much, @scottdave and @FactChecker .

I repeated the calculations in MATLAB using vpa() function and you were right it had to do with the precison.

Thanks for the patience and help!
 
  • Like
Likes scottdave
  • #8
I think VPA function is overkill for this. The important thing is not to truncate or round the intermediate number. Store the results of your calculations into a variable, rather than writing down what is displayed. The computer typically stores more digits than are displayed.

Use the stored variable in the next step, rather than typing in the numbers.

I am speculating that's what you may have done in the first attempt, as I took the "matrix" numbers in your screenshot and I got similar numbers to what you initially got.
 
  • Like
Likes PainterGuy

1. What is Cramer's rule?

Cramer's rule is a method for solving systems of linear equations using determinants. It involves finding the determinants of the coefficient matrix and the matrices obtained by replacing each column of the coefficient matrix with the values from the constant matrix. The solution to the system of equations is then given by the ratios of these determinants.

2. How does Cramer's rule relate to first degree polynomial curve fitting?

Cramer's rule can be used to find the coefficients of a first degree polynomial (a straight line) that best fits a set of data points. The coefficients are found by setting up a system of equations using the data points and solving it using Cramer's rule.

3. What is first degree polynomial curve fitting?

First degree polynomial curve fitting is a method for finding a straight line that best represents a set of data points. This is often used in statistics and data analysis to identify trends and relationships between variables.

4. When should Cramer's rule be used for first degree polynomial curve fitting?

Cramer's rule is most useful when there are only two variables (x and y) and a small number of data points. It is not as efficient for larger systems of equations or when there are many data points.

5. What are the limitations of Cramer's rule for first degree polynomial curve fitting?

Cramer's rule can only be used to find the coefficients of a first degree polynomial, so it is not suitable for more complex curve fitting. It also relies on the data points being linearly related, so it may not be accurate for non-linear relationships. Additionally, Cramer's rule can be computationally intensive for larger systems of equations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
  • Astronomy and Astrophysics
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
875
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Replies
5
Views
466
  • Introductory Physics Homework Help
Replies
8
Views
1K
Replies
7
Views
1K
  • Precalculus Mathematics Homework Help
Replies
10
Views
2K
  • Programming and Computer Science
Replies
5
Views
873
  • Precalculus Mathematics Homework Help
Replies
1
Views
2K
Back
Top