Cramer's rule and first degree polynomial curve fitting

AI Thread Summary
The discussion centers on issues with applying Cramer's rule for first-degree polynomial curve fitting in MATLAB, where the user struggles to match results obtained from MATLAB's built-in polyfit function. Key advice includes ensuring precise calculations by avoiding rounding during intermediate steps, as this can significantly affect the final results. The importance of using stored variables instead of displayed numbers is emphasized to maintain accuracy. After re-evaluating the calculations with the vpa() function, the user acknowledges that precision was indeed the problem. The conversation highlights the sensitivity of determinant calculations to numerical precision in computational methods.
PainterGuy
Messages
938
Reaction score
72
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
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
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:
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
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.
 
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
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
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
Back
Top