Skip to main content
Tweeted twitter.com/StackMma/status/1120205808225198080
Became Hot Network Question
added 423 characters in body
Source Link

EDIT:

It looks like the above code works fine for a real-valued function, as opposed to matrices:

mat1[G_, tfinal_] := Block[{t}, NDSolveValue[{u'[t] == G[t]*u[t], u[0] ==1},u, {t, 0, tfinal}, Method -> "ExplicitRungeKutta"]]

g[t_?NumericQ] := Sin[t]

NIntegrate[mat1[g, 10][t], {t, 0, 10}]

(*Result: 36.4662*)

So it looks like the problem has something to do with $g$ being a matrix. I'm not sure how though.

EDIT:

It looks like the above code works fine for a real-valued function, as opposed to matrices:

mat1[G_, tfinal_] := Block[{t}, NDSolveValue[{u'[t] == G[t]*u[t], u[0] ==1},u, {t, 0, tfinal}, Method -> "ExplicitRungeKutta"]]

g[t_?NumericQ] := Sin[t]

NIntegrate[mat1[g, 10][t], {t, 0, 10}]

(*Result: 36.4662*)

So it looks like the problem has something to do with $g$ being a matrix. I'm not sure how though.

Source Link

NIntegrate on a solution of a matrix ODE

I've seen similar questions on this site but somehow the solutions there didn't manage to solve my specific problem.

I have a function mat1 that takes a square $n \times n$ matrix G, and some final time tfinal, and solves the following ODE numerically: $$u'(t) = G(t) u(t)$$ $$u(0) = \mathrm{id}_{n\times n}$$ The code is:

mat1[G_, tfinal_] := Block[{t}, NDSolveValue[{u'[t] == G[t].u[t], u[0] == IdentityMatrix[Dimensions[G[0]][[1]]]}, u, {t, 0, tfinal},
Method -> "ExplicitRungeKutta"]]

Let's take an example matrix-valued function $g(t)$:

g[t_?NumericQ] := {{Sin[t], 0}, {Cos[t], t}}

Mathematica has no problems solving the ODE with g as the input matrix:

mat1[g, 10][1.21]
(*Result: {{1.90977, 0.}, {1.92296, 2.07912}}*)

But when I want to numerically integrate it, I get the following error:

NIntegrate[mat1[g, 10][t], {t, 0, 10}]
(*NIntegrate::inum: Integrand InterpolatingFunction[{{0.,10.}},{5,3,1,{98},{4},0,0,0,0,Automatic,{},{},False},{{0.,0.120666,0.60333,0.874901,<<43>>,6.97746,7.05172,7.12517,<<48>>}},{{{{1.,0.},{0.,1.}},{{0.,0.},{1.,0.}}},{{{1.0073,0.},{0.121253,1.00731}},{{0.121252,0.},{1.0146,0.121548}}},<<48>>,<<48>>},{Automatic}][t] is not numerical at {t} = {0.000960178}.*)
(*NIntegrate::inum: Integrand InterpolatingFunction[{{0.,10.}},{5,3,1,{98},{4},0,0,0,0,Automatic,{},{},False},{{0.,0.120666,0.60333,0.874901,<<43>>,6.97746,7.05172,7.12517,<<48>>}},{{{{1.,0.},{0.,1.}},{{0.,0.},{1.,0.}}},{{{1.0073,0.},{0.121253,1.00731}},{{0.121252,0.},{1.0146,0.121548}}},<<48>>,<<48>>},{Automatic}][t] is not numerical at {t} = {0.000960178}.*)

I've also tried defining a function in between:

mat2[t_?NumericQ] := mat1[g, 10][t]

But I get the same error:

NIntegrate[mat2[t], {t, 0, 10}]
(*NIntegrate::inum: Integrand mat2[t] is not numerical at {t} = {0.0795732}.*)

It looks like even with the NumericQ, Mathematica is trying to manipulate the integrand with a symbolic $t$ before putting numbers in.