Skip to main content

differential equations - ODE solving and NDSolveValue error depending on parameters


Given the two sets of 2N equations


Eu[n_, i_] := ((I*k)/(2*Pi))*Subscript[λu, i][t] - Sum[If[j != i, Coth[(Subscript[λu, j][t] - Subscript[λu, i][t])/2], 0], {j, 1, n}] + (1/2)*Sum[Tanh[(Subscript[λt, j][t] - Subscript[λu, i][t] - mt)/2] + Tanh[(Subscript[λt, j][t] -Subscript[λu, i][t] + mu)/2], {j, 1, n}]; 
Et[n_, i_] := (-((I*k)/(2*Pi)))*Subscript[λt, i][t] - Sum[If[j != i, Coth[(Subscript[λt, j][t] - Subscript[λt, i][t])/2], 0], {j, 1, n}] + (1/2)*Sum[Tanh[(Subscript[λu, j][t] - Subscript[λt, i][t] - mu)/2] + Tanh[(Subscript[λu, j][t] - Subscript[λt, i][t] + mt)/2], {j, 1, n}];

I need to solve the following system of ODE


Eqs[n_] := Flatten[Table[{τu*D[Subscript[λu, i][t], t] == Eu[n, i], τt*D[Subscript[λt, i][t], t] == Et[n, i]}, {i, n}]];


with the following initial values


ICs[n_] := Flatten[Table[{Subscript[λu, i][0] == 0.1*i, Subscript[λt, i][0] == 0.1*i}, {i, n}]];

The functions to determine are the following


Vars[n_] := Join[Table[Subscript[λu, i], {i, n}], Table[Subscript[λt, i], {i, n}]]; 

In particular I need to determine numerically late solution (i.e. solution for t enough big such that the Eu and Et value is small) of the initial value problem for some large value of N (the larger the better), say at least N200 for certain value of the other parameters k, τu, τt, mu and mt. So I used


n = 200; 
k = 1;

τu = 1;
τt = 1;
mu = 2.;
mt = -2.5;
sol = NDSolveValue[Join[Eqs[n], ICs[n]], Vars[n], {t, 0, 1000}];

What I get is the following message


NDSolveValue::ntdv: Cannot solve to find an explicit formula for the derivatives. Consider using the option Method->{"EquationSimplification"->"Residual"}.

However if I add the option as it suggest I get



NDSolveValue::mconly: For the method IDA, only machine real code is available. Unable to continue with complex values or beyond floating-point exceptions.

NDSolveValue::icfail: Unable to find initial conditions that satisfy the residual function within specified tolerances. Try giving initial conditions for both values and derivatives of the functions.

Notice that all works from the beginning if i let n=100 or so. The problem is that I need the result for larger values of N.


Can you suggest me something?



Answer



The underlying issue is already discussed in


What's behind Method -> {"EquationSimplification" -> "Residual"}


so I'd like not to talk too much about it in this answer. In short, NDSolve is having difficulty in recognizing the system is an ODE system and the DAE solver of NDSolve isn't strong enough (at least now) so we need to help NDSolve to choose an ODE solver. One possible solution is to use Experimental`NumericalFunction:



rhs[n_] := Flatten@Transpose@Table[{Eu[n, i]/τu, Et[n, i]/τt}, {i, n}];
vars[n_] := Table[{Subscript[λu, i], Subscript[λt, i]}, {i, n}] // Transpose // Flatten;
icvalues[n_] := Table[{0.1 i, 0.1 i}, {i, n}] // Transpose;
rhsnumeric =
Experimental`CreateNumericalFunction[vars[n][t] // Through,
rhs@n, {2 n}]; // AbsoluteTiming
(* {13.9278, Null} *)

sol =
NDSolveValue[{v'[t] == rhsnumeric@v@t, v[0] == Flatten@icvalues@n},

v, {t, 0, 1}]; // AbsoluteTiming
(* {138.54, Null} *)

The calculation inside NDSolve is slow so I choose 1 as end of time for illustration. If you have a C compiler installed then we can speed up the code a bit with a more advanced solution:


rhscompiled = 
Hold@Compile[{{utlst, _Complex, 2}},
Transpose@Table[{Eu[n, i]/τu, Et[n, i]/τt}, {i, n}],
RuntimeOptions -> "EvaluateSymbolically" -> False, CompilationTarget -> C] //.
Flatten@{DownValues /@ {Eu, Et},
OwnValues /@ Unevaluated@{n, k, τu, τt, mu, mt}} /.

{Subscript[λu, i_][t] -> Compile`GetElement[utlst, 1, i],
Subscript[λt, i_][t] -> Compile`GetElement[utlst, 2, i]} // ReleaseHold;

solcompiled = NDSolveValue[{v'[t] == rhscompiled@v@t, v[0] == icvalues@n},
v, {t, 0, 1}]; // AbsoluteTiming
(* {45.4734, Null} *)

Plot[solcompiled[t] // Abs, {t, 0, 1}]

enter image description here



Notice the structure of output of sol and solcompiled is a bit different.


Comments

Popular posts from this blog

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]