# Coupled nonlinear differential system with discontinuous functions which values are given by list elements, boundary conditions, NDSolve

I am very new to mathematica, I have googled my problem in vain, hope you can help me. I am currently trying to solve a non linear system of two coupled differential equations given by (41) and (42). All variables depend on k (alpha, theta, xhi, phi, as well as the matrix elements A11, A22, A12, and A21). Eta is a fixed arbitrary parameter.

I have started by calculating all matrix elements for k in a certain range (say k0 to k1) with step s=(k1-k0)/N. so that I have N 2x2 matrices A[k0], A[k0+s]...A[k1].

My problem is that when I define my system of equation on mathematica as well as the following boundary conditions on alpha (alpha[k0]==pi/8 ; alpha[k1]==pi/8 ) (and none on theta because I do not care about it for the moment : it's a free parameter), I always get an error message which is basically related to the fact that when K varies in the NDSOLVE problem, the matrix elements are not retrieved, instead of getting something like A12[k] I get something like A12[\$NDSOLVE1245845] which is not a valid part specification for a list.

I have tried summarizing my problem the way I understand it, but here is the code if you wanna try it yourself.

I have copied my mathematica notebook here, I give you the A matrices in the beginning, it's not very good looking so I can send the notebook directly if someone wants it.

(*Here is a list of lists called aMat, each element \
aMat[[k]] corresponds to one of the matrices 2x2 A(k) matrix in the \
equations, the process leading to aMat is kinda long so I do not post \
it here!  *)

Nsite = 51;
d = 532*10^-9;

aMat = {{{8.033808200646101*^-21 +
1.0195947261614721*^-7 I, -2.259193402732543*^-14 +
5.6606425073577124*^-8 I}, {-5.579587617378922*^-14 -
1.3979370715909291*^-7 I, -2.6939288474872005*^-18 -
1.1360855346370705*^-7 I}}, {{4.920008417679118*^-21 +
2.864988207444914*^-7 I,
6.448591285746703*^-14 +
1.615615344995959*^-7 I}, {-2.626485508352081*^-19 -
6.4559205290202805*^-9 I,
4.02818142522391*^-15 +
1.0091758074619052*^-8 I}}, {{9.701612357018*^-21 +
2.8808758498493*^-7 I,
4.941191381151055*^-18 +
1.5994433204297324*^-7 I}, {-2.612213198258168*^-19 -
1.2747464222971996*^-8 I,
7.580144893918093*^-19 +
1.9940869401651616*^-8 I}}, {{1.4215083632434476*^-20 +
2.9067525276121966*^-7 I,
2.769500855905615*^-18 +
1.5731234585782317*^-7 I}, {-2.588987808569906*^-19 -
1.871924893424333*^-8 I,
6.580740142263754*^-19 +
2.9316926956117847*^-8 I}}, {{1.8346598707244687*^-20 +
2.94176405812251*^-7 I,
1.9604382654454056*^-18 +
1.5375525696154838*^-7 I}, {-2.5576628331274985*^-19 -
2.423282541976617*^-8 I,
6.265767976797439*^-19 +
3.8012641902104256*^-8 I}}, {{2.200384369841378*^-20 +
2.984806514429946*^-7 I,
1.523813781806201*^-18 +
1.493887717289512*^-7 I}, {-2.5193244339608564*^-19 -
2.9172715176654585*^-8 I,
6.095502396476031*^-19 +
4.585226440947448*^-8 I}}, {{2.5119152956265407*^-20 +
3.0346017235825585*^-7 I,
1.247497168319565*^-18 +
1.443465362831549*^-7 I}, {-2.4753336539568484*^-19 -
3.345006505614873*^-8 I,
5.9721585438584*^-19 +
5.269690247822571*^-8 I}}, {{2.7650213281433854*^-20 +
3.0897752252144107*^-7 I,
1.0566155606811343*^-18 +
1.3877179229760157*^-7 I}, {-2.426853865088762*^-19 -
3.700384427991734*^-8 I,
5.867343436946562*^-19 +
5.844660952900648*^-8 I}}, {{2.9578655420869275*^-20 +
3.148928398202045*^-7 I,
9.172964717301432*^-19 +
1.328096678093049*^-7 I}, {-2.375378134195755*^-19 -
3.979986495755983*^-8 I,
5.770644027762974*^-19 +
6.303956594952819*^-8 I}}, {{3.090708773872836*^-20 +
3.210698904499792*^-7 I,
8.121591406248909*^-19 +
1.2660073581881407*^-7 I}, {-2.32199340437251*^-19 -
4.182814287649467*^-8 I,
5.678643893447754*^-19 +
6.644898914789824*^-8 I}}, {{3.165520994795495*^-20 +
3.2738064259815684*^-7 I,
7.311146538100815*^-19 +
1.2027616963291322*^-7 I}, {-2.268068980986546*^-19 -
4.3099210077193304*^-8 I,
5.59006120501795*^-19 +
6.867854226800027*^-8 I}}, {{3.185560132672103*^-20 +
3.3370832173128694*^-7 I,
6.681804660377018*^-19 +
1.1395454856529243*^-7 I}, {-2.2145281297650666*^-19 -
4.363996434159545*^-8 I,
5.505167839307502*^-19 +
6.975699420756697*^-8 I}}, {{3.1549654442220284*^-20 +
3.399490870570553*^-7 I,
6.194946126444731*^-19 +
1.0774016379153097*^-7 I}, {-2.162334268759257*^-19 -
4.348953406727124*^-8 I,
5.424303799691056*^-19 +
6.973276185751127*^-8 I}}, {{3.072979637965408*^-20 +
3.460125773063581*^-7 I,
5.826703023850006*^-19 +
1.0172255486431532*^-7 I}, {-2.1121053091407048*^-19 -
4.2695495500116656*^-8 I,
5.348145787220257*^-19 +
6.866879461775877*^-8 I}}, {{2.9583840885090626*^-20 +
3.51821613085182*^-7 I,
5.562736834605877*^-19 +
9.597696338490143*^-8 I}, {-2.0646443809917233*^-19 -
4.131063934935675*^-8 I,
5.277372434606911*^-19 +
6.663808654963002*^-8 I}}, {{2.8044775908692463*^-20 +
3.5731133206229025*^-7 I,
5.397324217219753*^-19 +
9.056540111771756*^-8 I}, {-2.0200689167033389*^-19 -
3.939036686209166*^-8 I,
5.212818970912487*^-19 +
6.371995170785319*^-8 I}}, {{2.6247451780140498*^-20 +
3.6242799318184524*^-7 I,
5.331247036562746*^-19 +
8.553807289819507*^-8 I}, {-1.9791340461962209*^-19 -
3.699071121991565*^-8 I,
5.15548745722521*^-19 +
5.999708615932234*^-8 I}}, {{2.4090528817032664*^-20 +
3.6712763421650144*^-7 I,
5.375201785758898*^-19 +
8.093495114976781*^-8 I}, {-1.9417830755396824*^-19 -
3.4166927276595516*^-8 I,
5.106517303262318*^-19 +
5.5553366763549385*^-8 I}}, {{2.1736332854480823*^-20 +
3.7137471524357817*^-7 I,
5.552884482597155*^-19 +
7.678735560534001*^-8 I}, {-1.9083153269163605*^-19 -
3.097256626088577*^-8 I,
5.067908381052614*^-19 +
5.047229628922304*^-8 I}}, {{1.918972443974762*^-20 +
3.751408356668874*^-7 I,
5.908999847273977*^-19 +
7.311944143641007*^-8 I}, {-1.8789836409047963*^-19 -
2.745894444955685*^-8 I,
5.042625743532527*^-19 +
4.483598845297464*^-8 I}}, {{1.6484488934489734*^-20 +
3.784035769353109*^-7 I,
6.531992545271056*^-19 +
6.994953833221866*^-8 I}, {-1.8537189059701642*^-19 -
2.3674918938353474*^-8 I,
5.036732002915816*^-19 +
3.872458668850092*^-8 I}}, {{1.3651087345306606*^-20 +
3.81145497297622*^-7 I,
7.607243135375442*^-19 +
6.72913118158307*^-8 I}, {-1.8326259355206506*^-19 -
1.9666894588395473*^-8 I,
5.063833558236244*^-19 +
3.221602008658648*^-8 I}}, {{1.071702867170963*^-20 +
3.833532875741768*^-7 I,
9.597905062862139*^-19 +
6.515473744661214*^-8 I}, {-1.8157473867785008*^-19 -
1.5478999238071165*^-8 I,
5.162604364852125*^-19 +
2.538601387320923*^-8 I}}, {{7.70733053769387*^-21 +
3.85017086373458*^-7 I,
1.4104236563210386*^-18 +
6.35468903630958*^-8 I}, {-1.803060954271949*^-19 -
1.1153377614241531*^-8 I,
5.487471866782634*^-19 +
1.8308286752025182*^-8 I}}, {{4.6450348089588645*^-21 +
3.861299476885155*^-7 I,
3.45777828374654*^-18 +
6.247255872771278*^-8 I}, {-1.7946103885000363*^-19 -
6.730566203934185*^-9 I,
7.543375479191052*^-19 +
1.1054881348791118*^-8 I}}, {{1.5517555266936961*^-21 +
3.866874519217884*^-7 I,
1.894536140693539*^-18 -
6.193469167651351*^-8 I}, {-1.7903838116380825*^-19 -
2.249921164821364*^-9 I, -2.9423700996263846*^-20 -
3.696585844110595*^-9 I}}, {{-1.5637272016006697*^-21 +
3.866874519218525*^-7 I, -1.8945280793554763*^-18 -
6.193469167662479*^-8 I}, {1.790388518851441*^-19 -
2.2499211668119864*^-9 I,
2.942484092397458*^-20 -
3.6965858435714946*^-9 I}}, {{-4.742174432805856*^-21 +
3.861299476885976*^-7 I, -3.457798716561031*^-18 +
6.247255872778867*^-8 I}, {1.794622002239326*^-19 -
6.730566202745649*^-9 I, -7.543402900929096*^-19 +
1.105488134876665*^-8 I}}, {{-7.804190196500871*^-21 +
3.850170863734963*^-7 I, -1.41042949354795*^-18 +
6.354689036299113*^-8 I}, {1.8030775562086866*^-19 -
1.1153377613396663*^-8 I, -5.487469877992032*^-19 +
1.830828675237706*^-8 I}}, {{-1.072217587355892*^-20 +
3.833532875741192*^-7 I, -9.597948862826747*^-19 +
6.515473744655095*^-8 I}, {1.815736973880872*^-19 -
1.547899923794939*^-8 I, -5.162600944513975*^-19 +
2.5386013873208913*^-8 I}}, {{-1.355788229112468*^-20 +
3.811454972975411*^-7 I, -7.60714470754315*^-19 +
6.729131181592481*^-8 I}, {1.8326086245605312*^-19 -
1.9666894589467598*^-8 I, -5.063836859354622*^-19 +
3.221602008635054*^-8 I}}, {{-1.6414597680533996*^-20 +
3.7840357693517435*^-7 I, -6.531898766876434*^-19 +
6.99495383323809*^-8 I}, {1.8537175483580718*^-19 -
2.367491893831529*^-8 I, -5.036749390236897*^-19 +
3.8724586688078445*^-8 I}}, {{-1.921725421279006*^-20 +
3.751408356667764*^-7 I, -5.909171207125913*^-19 +
7.31194414365596*^-8 I}, {1.878974717179978*^-19 -
2.7458944450273424*^-8 I, -5.04268334534577*^-19 +
4.483598845268152*^-8 I}}, {{-2.1777458316409858*^-20 +
3.7137471524352475*^-7 I, -5.552957621470693*^-19 +
7.678735560547088*^-8 I}, {1.9083440519447586*^-19 -
3.0972566261077335*^-8 I, -5.067947372672451*^-19 +
5.0472296289018394*^-8 I}}, {{-2.4168302854081965*^-20 +
3.6712763421628677*^-7 I, -5.375185227215908*^-19 +
8.093495114988769*^-8 I}, {1.9418863411275705*^-19 -
3.4166927275981066*^-8 I, -5.106556154321534*^-19 +
5.5553366763138164*^-8 I}}, {{-2.632817352120827*^-20 +
3.624279931815362*^-7 I, -5.331568308207647*^-19 +
8.55380728985272*^-8 I}, {1.9791371594348603*^-19 -
3.6990711221359786*^-8 I, -5.155584220640963*^-19 +
5.999708615869976*^-8 I}}, {{-2.818109962336356*^-20 +
3.5731133206196647*^-7 I, -5.397246617010634*^-19 +
9.05654011181913*^-8 I}, {2.0202438086576516*^-19 -
3.939036686264893*^-8 I, -5.21280417895509*^-19 +
6.3719951707024*^-8 I}}, {{-2.9586895515665964*^-20 +
3.5182161308500464*^-7 I, -5.562991385562087*^-19 +
9.597696338517826*^-8 I}, {2.0645434923365562*^-19 -
4.131063934951449*^-8 I, -5.277380643708293*^-19 +
6.663808654914656*^-8 I}}, {{-3.0676690533857427*^-20 +
3.4601257730656186*^-7 I, -5.826255321520116*^-19 +
1.0172255486428117*^-7 I}, {2.1121226387071561*^-19 -
4.2695495500631116*^-8 I, -5.347976825781128*^-19 +
6.866879461805875*^-8 I}}, {{-3.1413633840733296*^-20 +
3.399490870573189*^-7 I, -6.194822088499933*^-19 +
1.0774016379118343*^-7 I}, {2.1621455849783666*^-19 -
4.348953406570069*^-8 I, -5.424199991009962*^-19 +
6.973276185810851*^-8 I}}, {{-3.176580420118423*^-20 +
3.337083217314582*^-7 I, -6.6816170131315625*^-19 +
1.1395454856494644*^-7 I}, {2.2144503793694587*^-19 -
4.363996434169416*^-8 I, -5.505116398374329*^-19 +
6.975699420819069*^-8 I}}, {{-3.158091513129204*^-20 +
3.273806425978735*^-7 I, -7.311116952049515*^-19 +
1.20276169633033*^-7 I}, {2.268030718650996*^-19 -
4.30992100768835*^-8 I, -5.590139197241613*^-19 +
6.867854226744586*^-8 I}}, {{-3.084553118990792*^-20 +
3.2106989044961364*^-7 I, -8.121317943872537*^-19 +
1.2660073581925464*^-7 I}, {2.3219598122734736*^-19 -
4.182814287779874*^-8 I, -5.678574394024991*^-19 +
6.64489891468643*^-8 I}}, {{-2.9586960811889886*^-20 +
3.148928398201589*^-7 I, -9.172923860730165*^-19 +
1.3280966780965467*^-7 I}, {2.375323980492081*^-19 -
3.9799864958056516*^-8 I, -5.770522232961653*^-19 +
6.30395659489623*^-8 I}}, {{-2.773639454812924*^-20 +
3.089775225213917*^-7 I, -1.0565548046853918*^-18 +
1.3877179229758124*^-7 I}, {2.4268947069842577*^-19 -
3.70038442782392*^-8 I, -5.866989037626828*^-19 +
5.844660952881337*^-8 I}}, {{-2.5219765064266593*^-20 +
3.0346017235835437*^-7 I, -1.2475239204315625*^-18 +
1.4434653628306901*^-7 I}, {2.475259145231046*^-19 -
3.345006505766175*^-8 I, -5.97196411162458*^-19 +
5.269690247871184*^-8 I}}, {{-2.201120865499778*^-20 +
2.984806514432385*^-7 I, -1.523749426424153*^-18 +
1.49388771728884*^-7 I}, {2.5193381479359126*^-19 -
2.9172715176301558*^-8 I, -6.095315110032011*^-19 +
4.585226441016985*^-8 I}}, {{-1.8296693643235808*^-20 +
2.9417640581247653*^-7 I, -1.960411302002763*^-18 +
1.537552569612602*^-7 I}, {2.5576456729892*^-19 -
2.4232825419138733*^-8 I, -6.265738883668753*^-19 +
3.80126419035277*^-8 I}}, {{-1.4165772515444426*^-20 +
2.906752527612242*^-7 I, -2.7694897162441683*^-18 +
1.5731234585759723*^-7 I}, {2.5889508004545654*^-19 -
1.871924893406248*^-8 I, -6.580711156897425*^-19 +
2.9316926956975514*^-8 I}}, {{-9.677807805559877*^-21 +
2.8808758498495014*^-7 I, -4.9411868928239335*^-18 +
1.5994433204294002*^-7 I}, {2.612202665348913*^-19 -
1.2747464224024206*^-8 I, -7.580139298539908*^-19 +
1.9940869402047568*^-8 I}}, {{-4.89633514500996*^-21 +
2.8649882074453543*^-7 I, -6.44858048312444*^-14 +
1.615615344996214*^-7 I}, {2.626485507944989*^-19 -
6.455920528462238*^-9 I, -4.0281746783825426*^-15 +
1.0091758074771626*^-8 I}}, {{-8.02538333405992*^-21 +
1.0195947261628438*^-7 I,
2.2591971871808648*^-14 +
5.660642507341513*^-8 I}, {5.5795782702384586*^-14 -
1.3979370715915536*^-7 I,
2.5420215571980736*^-18 - 1.136085534636542*^-7 I}}};

aMat[[52]](*2x2 matrix*)

Out[24]= {{-8.02538*10^-21 + 1.01959*10^-7 I,
2.2592*10^-14 + 5.66064*10^-8 I}, {5.57958*10^-14 - 1.39794*10^-7 I,
2.54202*10^-18 - 1.13609*10^-7 I}}

aMat[[1]](*2x2 matrix*)

Out[5]= {{8.03381*10^-21 + 1.01959*10^-7 I, -2.25919*10^-14 +
5.66064*10^-8 I}, {-5.57959*10^-14 -
1.39794*10^-7 I, -2.69393*10^-18 - 1.13609*10^-7 I}}

(*I define my equations here, notice that I call \
aMat[[Round[((d*k/\[Pi]+1)*Nsite)/2+1]], that is because in NDSolve k \
varies between -\[Pi]/d to \[Pi]/d, this function gives the \
corresponding matrix element, for example \[Pi]/d will give 52, \
already here the definitionf of eqn1 and eqn2 gives an error !!!*)

eqn1 =
a'[k]/2 == -(Cos[2 t[k]]/
Sin[t[k]])*(Re[
aMat[[Round[((d*k/\[Pi] + 1)*Nsite)/2 + 1]]][[1, 2]]] Cos[
aMat[[Round[((d*k/\[Pi] + 1)*Nsite)/2 + 1]]][[1, 2]]] Sin[
1/(Tan[a[k]/2] Tan[
t[k]])*(Re[
aMat[[Round[((d*k/\[Pi] + 1)*Nsite)/2 + 1]]][[1, 2]]] Sin[
aMat[[Round[((d*k/\[Pi] + 1)*Nsite)/2 + 1]]][[1, 2]]] Cos[
Cos[t[k]] (aMat[[Round[((d*k/\[Pi] + 1)*Nsite)/2 + 1]]][[1, 1]] -
aMat[[Round[((d*k/\[Pi] + 1)*Nsite)/2 + 1]]][[2, 2]]);

eqn2 =
t'[k]/2 == (Cos[t[k]] Sin[a[k]])/
Sin[a[k]/
2]^2*(Re[
aMat[[Round[((d*k/\[Pi] + 1)*Nsite)/2 + 1]]][[1, 2]]] Cos[
aMat[[Round[((d*k/\[Pi] + 1)*Nsite)/2 + 1]]][[1, 2]]] Sin[
Cos[a[k]]/
Sin[a[k]/
2]^2*(Re[
aMat[[Round[((d*k/\[Pi] + 1)*Nsite)/2 + 1]]][[1, 2]]] Sin[
aMat[[Round[((d*k/\[Pi] + 1)*Nsite)/2 + 1]]][[1, 2]]] Cos[
1/Tan[a[k]/2] Sin[
t[k]]*(aMat[[Round[((d*k/\[Pi] + 1)*Nsite)/2 + 1]]][[1, 1]] -
aMat[[Round[((d*k/\[Pi] + 1)*Nsite)/2 + 1]]][[2, 2]]);

(*boundary conditions*)

eqn = {eqn1, eqn2};

bcs = {a[-\[Pi]/d] == \[Pi]/8, a[\[Pi]/d] == \[Pi]/8};

system = Join[eqn, bcs];
sol = NDSolve[system, a, {k, -\[Pi]/d, \[Pi]/d}];

And the error log (the first message up to the Part::partw: Part 2 of Round[1+51/2 (1+(133 k)/(250000000 [Pi]))] does not exist message are due to the definition of eqn1 and eqn2, basically k is not a number ... the last message comes from NDSOLVE):

Part::pkspec1: The expression Round[1+51/2 (1+(133 k)/(250000000 \[Pi]))] cannot be used as a part specification.

Part::pkspec1: The expression Round[1+51/2 (1+(133 k)/(250000000 \[Pi]))] cannot be used as a part specification.

Part::pkspec1: The expression Round[1+51/2 (1+(133 k)/(250000000 \[Pi]))] cannot be used as a part specification.

General::stop: Further output of Part::pkspec1 will be suppressed during this calculation.

Part::partw: Part 2 of Round[1+51/2 (1+(133 k)/(250000000 \[Pi]))] does not exist.

Part::pkspec1: The expression Round[1+51/2 (1+(133 k)/(250000000 \[Pi]))] cannot be used as a part specification.

Part::pkspec1: The expression Round[1+51/2 (1+(133 k)/(250000000 \[Pi]))] cannot be used as a part specification.

Part::pkspec1: The expression Round[1+51/2 (1+(133 k)/(250000000 \[Pi]))] cannot be used as a part specification.

General::stop: Further output of Part::pkspec1 will be suppressed during this calculation.

Part::partw: Part 2 of Round[1+51/2 (1+(133 k)/(250000000 \[Pi]))] does not exist.

NDSolve::bvdisc: NDSolve is not currently able to solve boundary value problems with discrete variables.

You will notice that this is not the error message I have complained about, this i because I have stripped the problem of all superfluous parameters, this is the base error message, it seems related to the fact that the A function is discontinuous; I dont see why it should be a problem especially since these kind of algorithms are based on discretization methods (?).

As a precision, in my problem k is in fact discrete it goes from -pi/d to pi/d with step 2pi/(N*d), where d is a certain distance, and N an arbitrary number....

I am kind of fed up with this, I thought it would be straight forward, I thank you for any help you can provide. Since I am new to mathematica and equation solving, I would also appreciate pointing out bad practices (except for the aMat definition of course), and pointers on the Method options of NDSOLVE(I tried a few kind of randomly). Thank you very much, of course if you can solve the equations in any other way please do not hesitate to share your solution !

• "All variables depend on k (alpha, theta, xhi, phi, as well as the matrix elements A11, A22, A12, and A21). ……I have started by calculating all matrix elements for k in a certain range (say k0 to k1) with step s=(k1-k0)/N. so that I have N 2x2 matrices A[k0], A[k0+s]...A[k1]…" You should not. Directly use the symbolic 2*2 A matrix instead should resolve the problem. If you cannot (e.g. because it's from complicated calculation) at least build an InterpolatingFunction. Apr 8 '20 at 2:38
• the A matrices indeed come from a complicated calculation that starts with finding the eingenvalues of a certain hamiltonian... I'll try the interpolatingFunction idea, thanks for your help
– yfs
Apr 8 '20 at 13:03