Iteration of NDSolve

I have a problem with iteration of the result of NDSolve. Namely, the following code works fine

a[x_] := Exp[-x^2]
uo[x_] := Exp[-2 x^2]
sol := Module[{x, t}, NDSolve[{D[u[x, t], t] == 1 – u[x, t] a[x], u[x, 0] == uo[x]},
u, {x, -10, 10}, {t, 0, 10}]]
d[x_, t_] := Evaluate[u[x, t] /. sol]
d[1, 1]
Out[58]= {0.942309}

However, when I try to use the function d(x,t), I get an error, namely:

newsol := Module[{y, s}, NDSolve[{D[v[y, s], s] == 1 – v[y, s] d[y, s], v[y, 0] == uo[y]},
v, {y, -10, 10}, {s, 0, 10}]]
Evaluate[v[1, 1] /. newsol]
Thread::tdlen: Objects of unequal length in {0} {1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.,1.} cannot be combined. >>

and so on…

=================

2

 

Hi ! You once asked a question here and I am sure you noticed the proper code formatting. Please, edit your post and format it properly. Just head to the help centre to read more about code formatting.
– Sektor
Dec 3 ’14 at 23:31

  

 

The differential equations described above could be solved analytically as well.
– bbgodfrey
Dec 4 ’14 at 2:59

=================

2 Answers
2

=================

The problem is, that NDSolve always returns a list of solutions, in this case a list of length 1.
You can see it here:

d[1, 1]
Out[58]= {0.942309}

If you know there is only one solution, you can use

d[x_, t_] := Evaluate[u[x, t] /. First@sol]

to make d[1,1] evaluate to 0.942309 instead of {0.942309}
.

  

 

Thank you very much!
– Dmitri
Dec 4 ’14 at 5:35

1

 

@Dmitri you are saying “thank you” 2nd time already for 2nd question you asked. You should start accepting the answers
– Vitaliy Kaurov
May 15 ’15 at 9:50

Although @DaveStrider has answered the question above fully, I think it worth noting that these equations can be solved analytically. For instance,

a = Exp[-x^2]; uo = Exp[-2 x^2];
d = u[t] /. DSolve[{u'[t] == 1 – a u[t], u[0] == uo}, u, t][[1]]

with solution

E^(-(t/E^x^2) – 2*x^2)*(1 – E^(3*x^2) + E^(t/E^x^2 + 3*x^2))

Interestingly, N[d /. {x -> 1, t -> 1}] evaluates to 0.930365, which differs slightly from the value obtained with NDSolve.

To continue, the second equation is solved by

f = v[t] /. DSolve[{v'[t] == 1 – v[t] d, v[0] == uo}, v, t][[1]]

which has a more complicated solution

-(E^(-E^(-x^2) + E^(-(t/E^x^2) – x^2) – E^(-(t/E^x^2) + 2*x^2) – E^x^2*t –
2*x^2)*(-E^E^(2*x^2) + E^(E^(-x^2) + 2*x^2)*
Integrate[E^(-E^(-x^2 – K[1]/E^x^2) + E^(2*x^2 – K[1]/E^x^2) + E^x^2*K[1]),
{K[1], 1, 0}] – E^(E^(-x^2) + 2*x^2)*
Integrate[E^(-E^(-x^2 – K[1]/E^x^2) + E^(2*x^2 – K[1]/E^x^2) + E^x^2*K[1]),
{K[1], 1, t}]))

Finally, N[f /. {x -> 1, t -> 1}] is 0.795889. Analytical solutions, when available, often provide more insight than do numerical solutions.

1

 

I understood this, surely. The idea is that I need to solve much more complicate nonlinear equation with convolutions which is considered by Mathematica as an equation with delay and hence non-solvable. I know that solution exists by a fixed point theorem and iterate. I got similar mistakes in that difficult situation and to ask a question I ‘cut’ the equation to a simple (now linear) form.
– Dmitri
Dec 4 ’14 at 5:38