Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica-Commutive answers not equal

  1. Jan 3, 2012 #1
    Hello, I am using a matrix and finding roots of a polynomial, where the coefficients are based of different combinations of the entries in the matrix. If ln(1), Ln(2), ln(3), ln(4) are on the main diagonal, the answer should be the same as the answer for ln(2), Ln(1), ln(3), ln(4) on the main diagonal. However I am getting two different answers. All math is multiplication and addition so it should be communicative and yield the same answers. Here is my Code, any ideas to a solution?

    roots[W_] := ((*This function takes in a Work of Charging matrix that is all \
    numbers and yeilds the roots of the Binding Polynomial.*)
    fu[X_, x_, y_] := X[[x]][[y]];

    W11 = fu[W, 1, 1];
    W22 = fu[W, 2, 2];
    W33 = fu[W, 3, 3];
    W44 = fu[W, 4, 4];
    W12 = fu[W, 1, 2];
    W13 = fu[W, 1, 3];
    W23 = fu[W, 2, 3];
    W14 = fu[W, 1, 4];
    W24 = fu[W, 2, 4];
    W34 = fu[W, 3, 4];
    C0 = 1;
    C1 = E^(-W11) + E^(-W22) + E^(-W33) + E^(-W44) // N;
    Print["C1: ", C1];
    C2 = E^(-(W11 + W22 + (2*W12))) + E^(-(W22 + W33 + (2*W23))) +
    E^(-(W33 + W44 + (2*W34))) + E^(-(W11 + W33 + (2*W13))) +
    E^(-(W11 + W44 + (2*W14))) + E^(-(W22 + W44 + (2*W24))) // N;
    Print["C2: ", C2];
    C3 = E^(-(W11 + W22 + W33 + (2*W12) + (2*W13) + (2*W23))) +
    E^(-(W22 + W33 + W44 + (2*W23) + (2*W24) + (2*W34))) +
    E^(-(W33 + W44 + W11 + (2*W34) + (2*W13) + (2*W14))) +
    E^(-(W11 + W22 + W44 + (2*W12) + (2*W14) + (2*W24))) // N;
    Print["C3: ", C3];
    C4 = E^(-(W11 + W22 + W33 + W44)) // N;
    Print["C4: ", C4];

    answer =
    Solve[Evaluate[C4]*(x^4) + Evaluate[C3]*(x^3) +
    Evaluate[C2]*(x^2) + Evaluate[C1]*x + Evaluate[C0] == 0, x] //
    finalAnswer = Flatten[Evaluate[x /. answer]]);

    (*End of Function.*)

    A = {{Evaluate[Log[1]], 0, 0, 0}, {0, Evaluate[Log[2]], 0, 0}, {0, 0,
    Evaluate[Log[3]], 0}, {0, 0, 0, Evaluate[Log[4]]}};(*//MatrixForm*)


  2. jcsd
  3. Jan 4, 2012 #2
    Side note: Unless what you have posted is some simplification or modification of what you are really doing you can eliminate all your use of Evaluate[] in that code. It is more complicated than I need to explain at the moment, but nothing you have shown in that code is going to need to force evaluation beyond what will happen even if you don't want it to. You can probably toss most of your //N and perhaps just use one at the end.

    Now to the point. Could you slightly modify your code so that it prints out

    "This {{...},{...}...} is the matrix I am about to find roots of
    and these {...} are the roots it finds
    and this {{...},{...}...} is the second matrix I am about to find roots of
    and these {...} are the roots it finds which should be the same ones
    and as you can clearly see they are not."

    and have your code fill in all the "..." and clearly show exactly the function you have constructed to find the roots and then used twice, each time with a fixed matrix as the argument.

    With that I'll try to diagnose what is going on and suggest a fix.

    Just curious, is there any more conventional vector or matrix based notation or method that would make it nearly blindlingly clear exactly what you are doing?
  4. Jan 4, 2012 #3
    With {{Log[2], 0, 0, 0}, {0, Log[1], 0, 0}, {0, 0, Log[3], 0}, {0, 0, 0,
    Log[4]}} the roots are {-4,-3,-2,-1}.

    However with {{Log[1], 0, 0, 0}, {0, Log[2], 0, 0}, {0, 0, Log[3], 0}, {0, 0, 0,
    Log[4]}} the roots are {-3.51802 - 0.380407 I, -3.51802 + 0.380407 I, -1.9155, -1.00066}.

    The correct answer for both matrices is {-4,-3,-2,-1}.

    Does that help or do you want the code revised?
  5. Jan 4, 2012 #4
    So I am guessing there is a sign error or a substitution error in one of the terms.

    If you can pretend to be a fly on the wall for a minute and imagine that you don't know what the answer to this problem is supposed to be, can the fly look at that code and see where the error is? See if thinking about it in that way for a few minutes can tell you what information you need to be able to see so that it is possible to diagnose the error.
  6. Jan 4, 2012 #5


    User Avatar
    Science Advisor

    I copied and pasted your code into Mathematica and when I ran it, I got {-4,-3,-2,-1} in both cases. Did you just mis-type something? The notebook is attached.

    Attached Files:

    • Test.nb
      File size:
      13.7 KB
  7. Jan 5, 2012 #6
    Thank you for your responses...that is very interesting. So it works for physguy but it won't work for me?
  8. Jan 6, 2012 #7


    User Avatar
    Science Advisor

    What do you get when you run the notebook I sent you? Do you get the same thing I got? If so, what is different from what you ran?
  9. Jan 6, 2012 #8
    Ok when I saved the notebook you gave me. I said "Roots:" and gave the correct answer. But the test.nb doesn't say to print. Is this a novice mistake? It seems to not run the code above but other copies, the original code I have prints the roots like that. Is there a cache to clear or do there only need to be one function named that on your whole computer? Thanks for testing my code.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Mathematica-Commutive answers not equal
  1. In mathematica (Replies: 4)

  2. Mathematica NDSolve (Replies: 27)