Find all roots of an interpolating function in Mathematica

Click For Summary
SUMMARY

This discussion focuses on finding all roots of an interpolating function in Mathematica, specifically when the y-value equals 2. The user initially attempted to use the FindRoot function but encountered errors due to extrapolation beyond the interpolation limits. The solution provided involves refining the sampling by adjusting the step size and ensuring the search remains within the defined range using Union to eliminate duplicate roots. The corrected code snippet is: Union[Table[x /. FindRoot[ifun[x] == 2., {x, xInit, xmin, xmax}], {xInit, xmin + dx, xmax - dx, dx}], SameTest -> Equal].

PREREQUISITES
  • Understanding of Mathematica syntax and functions
  • Familiarity with NDSolve for generating interpolating functions
  • Knowledge of root-finding techniques in numerical analysis
  • Basic concepts of interpolation and function behavior
NEXT STEPS
  • Explore advanced features of FindRoot in Mathematica
  • Learn about different interpolation methods in Mathematica
  • Investigate error handling and debugging techniques in Mathematica
  • Study the implications of sampling density on root-finding accuracy
USEFUL FOR

Mathematica users, numerical analysts, and anyone involved in computational mathematics who needs to find roots of interpolating functions efficiently.

musicgirl
Messages
12
Reaction score
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
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:

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K