Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Try defining a[t_] = Sin[y[t]]. – b.gatessucks Sep 17 '12 at 8:10

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 :

(* -0.589753 *)

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


share|improve this answer
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]... – J. M. Sep 18 '12 at 3:39

With your definition of a


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]] *)


 a /. t -> 2.4985352432136567 /. sol

gives {-0.556156}.

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

share|improve this answer
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]];
share|improve this answer
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]

plot of solution and "special points"

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.