Finding the first roots of a function with Mathematica

In summary, you can use the "FindInstance" function to find all the roots of a function between 0 and 1000.
  • #1
Prometteus
8
0
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.
 
Physics news on Phys.org
  • #2
Perhaps NSolve is more like what you want.
 
  • #3
NSolve doesn't 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
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
FunkyDwarf said:
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.

But how to introduce such "stop" in FindRoot function? I have seen the entire documention and it not say anything about it.
Thanks
 
  • #6
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:
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
Just have a while loop for the stop command, ie when a root is returned succesfully increase the counter, counter hits 10, end.
 
  • #8
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
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
Thank you all, Hepth that exactly what i wanted. I´ll try with FindIstance.
Thanks a lot:biggrin:
 

FAQ: Finding the first roots of a function with Mathematica

How do I find the first roots of a function using Mathematica?

To find the first roots of a function using Mathematica, you can use the FindRoot function. This function takes in the function you want to find roots for, as well as an initial guess for the root. It then uses numerical methods to find the root of the function.

Can I find multiple first roots of a function with Mathematica?

Yes, you can find multiple first roots of a function with Mathematica by using the FindRoot function with different initial guesses for the root. You can also use the FindAllCrossings function to find all the roots of a function within a given interval.

How accurate are the first roots found by Mathematica?

The accuracy of the first roots found by Mathematica depends on the precision specified in the FindRoot function. By default, Mathematica uses machine precision, but you can specify a higher precision if needed. The accuracy of the roots also depends on the complexity of the function and the initial guess provided.

What if I have a complex function? Can Mathematica still find the first roots?

Yes, Mathematica can find the first roots of complex functions. It uses powerful numerical algorithms to handle complex functions and can handle both real and complex roots.

Are there any other functions in Mathematica that can help me find roots?

Yes, besides FindRoot and FindAllCrossings, there are other functions in Mathematica that can help you find roots of a function. Some of these functions include NSolve, Reduce, and Roots. It is always a good idea to explore and experiment with different functions to find the best approach for your specific problem.

Similar threads

Replies
4
Views
1K
Replies
3
Views
2K
Replies
5
Views
2K
Replies
9
Views
727
Replies
2
Views
2K
Back
Top