TransWikia.com

Obtaining the other axis value

Mathematica Asked by Sosa on May 8, 2021

I’m new at coding and I got this simple differential equiation and the plot of this function involved, I can obtain the value of the dependant variable based on the independant varible as usual, what I want to do now is to find a new value of the independant variable giving it the dependant variable based on the ploted graph, in other words I want to find y for a given x from the graph. this is my short code. Only half of it works.

Sol = NDSolve[{a'[t] == Sqrt[a[t]], a[0] == 3}, a, {t, 0, 180}]
graph = Plot[a[t] /. Sol, {t, 0, 180}]
FindRoot[a[t] == 0.44 /. graph, {t, 0}] (*this is the part that doesn't work*)
a[t] /. Sol /. t -> 180

Any kind of help would be hugely appreciated. Thanks c:

2 Answers

First, you should not use variable names with capital letters, those are used by the system. I corrected your syntax. Here is the working code. Note that the FindRoot gives warnings because the specified value is out of bounds:

sol[t_] = 
 a[t] /. NDSolve[{a'[t] == Sqrt[a[t]], a[0] == 3}, a, {t, 0, 180}][[1]]
graph = Plot[sol[t], {t, 0, 180}]
FindRoot[sol[t] == 0.4, {t, 0}] 
sol[180]

Answered by Daniel Huber on May 8, 2021

Another way is use WhenEvent and Reap and Sow.(Also need to replace 0.44 to another number such as 10).

{{sol}, {pts}} = 
 NDSolve[{a'[t] == Sqrt[a[t]], a[0] == 3, 
    WhenEvent[a[t] == 10, {Sow[t]}]}, a, {t, 0, 180}] // Reap
Plot[a[t] /. First@sol // Evaluate, {t, 0, 10}, 
 Epilog -> {PointSize[Large], Red, 
   Point[({#, a[#]} /. sol) & /@ pts]}]

{{2.86045}}

enter image description here

Appendix

The questioner's original approach.

sol = NDSolve[{a'[t] == Sqrt[a[t]], a[0] == 3}, a, {t, 0, 180}]
graph = Plot[a[t] /. sol, {t, 0, 180}]
FindRoot[a[t] == 10 /. sol, {t, 
  0}] (*this is the part that doesn't work*)
a[t] /. sol /. %

{t -> 2.86045}

{10.}

Answered by cvgmt on May 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP