0
$\begingroup$

I want to solve the ode and plot the solution v[x] for different values of parameter a where x=2000 (for just any fixed position x or event at x). By looking at examples of EventLocator available online i tried to write the code as:

data = Table[Reap[
    soln = 
     NDSolve[{I D[v[x], x] == (0.4 + a)*v[x], v[0] == 1}, {v[x]}, {x, 0, 2000},
      Method -> {"EventLocator", 
        "Event"          -> Abs[v[x]]^2, 
        "EventCondition" -> x == 1000,
        "EventAction"    :> Sow[{a, Evaluate[Abs[v[x]]]^2 /. soln}]}]][[2, 1]],
  {a, -1, 0, 0.3}]

But it does not give data file i wanted to grab as{a,[Abs[v[x]]^2}. instead it shows:

Part::partw: Part 1 of {} does not exist. >>
Part::partw: Part 1 of {} does not exist. >>
Part::partw: Part 1 of {} does not exist. >>
General::stop: Further output of Part::partw will be suppressed during this calculation. >>

{{{{v[x]->InterpolatingFunction[{{0.,2000.}},<>][x]}},{}}[[2,1]],
{{{v[x]->InterpolatingFunction[{{0.,2000.}},<>][x]}},{}}[[2,1]],
{{{v[x]->InterpolatingFunction[{{0.,2000.}},<>][x]}},{}}[[2,1]],
{{{v[x]->InterpolatingFunction[{{0.,2000.}},<>][x]}},{}}[[2,1]]}

Any kind of help or suggestion will great .Even if you can help using other method like using for loop and logical condition that will be great too.

$\endgroup$
3
  • $\begingroup$ I think you don't need the soln bits. This will work data = Table[ Reap[NDSolve[{I D[v[x], x] == (0.4 + a)*v[x], v[0] == 1}, {v[x]}, {x, 0, 2000}, Method -> {"EventLocator", "Event" -> Abs[v[x]]^2, "EventCondition" -> x == 2000, "EventAction" :> Sow[{a, Abs[v[x]]^2}]}]], {a, -1, 0, 0.3}] You don't get any points because Abs[v[x]] is never zero. $\endgroup$ – b.gates.you.know.what Jul 19 '12 at 10:48
  • $\begingroup$ I tried that but did not work. Thanks. $\endgroup$ – Mush Jul 19 '12 at 11:52
  • $\begingroup$ Could you specify what does not work ? $\endgroup$ – b.gates.you.know.what Jul 19 '12 at 13:37
1
$\begingroup$

Maybe you can do it more simply :

xvalue = 1000;
avalues = Range[-1, 0, 0.3];
results = Table[NDSolve[{I D[v[x], x] == (0.4 + a)*v[x], v[0] == 1}, 
 v[x], {x, 0, 2000}][[1, 1, 2]] /. x -> xvalue, {a, avalues}]

(* {-0.999044 + 0.0441925 I, -0.0221014 - 0.999765 I, 1. + 0. I, -0.0221014 + 0.999765 I} *)

solution

$\endgroup$
9
  • $\begingroup$ Its working! Thanks a lot! :) $\endgroup$ – Mush Jul 19 '12 at 11:53
  • $\begingroup$ Do you have a BRG ordered screen? $\endgroup$ – Szabolcs Jul 19 '12 at 13:43
  • $\begingroup$ @Szabolcs I'm sorry but I don't know what it is. $\endgroup$ – b.gates.you.know.what Jul 19 '12 at 13:59
  • 1
    $\begingroup$ @Mush More generally than row/column, which applies to 2 dimensional lists, [[...]] (shorthand for Part) applies to lists of any dimensions. In my case [[1,1,2]] extracts the InterpolationFunction solution to the equation so I can calculate its value at the point of interest. $\endgroup$ – b.gates.you.know.what Jul 19 '12 at 19:54
  • 1
    $\begingroup$ @Mush The solution is in the the form example = {{v[x] -> InterpolationFunction[...]}}; example[[1]] = {v[x] -> InterpolationFunction[...]}; example[[1,1]] = v[x] -> InterpolationFunction[...]; example[[1,1,2]] = InterpolationFunction[...]; $\endgroup$ – b.gates.you.know.what Jul 19 '12 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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