Mathematica Finding the first roots of a function with Mathematica

AI Thread Summary
The discussion revolves around finding multiple roots of a single-variable function in Mathematica, particularly for a user transitioning from Mathcad. The user initially attempted to use FindRoot but faced challenges due to the need for specific starting values and the nature of trigonometric functions, which have infinite solutions. Suggestions included using FindRoot in a loop with incremented starting points to tabulate roots, but this approach risked returning duplicate roots. A more efficient solution was proposed using the FindInstance function, which allows for finding multiple roots within a specified range. This method can efficiently return roots in order, with the recommendation to request more roots than needed and then select the first ten. The user expressed satisfaction with this solution, indicating it met their requirements.
Prometteus
Messages
8
Reaction score
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
Perhaps NSolve is more like what you want.
 
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
 
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.
 
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
 
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.
 
Just have a while loop for the stop command, ie when a root is returned succesfully increase the counter, counter hits 10, end.
 
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.
 
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:
 
Back
Top