1

I try to Evaluate something with replacement

Block[{{t1, t2} = NDSolveValue[{x''[t] + x[t] == 0., x[0.] == 1., x'[0.] == 0., 
 WhenEvent[x'[t] > 0., {"StopIntegration"}]}, {Indexed[
  x["Domain"], {1, -1}], x[Indexed[x["Domain"], {1, -1}]]}, {t, 
 0., 100.}, MaxStepSize -> 0.001]}, t1 + t2]

But it gets an Block::lvset error, but if I compute

{t1, t2} = NDSolveValue[{x''[t] + x[t] == 0., x[0.] == 1., x'[0.] == 0., WhenEvent[x'[t] > 0., {"StopIntegration"}]}, {Indexed[
x["Domain"], {1, -1}], x[Indexed[x["Domain"], {1, -1}]]}, {t, 0., 100.}, MaxStepSize -> 0.001]
t1 + t2

is ok. So how can I compute it in one expression?

Michael E2
  • 235,386
  • 17
  • 334
  • 747
JieJiang
  • 93
  • 6
  • 3
    Block doesn't support the syntax Block[{{t1,t2}=…}, please check the document carefully. Simplest fix is probably Block[{t1, t2}, {t1, t2} = NDSolveValue[{x''[t] + x[t] == 0., x[0.] == 1., x'[0.] == 0., WhenEvent[x'[t] > 0., {"StopIntegration"}]}, {Indexed[ x["Domain"], {1, -1}], x[Indexed[x["Domain"], {1, -1}]]}, {t, 0., 100.}, MaxStepSize -> 0.001]; t1 + t2]. If you want to create a Block that supports this syntax, check this post: https://mathematica.stackexchange.com/q/28610/1871 – xzczd Dec 12 '20 at 12:51
  • @xzczd, thank you so much! – JieJiang Dec 13 '20 at 00:27

0 Answers0