Re: and Adding matrices for use in NDSolve input

*To*: mathgroup at smc.vnet.net*Subject*: [mg63401] Re: [mg63316] and [mg63298] Adding matrices for use in NDSolve input*From*: "Ingolf Dahl" <ingolf.dahl at telia.com>*Date*: Thu, 29 Dec 2005 02:57:32 -0500 (EST)*Sender*: owner-wri-mathgroup at wolfram.com

Hi Maarten, Your problem in [mg63316] Adding matrices for use in NDSolve input: > > S'[t]=A.S[t]+B > S[t]=0 (n x m) > I assume here that you intend S[0]=0 to get appropriate boundary conditions. Moreover, I hope that you know that the different columns of S are decoupled and never mixed, so you could equally well solve one column at a time. Anyway, I got curious to check the applicability of my notation from "[mg63184] EUREKA Re: [mg62800] Types in Mathematica, a practical example", see http://forums.wolfram.com/mathgroup/archive/2005/Dec/msg00492.html. It is possible to use something that is similar to this approach: Enter the following expressions to define the matrix S[t]: n = 3; m = 2; SetAttributes[HoldArrayFunction, HoldFirst]; HoldArrayFunction[f_, x_, n__] := Module[{g}, Array[g, n] /. {g[a___] :> Hold[f[[a]]][x]}]; Clear[S]; S[t_] := HoldArrayFunction[S, t, {n, m}]; S[t] will then be defined as {{Hold[S[[1,1]]][t], Hold[S[[1,2]]][t]}, {Hold[S[[2,1]]][t], Hold[S[[2,2]]][t]}, {Hold[S[[3,1]]][t], Hold[S[[3,2]]][t]}} However, I think you here could equally well use any other undefined functions of t as matrix elements. For instance, you could use the Array command to specify the elements: Clear[S]; S[t_] := Array[dummy[#1, #2, t] &, {n, m}]; What definition you use is up to your taste, since here you probably are most interested in the final result, and there this choice does not show up. Instead of your matrix A, I have used the matrix a, defined by a = Table[Random[], {i, n}, {j, n}]; (replace by your own matrix!) and as B I have used b = Table[Random[], {i, n}, {j, m}]; To solve the equations for the range t=0 to t=1, I have used S[t_]= S[t]/. NDSolve[ Join[ Flatten[ Table[ D[ S[t][[ i,j]],t]== ( a. S[t]+b)[[ i,j]], { i,3}, { j,2}]], Flatten[ Table[ S[0][[ i,j]]==0, { i,3}, { j,2}]]], Flatten[ S[t]], { t,0.,1.}] [[1]] This seems to work, and I can plot one of the matrix elements by Plot[S[t][[3, 1]], {t, 0, 1.}] For your second problem, described in [mg63298] we can do similarly. The matrix equation was S'[t]==A(x).S[t]+B(x) x'[t]==f[x] Here we must add boundary conditions for S and x. I have assumed S[0]=0 and x[0]=1. As A[x] I have used a1[x_ ] := a*x; where the matrix a is defined as before. As B[x] I have used b1[x_] := b*x^2; where b is defined as before. We must also define f[x] in some way, and for the sake of testing I put f[x_] := Sin[x]; Then we are ready for NDSolve: Clear[S]; S[t_] := HoldArrayFunction[S, t, {n, m}]; S[t_]= S[t]/. NDSolve[ Join[ Flatten[ Table[ D[ S[t][[ i,j]],t]== ( a1[ x[t]]. S[t]+ b1[ x[t]])[[ i,j]], { i,3}, { j,2}]], { x'[t]== f[ x[t]]}, Flatten[ Table[ S[0][ [ i,j]]==0, { i,3}, { j,2}]], { x[0]==1.}], Flatten[ { S[t], x[t]}], { t,0.,1.}] [[1]] and we can plot one of the components of S by Plot[S[t][[1, 1]], {t, 0, 1.}] Best regards Ingolf Dahl Sweden > -----Original Message----- > From: Maarten [mailto:maarten at stack.nl] To: mathgroup at smc.vnet.net > Subject: [mg63401] [mg63316] Adding matrices for use in NDSolve input > > Hello, > > I have the following problem. I'm trying to integrate a > matrix equation of the form: > > S'[t]=A.S[t]+B > S[t]=0 (n x m) > > where A and B are symbolic matrices. B is of dimension n x m > and I intend S[t] to also be of this dimension. However when > I input this differential equation mathematica assumes S[t] > is an n x 1 vector and replicates this vector match B for addition: > > S'[t]={A.S[t],A.S[t],...,A.S[t]}+B > > How can I specify my intended dimension of S[t] without makes > variables for the individual elements of S[t]? I've tried > playing around with hold to delay interpretation of the > summation but prevents evaluation of A and B and I suspect > the delay of summation will not help when NDSolve releases > the hold. How can I get mathematica to interpret this > equation in the way I intend? > > > -----Original Message----- > From: Maarten [mailto:maarten at stack.nl] To: mathgroup at smc.vnet.net > Subject: [mg63401] [mg63298] Matrix differential equation in NDSolve > > Hello, > > I would like to evaluate a matrix equation of the form: > > S'[t]==A(x).S[t]+B(x) > x'[t]==f[x] > > in NDSolve where A and B are symbolic matrices. The problem > is that S[t] is an unknown n x m matrix, of the same > dimension as B. If I enter the first equation as shown above, > Mathematica evaluates it to > > {A(x).S[t]+B(x){1,:},A(x).S[t]+B(x){2,:},...,A(x).S[t]+B(x){m,:}} > > that is, it assumes S[t] is a vector which is replicated to > match the dimensions of B(x). How can I get Mathematica to > respect the fact that S[t] is of dimension B in NDSolve. I > tried holdform but this does not evaluate in NDSolve >