Mathematica calling module from NDSolve

  • Context: Mathematica 
  • Thread starter Thread starter musicgirl
  • Start date Start date
  • Tags Tags
    Mathematica module
Click For Summary
SUMMARY

This discussion focuses on the challenges of using a module within Mathematica's NDSolve function to calculate parameters dynamically. The user encounters the error "NDSolve::ndnum: Encountered non-numerical value for a derivative at s == 0" due to the variable Tau being defined as a function of v1 and v2 instead of a constant. The solution involves ensuring that all parameters passed to NDSolve are fixed numeric values, thereby avoiding non-numerical errors during computation.

PREREQUISITES
  • Familiarity with Mathematica programming language
  • Understanding of NDSolve function in Mathematica
  • Knowledge of modules and scoping in Mathematica
  • Basic concepts of differential equations
NEXT STEPS
  • Review Mathematica's documentation on NDSolve and its requirements
  • Learn about passing parameters to functions in Mathematica modules
  • Explore techniques for debugging NDSolve errors in Mathematica
  • Investigate the use of constant parameters in dynamic simulations
USEFUL FOR

Mathematica users, computational scientists, and anyone working with dynamic systems modeling and differential equations in Mathematica.

musicgirl
Messages
12
Reaction score
0
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?

Thanks for your help

---

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}];
 
Physics news on Phys.org
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
10K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
5K