Register to reply

Find all roots of an interpolating function in Mathematica

by musicgirl
Tags: function, interpolating, mathematica, roots
Share this thread:
Jun8-12, 06:59 AM
P: 12

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
Phys.Org News Partner Science news on
Scientists develop 'electronic nose' for rapid detection of C. diff infection
Why plants in the office make us more productive
Tesla Motors dealing as states play factory poker
Jun9-12, 07:08 PM
P: 17,329
Basically, your idea is sound, you just need to sample more finely and prevent it from going outside of the interpolation range:


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.

Register to reply

Related Discussions
Mathematica: Find Maximum Value of Interpolating Function Math & Science Software 3
Finding the first roots of a function with Mathematica Math & Science Software 9
Find all roots of a function Calculus & Beyond Homework 3
Using interpolating functions in Mathematica Math & Science Software 1
Finding all the roots of a function with Mathematica Math & Science Software 3