# Use Root object as a function in NonlinearModelFit or similar

I have a matrix and its eigenvalues are returned by Mathematica as a Root Object.

m = (154982971 \[Pi])/120000000000000000000000000
kx=(57*2*Pi*1000)^2*m;
ky = (63*2*Pi*1000)^2*m;
shift =41*lambda;
kz = (0.95*Abs[Cos[shift*Pi/(1.55*10^-6)]]*2.76748*NA*\[Sqrt](-((\[Sqrt](1+1.05166*10^11*shift^2)*(-3.04068*10^-53-1.59889*10^-41*shift^2-3.36298*10^-30*shift^4-3.53672*10^-19*shift^6-1.85972*10^-8*shift^8-3.91159*10^2*shift^10+2.5281*10^-3*shift^12-4.59177*10^17*shift^16)+\[Sqrt](r)*(-1.90651*10^-50-1.00122*10^-38*shift^2-2.10421*10^-27*shift^4-2.21222*10^-16*shift^6-1.16344*10^-5*shift^8-2.44865*10^5*shift^10-3.19614*10^1*shift^12-6.97912*10^11*shift^14+1.05794*10^21*shift^16+8.07794*10^30*shift^18-5.55112*10^41*shift^20)+r*\[Sqrt](1+1.05166*10^11*shift^2)*(-3.04068*10^-53+3.19777*10^-42*shift^2+1.68149*10^-30*shift^4+1.06101*10^-19*shift^6+3.95332*10^-24*shift^8+4.52784*10^-14*shift^10+1.16682*10^-3*shift^12-5.34553*10^7*shift^14))/((9.50876*10^-12+1.*shift^2)^5*\[Sqrt](1+1.05166*10^11*shift^2))))*2*Pi*1000)^2*m;
kappaXY = 7*10^-8;
kappaXZ = 0.7*(63/57)*10^-8;
kappaYZ = 0.7*1*10^-8;
Clear[kappaXY]
A = 1/m*{{0, 0, 0, -kx - kappaXY - kappaXZ, kappaXY, kappaXZ}, {0, 0,
0, kappaXY, -ky - kappaXY - kappaYZ, kappaYZ}, {0, 0,
0, +kappaXZ, +kappaYZ, -kz - kappaXZ - kappaYZ}, {m, 0, 0, 0, 0,
0}, {0, m, 0, 0, 0, 0}, {0, 0, m, 0, 0, 0}};
Eigenvalues[A]


gives among others

0.010266 Root[
2.80529*10^44 + 9.67644*10^50 kappaXY + 6.59487*10^45 Sqrt[r] +
2.27466*10^52 kappaXY Sqrt[r] - 3.58672*10^39 r -
1.23711*10^46 kappaXY r + (2.27095*10^30 +
7.11236*10^36 kappaXY + 9.72679*10^30 Sqrt[r] +
1.66138*10^37 kappaXY Sqrt[r] - 5.29007*10^24 r -
9.03569*10^30 kappaXY r) #1^2 + (2.88981*10^15 +
4.6771*10^21 kappaXY + 3.55217*10^15 Sqrt[r] -
1.9319*10^9 r) #1^4 + #1^6 &, 1]


I want to fit the imaginary part of this expression to data with kappaXY as a parameter and r the variable. Is there any way to do so? If I just use the expression as it is I get the error message

Root::npoly: 9.6764410^50+2.2746610^52 Sqrt[#1]-1.2371110^46 #1+(7.1123610^36+1.6613810^37 Sqrt[#1]-9.035710^30 #1) #1^2+(4.677110^21+3.5521710^15 Sqrt[#1]-1.9319*10^9 #1) #1^4+#1^6 is not a polynomial in #1.

Somehow #1 appeared as a Sqrt now.

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! Jun 12 '21 at 7:35
• You may find it useful to evaluate Eigenvalues[A} // ToRadicals, which will express the eigenvalues without using Root. In order to provide detailed help, people here will want to have values of $m, k_x, k_y, k_z$ so that we can duplicate your results. Jun 12 '21 at 7:43
• Thanks for your answer! I edited such that the constants are included. I think // ToRadicals  does not work for higher order Polynomials no? Jun 12 '21 at 8:26
• (1) I get lambda as a parameter, too. Should it have a value? (2) Something like ev1[kappa_?NumericQ, r_?NumericQ] = Root[..] might work but without runnable code, it's inconvenient to test & tweak. (3) Root objects are not in general continuous functions of parameters. It might or might not be a problem. Jun 13 '21 at 15:57
• If you give numeric definitions to kappaXY, lambda, NA, and r, Eigenvalues yields a numeric result. If you're fitting to data, thats what you want, not a huge symbolic expression. Jun 13 '21 at 21:41