# Mathematica calling module from NDSolve

1. Jul 9, 2013

### musicgirl

Hi,

I want to use a module to repeatedly calculate a parameter in NDSolve, but I seem to be having trouble. Here is a simplified version of what I'm doing. In this example, I want to use the difference between v1 and v2 at a given timepoint to calculate the connection between v1 and v2 for the next timepoint.

I am getting the error "NDSolve::ndnum: Encountered non-numerical value for a derivative at s == 0.`." but as far as I can see, everything is defined in the module. If I replace Tau with a constant, I get no errors, which leads me to think it must be something to do with the way I am sampling the value of v1 and v2 from the main NDSolve loop?

---

connection[vdiff_, t_] :=
Module[{deltav = vdiff, Tau, con},

Tau = 120 + 7434.49/(1 + 0.00262985 (deltav[[1]])^2);

con = NDSolve[{g' == (1/Tau)*(3 - g), g[0] == 1},
g, {s, 0, T}];

g[t] /. con]

A = 3;
\[Epsilon] = 0.2;
\[Alpha] = 3;
\[Gamma] = 0.05;
v0 = 0.4;
w0 = 0.4;
T = 25;

initial =
Solve[wi == A*vi*(vi - \[Alpha])*(1 - vi) - w0 &&
wi == (vi - v0)/\[Gamma], {vi, wi}, Reals];

fitz = NDSolve[{v1'[
t] == ((A*v1[t]*(v1[t] - \[Alpha])*(1 - v1[t]) - w1[t] -
w0)/\[Epsilon])
+ (connection[v1[t] - v2[t], t]*(v2[t] - v1[t])),

v2'[t] == ((A*v2[t]*(v2[t] - \[Alpha])*(1 - v2[t]) - w2[t] -
w0)/\[Epsilon])
+ (connection[v2[t] - v1[t], t]*(v1[t] - v2[t])),

w1'[t] == v1[t] - \[Gamma]*w1[t] - v0,

w2'[t] == v2[t] - \[Gamma]*w2[t] - v0,

v1[0] == kick + vi /. initial,
v2[0] == vi /. initial,
w1[0] == wi /. initial,
w2[0] == wi /. initial},

{v1, v2, w1, w2}, {t, 0, T}];

2. Jul 9, 2013

### Bill Simpson

NDSolve expects everything, except the function you are solving for and the independent variable, to have been assigned a fixed numeric value.

When I insert
Print["Tau=", Tau];
just after your initialization of Tau in your connection[] module it prints
Tau=120+7434.49/(1+0.00262985 v1[t]^2)
and then your NDSolve is going to fail.

That is why your connection[] NDSolve is throwing an error and then your replacement is throwing an error and then your outer NDSolve is throwing an error.

See if you can find a way to pass your connection constant Real values and see what happens.