3
$\begingroup$

I am trying to write a loop that will take the first n derivatives (lets say n =3), which outputs a list that will include 1(or more) roots for each derivative. I haven't actually set up the loop because I'm not quite sure which I want to use. (Do, For, While, maybe even Table) Here is what I have so far and I know I will need to use each of these somewhere in my loop.

 Clear[x,roots,x,n];
 f[x_] = x^8 - 3 x^5 + x - 1;

 NSolve[f'[x] == 0, x, Reals];

 Table[D[f[x], {x, n}], {n, 1, 3}]

I'm just not sure how I should start honestly.. I've been having difficulties with loops in Mathematica. Even a simple suggestion as to which loop I should use would be great (I know you could solve this using any of the loops but I'm not that familiar with them yet).

Any help/advice is always appreciated!

Brandon

* I GOT IT!*

 Clear[x, roots, x, n, d];
 f[x_] = x^8 - 3 x^5 + x - 1;
 n = 5;
 der = D[f[x], {x, #}] & /@ Range[n];
 roots = Table[NSolve[der[[#]] == 0, x, Reals] & /@ Range[n]];
 Grid[{der, roots} // Transpose]

Thanks to all.

$\endgroup$
2
1
$\begingroup$

You don't really need a loop at all. Here is the function and it's n derivatives:

f[x_] := x^8 - 3 x^5 + x - 1;
n=3;
der = D[f[x], {x, #}] & /@ Range[n]

Then map NSolve to all the answers:

NSolve[der[[#]] == 0, x] & /@ Range[n]
$\endgroup$
3
  • $\begingroup$ Hey Bill. Thanks for the response! That was pretty straight forward. I know I don't "need" to use a loop but I'm trying to practice my procedural programming skills in Mathematica. Of course there are a lot of built in commands that can get the same job done, like the way you just mentioned! Thanks again. $\endgroup$ – Brandon Oct 15 '16 at 5:19
  • $\begingroup$ Alternatively: With[{f = Function[x, x^8 - 3 x^5 + x - 1], n = 3}, Table[x /. NSolve[Derivative[k][f][x] == 0, x], {k, n}]] $\endgroup$ – J. M.'s ennui Oct 15 '16 at 5:42
  • $\begingroup$ @J.M. Thanks! I was just having some concerns because although the output displays all the roots, how can I visually see which derivatives correspond to those roots? That's where I was thinking the "AppendTo" command would be useful. $\endgroup$ – Brandon Oct 15 '16 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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