# Finding the first roots of a function with Mathematica

Tags:
1. Apr 16, 2010

### Prometteus

Hello everyone,

I have just created my account here and so this is my first post and I would like to appologise if my question may have been posted by someone else.

I am new to mathematica but I am very found of the program. So much so that I am trying to use it for one of my research projects as an alternative for mathcad which I've been using for many years and with many frustrations.

I have managed to work my way through functions and what not (I am still learning) but at the moment I've reached a delicate problem. I have this single variable function which has many roots and I would like to know how can I find them all.

I can use FindRoot and get one root at the time given I chose the proper starting value. But is there a way I can tell mathematica to find, for exemple, the first 5 or 10 roots?

As I said I am quite knew to Mathematica so this question might sound silly but any help would be much appreciated.

2. Apr 17, 2010

### Filip Larsen

Perhaps NSolve is more like what you want.

3. Apr 17, 2010

### Prometteus

NSolve doesnt solve it, appears the follow message: " The equations appear to involve the variables to be solved for in an essentially non-algebraic way. "
I have to really use FindRoot, but how the equation involves trigonometry has inifinite solutions. I Just want the first 10.
Thanks

4. Apr 17, 2010

### FunkyDwarf

You can restrict the search range of FindRoot and tabulate a series of them moving the search region along the number line then stop when you find the 10th.

5. Apr 17, 2010

### Prometteus

But how to introduce such "stop" in FindRoot function? I have seen the entire documention and it not say anything about it.
Thanks

6. Apr 18, 2010

### Hepth

I don't think its actually possible with only using FindRoot. What you have to do is write a little program that cycles through running FindRoot with increasing start points, and have it check to see if the new ones are the same as the old. Such as :

Code (Text):
F[t_] = Sin[t];
t0 = 0.0;
\[Delta]t = \[Pi]/5.;
count = 0;
roots[count] =
FindRoot[F[t], {t, t0}][[1]][[2]]; t0 += \[Delta]t; count++;
While[count < 10, roots[count] = FindRoot[F[t], {t, t0}][[1]][[2]];
t0 += \[Delta]t; If[roots[count] != roots[count - 1], count++]]
Table[roots[i]/\[Pi], {i, 0, 9}]
That code doesn't work really well, and if the function changes its very very wrong, but I hope you get the idea. You get a lot of repeats of roots, but I think if you do something with the accuracy of the values you can get that 1. == 1. instead of arbitrary precision saying its not.

7. Apr 18, 2010

### FunkyDwarf

Just have a while loop for the stop command, ie when a root is returned succesfully increase the counter, counter hits 10, end.

8. Apr 19, 2010

### Hepth

But that doesn't work, like in some code i tried it would find say, the root of Sin[t] at 0, but also one at 1.56*10^-17 (basically zero). So its a false root/duplicate. Theres probably a way to round everything to 3 decimal places before the comparison to throw out duplicates.

I wish there was just a way to do it with one command.

9. Apr 19, 2010

### Hepth

Ah I think I figured it out. Theres a function "FindInstance"
Use it like:

FindInstance[{Sin[t] Cos[t] == 0, t > 0, t < 100}, t, 10^7]

So it finds all 10^7 roots between 0 and 100. Since only a finite number exist, it just reports all of them, and does so in ORDER.
If you only have it say, find 3 of them, when more than 3 exist, it may choose ANY THREE rather than the first three. So have it choose wayyy more than you want it to and just take the first 10.

RTemp = FindInstance[{Sin[t] Cos[t] == 0, t >= 0, t < 1000}, t, 10^7];
roots = Table[RTemp[][[1]][[2]], {i, 1, 10}]

10. Apr 21, 2010

### Prometteus

Thank you all, Hepth that exactly what i wanted. I´ll try with FindIstance.
Thanks a lot