Mathematica calling module from NDSolve

In summary, the conversation discusses the use of a module to repeatedly calculate a parameter in NDSolve. The error "NDSolve::ndnum" is encountered and it seems to be caused by the way the value of v1 and v2 are sampled in the main NDSolve loop. The solution to this issue may involve passing Real values to the connection constant.
  • #1
musicgirl
12
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
  • #2
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.
 

1. How can I call a module from NDSolve in Mathematica?

In order to call a module from NDSolve in Mathematica, you can use the "WhenEvent" function. This function allows you to specify a condition for when the module should be called.

2. Can I pass arguments to the module when calling it from NDSolve?

Yes, you can pass arguments to the module when calling it from NDSolve. You can do this by using the "EventAction" option in the "WhenEvent" function. This allows you to specify the arguments for the module.

3. How do I ensure that the module is only called once during the NDSolve process?

You can use the "Once" option in the "WhenEvent" function to ensure that the module is only called once during the NDSolve process. This option ensures that the event is only triggered the first time the condition is met.

4. Can I call multiple modules from NDSolve in Mathematica?

Yes, you can call multiple modules from NDSolve in Mathematica. You can do this by using multiple "WhenEvent" functions with different conditions and event actions.

5. Is it possible to call a module from NDSolve at a specific time or time interval?

Yes, you can call a module from NDSolve at a specific time or time interval by specifying the time in the condition for the "WhenEvent" function. You can also use the "TimeConstraint" option to specify a time interval for when the module should be called.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
6K
  • Differential Equations
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
25
Views
4K
  • Programming and Computer Science
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
6K
  • Special and General Relativity
Replies
29
Views
1K
  • Advanced Physics Homework Help
Replies
2
Views
1K
Back
Top