# Using NDSolve in an Iteration

Posted 9 years ago
3930 Views
|
|
0 Total Likes
|
 Hey Folks,I have a question regarding the usage of NDSolve/ParametricNDSolve in an iteration. Probably I just struggle with the right formating of the code, but here is a simple idea of what I want to do:In the first step, I want to solve a differential equation N'[t] with NDSolve which looks like this: (1)d/dt N[t] = a* I[t]^6(this is no problem at all and easily feasible) but then I want to use the result in the next step to modify I[t] and use it to solve the differential equation again: (2)d/dt N[t] = a* (I[t] - b*N[t] (previous result) )^6.and so on...So my question is how to use it in an as simple as possible iteration without recursion to take place.Maybe you guys could give me a simple example of how to properly use NDSolve in an iteration.Thanks in advance!Thomas
 Hello, I know you asked for no recursion. However, this problem lends itself perfectly for a recursive step, that I have put it in anyway.... ellFunc[t_] := t Exp[-t] (*example for your l[t]*) Define the first RHS of your ODE: rhs[0][a_, b_, ell_] = a ell[t]^6 Compute the the base numerical solution to your ODE (I've arbitrarily integrated for 0 < t < 10 and set l[0]=0) sol[i] is the solution at the ith iteration; a and b are from your definition, ell is the choice for l[t] sol[0][a_?NumericQ, b_?NumericQ, ell_] := sol[0][a, b, ell] = enn /. NDSolve[{D[enn[t], t] == rhs[0][a, b, ell], enn[0] == 1}, enn, {t, 0, 10}][[1]] example: sol[0][1, 2, ellFunc] Plot[sol[0][1, 2, ellFunc][t], {t, 0, 10}] The RHS for the ith iteration, need the solution from the previous iteration. rhs[i_][a_?NumericQ, b_?NumericQ, ell_] := rhs[i][a, b, ell] = a (ell[t] - b enn[t]) sol[i - 1][a, b, ell][t] The solution at the ith iteration uses the RHS for that iteration: sol[i_][a_?NumericQ, b_?NumericQ, ell_] := sol[i][a, b, ell] = enn /. NDSolve[{D[enn[t], t] == rhs[i - 1][a, b, ell], enn[0] == 1}, enn, {t, 0, 10}][[1]] Example: Plot[Evaluate[sol[1][1, 2, ellFunc][t]], {t, 0, 10}] A list of solutions: solutions = Table[sol[i][1, 2, ellFunc][t], {i, 0, 10}] and plotting them: Plot[solutions, {t, 0, 10}]