Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  b.gatessucks Jul 19 '12 at 10:48
    
I tried that but did not work. Thanks. –  Mush Jul 19 '12 at 11:52
    
Could you specify what does not work ? –  b.gatessucks Jul 19 '12 at 13:37
add comment

1 Answer

up vote 1 down vote accepted

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

share|improve this answer
    
Its working! Thanks a lot! :) –  Mush Jul 19 '12 at 11:53
    
Do you have a BRG ordered screen? –  Szabolcs Jul 19 '12 at 13:43
    
@Szabolcs I'm sorry but I don't know what it is. –  b.gatessucks Jul 19 '12 at 13:59
1  
@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. –  b.gatessucks Jul 19 '12 at 19:54
1  
@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[...]; –  b.gatessucks Jul 19 '12 at 20:44
show 5 more comments

Your Answer

 
discard

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.