Re: Applying FindRoot to Interpolating Functions
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1804] Re: Applying FindRoot to Interpolating Functions
- From: rknapp (Robert Knapp)
- Date: Thu, 17 Aug 1995 00:01:26 -0400
- Organization: The Santa Fe Institute
In article <DDACz2.GK8 at wri.com> mdaeng!acf2.nyu.edu!adler at uu4.psi.com (adler) writes: > > Hi, > I need some help with the Interpolating Function, created > by NDSolve. I am using it to solve two second order coupled ordinary > differential equations. It does so easily and I have no > troble the solutions, x[t] and y[t]. Now I need to find the > first value of t where x[t] == y[t]. I tried to use NRoots and > FindRoot, but they did not work. I can do the problem by > inspection, looking at where the curves, x[t] vs t and y[t] vs t > intersect, but I am looking for something more accurate. > > Any help would be greatly appreciated. > > Thanks, > > Andre Adler NRoots is for polynomials, so that function will not work, however it IS possible to use InterpolatingFunction objects with FindRoot and other numerical functions. Without seeing exactly what you were working with, I cannot tell what went wrong. Perhaps you can use the structure of the following example to obtain the result you wish. In[1]:= sol = NDSolve[{x'[t] == y[t],y'[t] == -x[t], x[0] == 1,y[0] == 0},{x,y},{t,0,Pi}] Out[1]= {x -> InterpolatingFunction[{0., 3.14159}, <>], y -> InterpolatingFunction[{0., 3.14159}, <>]} In[2]:= FindRoot[Evaluate[(x /. sol[[1]])[t] - (y /. sol[[1]])[t] == 0], {t,2}] Out[2]= {t -> 2.35619} Note the use of the first part of sol (sol[[1]])-without this, FindRoot gets a bit confused because is is seeing the difference of two lists being set equal to something which is not a list. Also, the Evaluate, while not absolutely necessary in this case, is a good idea because FindRoot is a HoldAll function. This is by no means the most efficient way to do this, however. It takes a significant amout of time to differentiate and InterpolatingFunction object (this will not be a problem in the next version to be released.) The differentiation can be avoided un two ways. First, if you give two starting points, FindRoot will use and approximate derivative. Second, you can use the differential equations to supply the Jacobian, as in In[3]:= FindRoot[Evaluate[(x /. sol[[1]])[t] - (y /. sol[[1]])[t] == 0], {t,2}, Evaluate[Jacobian->{{(y /. sol[[1]])[t]+(x /. sol[[1]])[t]}}]] Out[3]= {t -> 2.35619} Rob Knapp WRI