Mathematica-Commutive answers not equal

  • Mathematica
  • Thread starter lpk7096
  • Start date
In summary, Lauren is trying to find the roots of a polynomial using two matrices and she is getting two different answers. Her code is filled in with a summary of what is happening, but it is difficult to see where the error is.
  • #1
lpk7096
4
0
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] //
N;
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*)

roots[A]

Thanks,
Lauren
 
Physics news on Phys.org
  • #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?
 
  • #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?
 
  • #4
lpk7096 said:
The correct answer for both matrices is {-4,-3,-2,-1}

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.
 
  • #5
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.
 

Attachments

  • Test.nb
    13.7 KB · Views: 359
  • #6
Thank you for your responses...that is very interesting. So it works for physguy but it won't work for me?
 
  • #7
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?
 
  • #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.
 

Related to Mathematica-Commutive answers not equal

What is Mathematica-Commutive?

Mathematica-Commutive is a software program developed by Wolfram Research that is primarily used for mathematical and scientific computing. It provides a wide range of tools and functions for performing complex calculations, data analysis, and visualization.

What does "commutive answers not equal" mean?

"Commutive answers not equal" refers to the commutative property in mathematics, which states that the order in which two numbers are multiplied does not affect the result. In other words, the answer should be the same regardless of the order in which the numbers are multiplied. If the answers are not equal, it could indicate an error in the calculation or an issue with the software.

Why are my commutive answers not equal in Mathematica?

There could be several reasons for this. It could be due to a mistake in the input, an error in the code, or a limitation of the software. It is also possible that the calculation involves numbers with a high degree of precision, and the slight differences in the answers are due to rounding errors.

How can I troubleshoot commutive answers not equal in Mathematica?

If you encounter this issue, the first step is to carefully check your input and make sure there are no mistakes or typos. If the problem persists, you can try using a different method or function to perform the calculation. You can also consult the Mathematica documentation or seek help from the community for assistance in troubleshooting the issue.

Is it normal for commutive answers not to be equal in Mathematica?

In most cases, commutive answers should be equal in Mathematica. However, as mentioned earlier, there are instances where slight differences may occur due to precision or other factors. It is always a good idea to double-check your input and consider the context of the calculation before assuming that the answers should be equal.

Similar threads

Replies
3
Views
383
  • Calculus and Beyond Homework Help
Replies
5
Views
330
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
2K
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
Replies
3
Views
1K
Replies
7
Views
1K
  • Math Proof Training and Practice
2
Replies
69
Views
4K
  • Precalculus Mathematics Homework Help
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
15
Views
4K
Back
Top