I’m in my last year of my physics degree, and part of my dissertation project involves mathematica, modelling the orbital behaviour of a spacecraft using Ion propulsion.
I have 2 coupled differential equations being solved, r’‘(t) and o’'(t), in an NdSolve function (o is pheta in my case, modelling the change in angular momentum). It solves this with no problem, and plots the results fine. I now need to find the value of t and o at which r=5.3. It seems simple enough on paper, but i’m not sure how to translate that into my code.
Attached is a simplified version of my code. Any help would be greatly appreciated.
You are looking for a value of t between -10 and 10 such that:
First[r[t] /. TotalThrust] == 5.3
The “First” is there to get rid of the list. See the previously linked article on how to use the output of NDSolve for more info.
Usually you can take equations like the above and use NSolve or FindRoot on them to find a value of t that solves them.
But, It doesn’t look like that value exists according to your graph! It does look like r[t] goes up to 5.3. NDSolve is set in your code so that it runs from t=-10 to t=10. If I increase that boundary I still don’t see r[t] going anywhere near 5.3.
Here’s what it looks like when I run NDSolve from -20 to 20 and plot it:
Sorry about the disagreement with the graph, in my currently used model, different initial conditions are set such that r does eventually reach the desired value.
What is the syntax of the Nsolve and Findroot when solving a specific equation like First[r[t] /. TotalThrust] == 5.3?
I’ve attempted with NSolve [First[r[t] /. TotalThrust] == 5.3, t] and it returns a value of 16.22 which is wrong when looking at my graph, and returns the error : Inverse functions are being used by NSolve, so some solutions may not be found; use Reduce for complete solution information. >>
Trying that, it returns the error FindRoot::nlnum: The function value {-5.3+<<1>>[t]} is not a list of numbers with dimensions {1} at {x} = {7.}. >>, i looked up the syntax and help documents but i couldn’t get it to give me anything except that error. I assume its to do with the First function as it is treating it like a list?
It would be easier to respond if you would post complete code or a new notebook. However, if you are not interested in the values for r after it first passes a limit, you could add WhenEvent to the NDSolve equations. In the code below I used your intial notebook and set rLimit to 2.5. WhenEvent stops integration when r hits 2.5, and records the time in tStop. So r is then r[tStop]. (Note that, for some reason, tStop needs to be defined prior to executing NDSolve or the value will not be saved.)
That works really well, but i prefer how dynamic the FindRoot method is, as i will be looking into the values after the limit. Any idea’s about the error it returned when i tried to use it?
Regarding my earlier comment, I think tStop, or in this case tList, needs to be defined before its use in WhenEvent because that provides it with global scope. Otherwise it has scope local to NDSolve and expires when NDSolve finishes.
This actually works fantastically! how do i manipulate the code to instead give me the value of o at our given rlimit?
This was my attempt, and it failed horribly!
In the code I posted, NDSolveValue creates interpolating functions for both o and r. Actually, because I asked it for o[t] and r[t], rather than o and r, the argument t is included in the output. If you execute the NDSolveValue without the semicolon you will see two interpolating functions, each expressed as a function of t. They are assigned to the variables fo and fr.
The first time at which rLimit is crossed is tList[[1,1]], so the value of o at that time is just fo/.t->tList[[1,1]] (Look up Rule and Replace.)
In[10]:= fo /. t -> tList[[1, 1]]
Out[10]= 20.0634
You can of course plot fo[t] just the way I plotted fr[t]. You could even put them both in the same plot.
Thanks David, that worked perfectly! My final question is now that i have our value for t* at r=5.2, how do i find o’(t*) (the first derivative of o at our value of t)?