2
$\begingroup$

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:

$\endgroup$
3
  • $\begingroup$ Could you provide a definition? $\endgroup$
    – Ben Izd
    Feb 24 at 6:37
  • $\begingroup$ How so? what do you mean? $\endgroup$
    – Sosa
    Feb 24 at 6:46
  • $\begingroup$ Sorry, it was my misunderstanding, you can't use /. graph, replace graph with Sol, you will get {t -> -0.0163865} which is out of bound and a[-0.0163865] /. Sol is around {2.98117}, technically you'd find a local minimum not the exact answer you're looking for. $\endgroup$
    – Ben Izd
    Feb 24 at 7:27
2
$\begingroup$

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.}

$\endgroup$
3
  • $\begingroup$ i just want to read the graphic at another value i.e.30 in the Y axis what is in the X axis, from that same graph $\endgroup$
    – Sosa
    Feb 24 at 18:11
  • $\begingroup$ {a'[t] == (0.16 - 0.0622 Sqrt[a[t]])/(Pi (a[t])^2), a[0] == 0.5} in this equation in it's graph, if i give it a value of 3 in the Y axis what it's the value on the X axis? $\endgroup$
    – Sosa
    Feb 24 at 18:15
  • $\begingroup$ @Sosa {{sol}, {pts}} = NDSolve[{a'[t] == (0.16 - 0.0622 Sqrt[a[t]])/(Pi (a[t])^2), a[0] == 0.5, WhenEvent[a[t] == 2.3, {Sow[t]}]}, a, {t, 0, 180}] // Reap Plot[a[t] /. First@sol // Evaluate, {t, 0, 180}, Epilog -> {PointSize[Large], Red, Point[({#, a[#]} /. sol) & /@ pts]}] $\endgroup$
    – cvgmt
    Feb 24 at 22:58
0
$\begingroup$

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]
$\endgroup$
1
  • $\begingroup$ Thanks for the tip, I would take that in count $\endgroup$
    – Sosa
    Feb 24 at 19:02

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.