Find all roots of an interpolating function in Mathematica

In summary, the conversation is about finding the x values for which the y value is equal to 2 in an interpolating function. The person has tried using FindRoot and sampling more finely, but is experiencing errors and extrapolation beyond the interpolation range. The solution is to use Union to eliminate repeated answers and prevent going outside of the range.
  • #1
musicgirl
12
0
Hi,

I've got an interpolating function which has been generated from using NDSolve and I'm trying to find all the values of x for which the y value is equal to 2.

I've constructed a (much) easier example to show what I mean.

Suppose I have a set of points which I have generated an interpolating function from:

points = {{0, 0}, {1, 1}, {2, 3}, {3, 4}, {4, 3}, {5, 0}};
ifun = Interpolation[points]

which gives me:

InterpolatingFunction[{{0, 5}}, "<>"]


How would I then go about finding the x values in this function for which y is equal to 2?

The code I have tried is as follows:

Table[x /. FindRoot[ifun[x] == 2, {x, xInit}], {xInit, 0, 10, 1}]

but this produces a lot of errors and extrapolates beyond the limits of my interpolating function. Also, while this finds all the values in this particular example, once the interpolation function gets more complicated it's skipping roots and extrapolating far beyond my limits. Does anyone have any ideas? Thanks in advance
 
Physics news on Phys.org
  • #2
Basically, your idea is sound, you just need to sample more finely and prevent it from going outside of the interpolation range:

dx=0.1;
xmin=0.;
xmax=5.;

Union[
Table[ x /. FindRoot[ifun[x] == 2., {x, xInit, xmin, xmax}], {xInit, xmin + dx, xmax - dx, dx}],
SameTest -> Equal]

The Union function is not necessary, but it trims out all of the repeats of the same answer. That will be particularly useful if you have a more complicated function and have to make dx smaller.
 
Last edited:

1. How can I use Mathematica to find all the roots of an interpolating function?

To find all the roots of an interpolating function in Mathematica, you can use the Roots function. This function takes in the interpolating function as its argument and returns a list of all the roots.

2. Can I specify a range for the roots to be found?

Yes, you can specify a range for the roots to be found by using the Roots function with the option Interval. This option takes in a range in the form of {min, max} and returns only the roots within that range.

3. What if the interpolating function has multiple variables?

If the interpolating function has multiple variables, you can use the Roots function with the option Variables. This option takes in a list of the variables in the function and returns the roots with respect to those variables.

4. Can I visualize the roots of an interpolating function?

Yes, you can visualize the roots of an interpolating function by using the ContourPlot function in Mathematica. This function plots the function and highlights the roots as contours.

5. Is there a way to find only the real roots of an interpolating function?

Yes, you can find only the real roots of an interpolating function by using the NSolve function with the option Reals. This will return only the real roots, if they exist.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
261
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
221
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
898
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
829
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
135
  • Programming and Computer Science
Replies
3
Views
355
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
Back
Top