1
$\begingroup$

I would like to find roots of legendre polynomial NRoots[LegendreP[4, x]== 0, x] , I'm getting
x == -0.861136 || x == -0.339981 || x == 0.339981 || x == 0.861136 as output.

Instead i need my output as { -0.861136 , -0.339981 , 0.339981 ,0.861136}

Could you please suggest me what type of modification i should make in input code so that i get my output as { -0.861136 , -0.339981 , 0.339981 ,0.861136}

$\endgroup$
2
  • 2
    $\begingroup$ Use NSolve[] instead: x /. NSolve[LegendreP[4, x] == 0, x]. $\endgroup$ Apr 6, 2017 at 2:25
  • $\begingroup$ @J.M. Aside from the format, I would assume that NRoots and NSolve use different algorithms, so, conceivably, the OP would like to use the former for algorithmic reasons?! $\endgroup$
    – Igor Rivin
    Apr 6, 2017 at 3:28

2 Answers 2

3
$\begingroup$

If you prefer NRoots to NSolve as suggested by J.M., you can do

x /. Rule @@ # & /@ (List @@ NRoots[LegendreP[4, x] == 0, x]
$\endgroup$
1
  • 3
    $\begingroup$ I'd use ToRules[] myself if the OP insists on using NRoots[]: x /. {ToRules[NRoots[LegendreP[4, x] == 0, x]]}. $\endgroup$ Apr 6, 2017 at 3:33
2
$\begingroup$

For your specified problem you can Solve for exact solutions

roots = Simplify[x /. Solve[LegendreP[4, x] == 0, x]]

enter image description here

Verifying,

LegendreP[4, #] & /@ roots

(*  {0, 0, 0, 0}  *)

Then, if desired, use N with any desired precision

roots // N[#, 20] &

(*  {-0.33998104358485626480, 0.33998104358485626480, 
     -0.86113631159405257522, 0.86113631159405257522}  *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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