Re: Getting the value of the independent var from the dep.var using NDSolve
- To: mathgroup at smc.vnet.net
- Subject: [mg63715] Re: Getting the value of the independent var from the dep.var using NDSolve
- From: rknapp at wolfram.com
- Date: Wed, 11 Jan 2006 06:49:37 -0500 (EST)
- References: <dptd4r$be$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
pradeep suresh wrote:
> Hi all,
> i have a specific problem of finding the point at which the dependent
> variable takes on a particular value in teh solution of a differential
> equation.
> e.g. for simplicity
> we have the equation y'[t]==t, and the initial cond. y[0]==0.y is the
> dep. variable and t is the independent variable.
> i want to find out the 't' at which y[t]==p(p is known, say 8)
> and since i am already using NDSolve to solve this and other equations
> i'd like to achieve this working around NDSolve,
> could anyone suggets a solution to this problem??
> thanks a ton!
> Pradeep
The tidiest way to do this is with the EventLocator method in NDSolve.
For your example:
p = 8;
sol = NDSolve[{y'[t] == t, y[0] == 0}, y, {t, 0, 10},
Method -> {"EventLocator", "Event" -> y[t] - p, "EventAction" :>
(ptime = t)}];
ptime
returns 4.
The event is just a function that has a root when the condition you
want occurs.
The event action is an expression that is evaluated with the values
when the event
occurs. Note that it is given with RuleDelayed (:>) so that it does not
get evaluated
until the event occurs.
There are several examples of this method in the advanced documentation
for NDSolve:
http://documents.wolfram.com/mathematica/Built-inFunctions/AdvancedDocumentation/DifferentialEquations/NDSolve/ODEIntegrationMethods/ControllerMethods/EventLocation.html
If you think the event may occur several times, then you can use Reap
and Sow as suggeested in another response.
For example:
In[4]:=
p = 8.;
Reap[sol = NDSolve[{y''[t] + y[t] == 0, y[0]== 10, y'[0]
==0},y,{t,0,100},
Method->{"EventLocator", "Event"->y[t] - p,
"EventAction":>Sow[t]}]]
Out[5]=
{{{y->InterpolatingFunction[{{0.,100.}},<>]}},{{0.643501,5.63968,6.92669,\
11.9229,13.2099,18.2061,
19.4931,24.4892,25.7762,30.7724,32.0594,37.0556,38.3426,43.3388,44.6258,49.\
622,50.909,55.9052,57.1922,62.1884,63.4754,68.4715,69.7585,74.7547,76.0417,
81.0379,82.3249,87.3211,88.6081,93.6043,94.8913,99.8875}}}
gives all the times at which the event occurs.