# using Module in NDSolve

I am trying to solve a set of two wave equations where solution of first equation defines initial conditions for the second one. And I would like to use Module to define those equations as a function so that I then can loop it through NestList. But I am having difficulties to write a code for the Module. Can someone educate me? In simplified version here is what I am trying to achieve:

eq1=I D[u[t,x],{x,1}]+0.5D[u[t,x],{t,2}]-I u[t,x]==0;
eq2=I D[u[t,x],{x,1}]+0.5D[u[t,x],{t,2}]+I u[t,x]==0;
f0[t_]:=Exp[-t^2];t0=20;
sol1[f_]:=NDSolve[{eq1,u[t,0]==f[t],u[+t0,x]==f0[t0],u[-t0,x]==f0[-t0]},u[t,x],{t,-t0,t0},{x,0,1},AccuracyGoal->1];
in1[f_]:=u[t,x]/.First@sol1[f]/.x->1;
in11[t]=in1[f0]

sol2[f_]:=NDSolve[{eq2,u[t,0]==f[t],u[+t0,x]==f0[t0],u[-t0,x]==f0[-t0]},u[t,x],{t,-t0,t0},{x,0,1}, AccuracyGoal->1];
in2[f_]:=u[t,x]/.First@sol2[f]/.x->1;
in21[t]=in2[in11]


When I run this code as is it gives two solutions (as expected) Now I want to create a function which can describe this set of equations. Here is what I came up with

h[z_]:=Module[{in1}, in1[f_]:=u[t,x]/.First@sol1[f]/.x->1;in2[in1[z]]]


and this does not work returning error message "Encountered non-numerical value for a derivative at x==0". Can someone help me to solve this problem? Thanks

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Michael E2 Nov 13 '14 at 2:21

There are several little issues that conspire to trip up your code here and there. Let me just point out a couple of the major things to think about.

First, keep clearly in mind the difference between a function f and the expression f[t]. I know from teaching mathematics that these tend to get conflated in ordinary conversation, but you cannot get away with that in computer programming.

Second, in my opinion, the function u instead of the expression u[t, x] is a better form to work with. I mean that the call to NDSolve should be like

NDSolve[eqns, u, {t, -t0, t0}, {x, 0, 1},...]


In some narrow situations, the form u[t, x] might be better, such as if you only want to plot it.

eq1 = I D[u[t, x], {x, 1}] + 0.5 D[u[t, x], {t, 2}] - I u[t, x] == 0;
eq2 = I D[u[t, x], {x, 1}] + 0.5 D[u[t, x], {t, 2}] + I u[t, x] == 0;
f0[t_] := Exp[-t^2]; t0 = 20;

sol1[f_] :=
NDSolve[{eq1, u[t, 0] == f[t], u[+t0, x] == f0[t0],
u[-t0, x] == f0[-t0]}, u, {t, -t0, t0}, {x, 0, 1},
AccuracyGoal -> 1];
in1[f_] := u[#, 1] & /. First@sol1[f];
in11 = in1[f0]

sol2[f_] :=
NDSolve[{eq2, u[t, 0] == f[t], u[+t0, x] == f0[t0],
u[-t0, x] == f0[-t0]}, u, {t, -t0, t0}, {x, 0, 1},
AccuracyGoal -> 1];
in2[f_] := u[#, 1] & /. First@sol2[f];
in21 = in2[in11]

h[z_] := Module[{in1}, in1[f_] := u[#, 1] & /. First@sol1[f]; in2[in1[z]]]


The following return solutions:

h[Exp[-#^2] &]
h[Exp[-#^2] Cos[Pi #/10] &]


(I have no idea what the input to h should be.)

• Thanks for pointing me in right direction. In this context I am wondering if there are other ways (more elegant/efficient/etc) to approach the same problem ie solution of a number DE with initial conditions of one equation being a solution of the previous one. And then the whole thing can be put in a loop. It is a fairly common problem in laser physics and would be really useful to have an efficient code. Thanks again. – YDFA Nov 13 '14 at 10:55
• @YDFA You're welcome. In your example, eq1 and eq2 are the same; so sol1 and sol2 are the same function. If that's typical, then you don't need both functions but could just use sol1 in both cases. Then you could use something like Nest; perhaps NDSolveValue would be easier to work with than NDSolve in that case. If the equations are different, you could use Fold. Otherwise, your approach seems fairly straightforward. (I would worry about the accumulation of error in nesting approximate solutions to PDEs, but cross that bridge when you come to it.) – Michael E2 Nov 13 '14 at 14:12