1

I have a $45\times 45$ matrix $H$ and its elements depend on $Ca$. I want to find its eigenvalues (which will also depend on $Ca$). I do this using:

evals = Simplify[Eigenvalues[H], Element[{Ca}, Reals]];

This gives me all the 45 eigenvalues of $H$. Now, I want see the dependence of these eigenvalues on $Ca$ (by plotting them against $Ca$). This is done using:

data1 = Transpose[{x, evals[[1]] /. Ca -> x}];

I do this for different evals[[i]] and in most of the cases, I get the following error:

Root::npoly: {0. +4. #1-4. #1^2+#1^3,2.4875*10^-11+3.9802 #1-3.99015 #1^2+#1^3,7.92*10^-10+3.96079 #1-3.9806 #1^2+#1^3,5.98388*10^-9+3.94177 #1-3.97135 #1^2+#1^3,2.5088*10^-8+3.92314 #1-3.9624 #1^2+#1^3,<<42>>,0.00438622 +3.43457 #1-3.86135 #1^2+#1^3,0.00484128 +3.43002 #1-3.8656 #1^2+#1^3,0.00533172 +3.42579 #1-3.87015 #1^2+#1^3,<<351>>} is not a polynomial in #1.

First, I thought this may be caused because not all eigenvalues are dependent on $Ca$, so I tired to check this using:

Table[
 With[{globalQ = Context@# === "Global`" &}, DeleteDuplicates@Cases[evals[[j]], _Symbol?globalQ, Infinity]],
 {j, 1, 45}]

I got this code from here: Extracting variables from an expression. This gives me:

{{Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}, {Ca}}

This means that all eigenvalues are, in fact, dependent on $Ca$.

I want to know what is the cause of this error? I am just substituting the values. Why is Mathematica calculating the roots? Also, how do I make my code work correctly?

I have just started using Mathematica, so its possible I'm interpreting the error totally wrong.

  • 1
    Transpose[{x, evals[[1]] /. Ca -> x}] Why are you transposing? If you want to plot, just do something like Plot[evals[[1]],{Ca,-10,10}] – flinty Jul 19 '20 at 17:47
  • 1
    Note that the argument that Root is complaining about looks like a vector of polynomials, not just one polynomial. I thought x might be a vector, but I didn't get the same form when I tried it. – Michael E2 Jul 19 '20 at 17:50
  • 1
    Do you get any other errors than just the one? – Michael E2 Jul 19 '20 at 17:51
  • 1
    Is it possible that you are replacing x with a list of values? Root is not listable, so to build data1 you should map over the different values of x, i.e. data1 = Transpose[{#, evals[[1]] /. Ca -> #}&/@x]. – Hausdorff Jul 19 '20 at 19:41

1 Answers1

1

An MWE is helpful. Here's my stab at it:

SeedRandom[0];
evals = Eigenvalues[
   RandomReal[1, {5, 5}] + DiagonalMatrix[Ca Range@4, 1]];

Block[{x = Range@3}, evals[[1]] /. Ca -> x ]

Root::npoly: {-1.01544-1.93459 #1-0.974557 #1^2-0.234265 #1^3-0.121439 #1^4+0.0463375 #1^5,<<9>>+<<20>> #1^5,<<1>>} is not a polynomial in #1.

Root::npoly:....

Root[-0.00380005 - 0.0522129 {1, 2, 3} + 0.0359471 {1, 2, 3}^2 + 
   0.00462834 {1, 2, 3}^3 - 1. {1, 2, 
     3}^4 + (-0.00311075 - 0.187362 {1, 2, 3} - 
      0.456386 {1, 2, 3}^2 - 1.28773 {1, 2, 3}^3) #1 + (-0.00177307 - 
      0.227039 {1, 2, 3} - 0.745744 {1, 2, 3}^2) #1^2 + (-0.047883 - 
      0.186382 {1, 2, 3}) #1^3 - 0.121439 #1^4 + 0.0463375 #1^5 &, 1]

If I try Transpose[{x, evals[[1]] /. Ca -> x}], I get a Transpose::nmtx error, too, which the OP doesn't report. Notice that the form of the Root obtained does not match the form in the OP's error message.

Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • I think the error appears in your case, because you are replacing Ca with the entire x={1,2,3}. If you instead map over x, I get no errors and {-1.07633, 6.69851, 8.329} as the result. – Hausdorff Jul 19 '20 at 19:36
  • @Hausdorff I know why I get an error. I was trying to replicate the OP's problem with a minimal example. To get the same form of result as the OP, I would have to add an extra step, something like this: Block[{x = Range@3}, evals[[1]] /. Ca -> x] /. Function[body_] :> With[{b = body}, Function[b] /; True] -- I don't know how the OP got what is shown; consequently, I'm unsure what the OP was trying to do. – Michael E2 Jul 19 '20 at 19:43
  • Oh, sorry, that went completely over my head. I see your point about the output – Hausdorff Jul 19 '20 at 19:53
  • @Hausdorff Actually I just noticed that the form in the error messages of my example and in the OP’s do look the same. They just do not match the output. The error message must pull out the body of the function (and let it evaluate). How careless of me. I guess I did construct a possible MWE. Time for bed, though. – Michael E2 Jul 20 '20 at 04:14