# How can I get the value of a at “t=2.4985352432136567” in the following expression?

By running the following code:

a = Sin[y[t]];
sol = NDSolve[{Derivative[2][y][t] + 0.1*Derivative[1][y][t] + Sin[y[t]] == 0,
Derivative[1][y][0] == 0, y[0] == 1}, y,
{t, 0, 10}, Method -> {"EventLocator", "Event" -> Derivative[1][y][t] - y[t]}]


we can obtain that the "Event" happens at t = 2.4985352432136567 . The value of y[2.4985352432136567] can be obtained by using y[2.4985352432136567] /. sol, which appears to be -0.589753.

However, when I apply this method to a for a[2.4985352432136567] /. sol, the results appear to be {Sin[InterpolatingFunction[{{0., 2.49854}}, <>][t]][2.49854]}, and no specific value is obtained.

My question is, how can I get the specific value of a at t = 2.4985352432136567?

-
Try defining a[t_] = Sin[y[t]]. –  b.gatessucks Sep 17 '12 at 8:10

## 4 Answers

What I'd do is to define the solution as a regular function :

sol[t_] = NDSolve[{Derivative[2][y][t] + 0.1*Derivative[1][y][t] + Sin[y[t]] ==
0, Derivative[1][y][0] == 0, y[0] == 1}, y[t], {t, 0, 10},  Method -> {"EventLocator",
"Event" -> Derivative[1][y][t] - y[t]}][[1, 1, 2]]


Now you can use it as any other function :

sol[2.4985352432136567]
(* -0.589753 *)

Plot[{sol[t], Sin[sol[t]]}, {t, 0., 2.4}]


-
Thanks for your help, it works well! –  SunnySky Sep 17 '12 at 8:47
It's a bit cleaner to have the second argument of NDSolve[] be y instead of y[t], methinks. Then, you can do something like sol[t_] = y[t] /. First@NDSolve[eqns, y, {t, tmin, tmax}, opts]... –  Guess who it is. Sep 18 '12 at 3:39

With your definition of a

 a[2.4985352432136567]


is Sin[y[t]][2.49854] (which explains the output you get from a[2.4985352432136567] /.sol)

To get Sin[y[2.49854]] you can use

 a /. t -> 2.4985352432136567
(*  Sin[y[2.49854]] *)


and

 a /. t -> 2.4985352432136567 /. sol


gives {-0.556156}.

Alternatively, you can make t an explicit argument of a as suggested in @b.gatessuckss comment.

-
Thanks for your help, that works indeed! –  SunnySky Sep 17 '12 at 8:47
@SunnySky, my pleasure. –  kglr Sep 17 '12 at 9:06
a = Sin[y[t]];


assigns the sine value to the variable a. If you would have defined the function y[t] to return a numeric value and have t assigned a value, then a would be assigned the numeric result of the function; otherwise, it would get the unevaluated value of Sin[y[t]].

You use the Set function (=) to assign the value to a, which means that the RHS will be evaluated at the moment of the assignment, and won't change later. If you want a to change with the value of t you'll have to use SetDelayed (:=), then it will be evaluated each time it's referenced.

But that still leaves a as a variable, which you can't pass an argument to, like a function. If you want to define a as a function of t, you have to write it as

a[t_] = Sin[y[t]];

-
Thanks for your detailed explanation, and it is helpful to me. –  SunnySky Sep 18 '12 at 0:07

The "EventAction" sub-option of "EventLocator" seems to have not been mentioned thus far; this, in combination with the Sow[]/Reap[] pair, can be used to find all the values of t such that y[t] == y'[t], and evaluate Sin[y[t]] at those points. Witness the following:

{fun, pts} = Reap[y /. First @
NDSolve[{y''[t] + 0.1 y'[t] + Sin[y[t]] == 0, y[0] == 1, y'[0] == 0},
y, {t, 0, 10}, Method -> {"EventLocator",
"Event" -> y'[t] - y[t],
"EventAction" :> Sow[{t, Sin[y[t]]}]}]] // MapAt[First, #, 2] &
{InterpolatingFunction[{{0., 10.}}, <>],
{{2.49854, -0.556156}, {5.7876, 0.480503}, {9.03428, -0.413819}}}

Plot[{fun[t], Sin[fun[t]]}, {t, 0, 10},
Epilog -> {AbsolutePointSize[7], Red, Point[pts]}, Frame -> True]
`

-