Finding all the roots of a function with Mathematica

In summary: His code is as follows:f = Log[Erf[x/7]] - Cos[x^2 - 1] + 1;rts = Reduce[f == 0 && 1 < x < 6, x]myvals = N[x /. {ToRules[rts]}]f = Log[Erf[x/7]] - Cos[x^2 - 1] + 1;rts = Reduce[f == 0 && 1 < x < 6, x];myvals = N[x /. {ToRules[rts]}]f = Log[Erf
  • #1
LucianImago
6
0
Hi guys and girls of physics forums,

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 vay I can tell mathematica to find the rest of the roots automatically? I am attaching a plot of the function which shows all the positions of the roots for a given set of parameters.

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

Cheers
 

Attachments

  • Energy levels r=4.png
    Energy levels r=4.png
    15.2 KB · Views: 797
Physics news on Phys.org
  • #2
Use Reduce:

Code:
f = Log[Erf[x/7]] - Cos[x^2 - 1] + 1;
rts = Reduce[f == 0 && 1 < x < 6, x]
myvals = N[x /. {ToRules[rts]}]

I just used an example in Mathematica with a bunch of transcendental roots and then called Reduce to find all the zeros between 1 and 6. Reduce returns a list of logical expressions x= x1 || x=x2 || or x=x3 || and so forth.

The ToRules converts the list of logical expressions to a list of rules x->x1, x->x2, x->x3, and so forth. The N[x/.{ToRules[rts]}] then converts this to a list of numeric values.

Maybe an easier way.
 
  • #3
Thank you very much for your reply. This is exactly what I want to do.

My programming experience is in the likes of Matlab and fortran. I am not used to this sort of structure but I a learning by doing.

Again thanks again.
 
  • #4
Hello everybody,
I am a new user of Mathematica. I have a problem of finding the real roots of a complex function. My equation is a dispersion relation which gives the complex wave growth for the corresponding wave number . I need help to solve this equation in Mathematica 6


\[Rho] = 0.01;
we = 1000;
oh = 0.1;
k =.;
n =.;
m = (Sqrt[k^2 + n^2]);
F[ome_] = (ome + I (we)^0.5 k + 4 m^2 oh)*(ome + I (we)^0.5 k) Tanh[
m] + 4 m^3 oh^2 *(m Tanh[
m] - (m^2 + (ome + I (we)^0.5 k)/oh)^0.5 *
Tanh[(m^2 + (ome + I (we)^0.5 k)/oh)^0.5]) + \[Rho] ome^2 +
m^3;

n = 1;

m = (Sqrt[k^2 + n^2]);
t = Table[{k,
Re[ome /.
FindRoot[F[ome] == 0, {ome, {(1.5 + I)}},
MaxIterations -> 500]]}, {k, 1, 100, 1}]

ListPlot[t, PlotJoined -> True]


I have pasted the corresponding lines from my Mathematica note book i used to solve the equation. I want to make plot for the real part of ome in the y-axis versus wave number K on the x axis.
I am really in need of help as I have been trying to solve it on for weeks by now.
Any body can help me . also contact me at kumar.kannan@uni.lu.

I am having problem in giving the initial guess root.

bye
with regards
K.Suresh kumar
 

FAQ: Finding all the roots of a function with Mathematica

How can I use Mathematica to find all the roots of a function?

To find all the roots of a function with Mathematica, you can use the Solve or NSolve function. These functions take the equation or set of equations as input and return a list of all the solutions, including complex roots.

Can I specify a specific range of values to search for roots in?

Yes, you can use the FindRoot function to specify a specific range of values to search for roots in. This function takes an initial guess for the root and a range of values to search in as input and returns the root within that range.

How can I handle cases where a function has multiple roots?

If a function has multiple roots, Mathematica will return a list of all the roots. You can use the Root function to represent these roots symbolically, or you can use the Reduce function to find a general solution that includes all the roots.

Are there any limitations to using Mathematica to find roots of a function?

While Mathematica is a powerful tool for finding roots of functions, there are some limitations. It may not be able to find exact solutions for highly complex functions, and it may also have difficulty with functions that have multiple variables or parameters.

Can I visualize the roots of a function using Mathematica?

Yes, you can use the ContourPlot or Plot3D functions in Mathematica to visualize the roots of a function. These functions create a graphical representation of the roots by plotting the function in 2D or 3D and showing the points where the function crosses the x-axis.

Similar threads

Replies
4
Views
1K
Replies
5
Views
2K
Replies
9
Views
727
Replies
9
Views
7K
Replies
0
Views
751
Replies
13
Views
2K
Back
Top