4
$\begingroup$

More specifically, I was using the "no sign-in" option of Wolfram Programming Lab.

I was trying to solve a matrix problem, with the following code:

ClearAll["Global`*"]
m={{2,0},{0,1}}*2500;
k={{3,-1},{-1,1}}*20000 Pi^2;
w1=N[2Pi,5];
w2=6.2832;
D1=Det[k-w1^2*m]
D2=Det[k-w2^2*m]

Since the numerical values of w1 and w2 should be close, I expect the numerical values of D1 and D2 should also be close. Strangely, Wolfram Cloud gives very different values:

enter image description here

It took me a whole night to pin down this segment of code. I don't know if this is only due to my computer/browser, or some one else, if runs the same code, will have same problem? What happened?


Edit

Suppose I would like to compare the determinant using exact symbolic $2\pi$ and function N[2Pi,5]

ClearAll["Global`*"]
m={{2,0},{0,1}}*2500;
k={{3,-1},{-1,1}}*20000 Pi^2;
w1=N[2Pi,5];
w2=2Pi;
D1=Det[k-w1^2*m]
D2=Det[k-w2^2*m]

The result is not exactly the same: enter image description here

So, is N[2Pi,5] exactly equal to $2\pi$ or not? What does the function N actually do?

$\endgroup$

closed as off-topic by Daniel Lichtblau, MarcoB, anderstood, Michael E2, Pinti May 29 at 12:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Daniel Lichtblau, MarcoB, anderstood, Michael E2, Pinti
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    $\begingroup$ Suppose small epsilon then ClearAll["Global`*"]; m={{2,0},{0,1}}*2500; k={{3,-1},{-1,1}}*20000 Pi^2; w1=2Pi+epsilon; FullSimplify[Det[k-w1^2*m]] returns 12500000*epsilon*(epsilon - 2*Pi)*(epsilon + 4*Pi)*(epsilon + 6*Pi) and for small epsilon that is approximately 12500000*epsilon*-2*Pi*4*Pi*6*Pi== -600000000*epsilon*Pi^3` so any small error in w is multiplied by about 1.86*10^10 in the determinant. $\endgroup$ – Bill May 24 at 18:39
  • 5
    $\begingroup$ No, N[x, p], represents, if possible, the value of x approximated to a precision of p digits. Read the documentation on N. $\endgroup$ – Michael E2 May 24 at 23:58
  • 7
    $\begingroup$ See reference.wolfram.com/language/tutorial/NumbersOverview.html, esp. the tutorials about exact, approximate and arbitrary-precision numbers. $\endgroup$ – Michael E2 May 25 at 0:00
12
$\begingroup$

I get the same result in Mathematica, so it's not a Mathematica Online issue. I don't think it's even a Mathematica issue. It's due to two factors:

  1. w1 is not equal to w2, because N doesn't actually truncate 2 Pi to five digits
  2. Det[k-w^2*m] changes quickly, so any little inaccuracy in w becomes a big discrepancy in Det[k-w^2*m]

To see #1:

w1 == 2 \[Pi]
(* True *)
w1 - w2
(* -0.0000146928 *)

To see #2:

Plot[Det[k - w^2*m], {w, 6.2831, 6.2833}]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Regarding #1, it appears that the determinants calculated using $2\pi$ and N[2Pi,5] are not exactly the same. I have edited the question. $\endgroup$ – York Tsang May 24 at 23:59
  • $\begingroup$ Agreed, this is about numeric math and not Mathematica per se (as this response also notes). $\endgroup$ – Daniel Lichtblau May 25 at 15:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.