NIntegrate on a solution of a matrix ODE Planned maintenance scheduled April 23, 2019 at 23:30...

Did John Wesley plagiarize Matthew Henry...?

Twin's vs. Twins'

One-one communication

Weaponising the Grasp-at-a-Distance spell

When does a function NOT have an antiderivative?

Maximum duration for Canada's short term visas?

Why do C and C++ allow the expression (int) + 4?

How does the body cool itself in a stillsuit?

Noise in Eigenvalues plot

Marquee sign letters

My mentor says to set image to Fine instead of RAW — how is this different from JPG?

In musical terms, what properties are varied by the human voice to produce different words / syllables?

As a dual citizen, my US passport will expire one day after traveling to the US. Will this work?

What did Turing mean when saying that "machines cannot give rise to surprises" is due to a fallacy?

Order between one to one functions and their inverses

How to resize main filesystem

latest version of QGIS fails to edit attribute table of GeoJSON file

Random body shuffle every night—can we still function?

Does the universe have a fixed centre of mass?

Is there any significance to the prison numbers of the Beagle Boys starting with 176-?

Is there a verb for listening stealthily?

What are some likely causes to domain member PC losing contact to domain controller?

malloc in main() or malloc in another function: allocating memory for a struct and its members

Is a copyright notice with a non-existent name be invalid?



NIntegrate on a solution of a matrix ODE



Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?How to calculate the numerical integral more efficiently?Nested NIntegrate - NIntegrate::inum: - errorDetermining which rule NIntegrate selects automaticallyProblems with NIntegrateNIntegrate giving message NIntegrate::slwcon:Numerical Differentiation of a Numerical IntegralNested NIntegrate with a function in betweenNIntegrate::slwcon, NIntegrate::eincr and Set::wrsym problemsNumerical integration gives errors NIntegrate::slwcon: and Integrate::eincr:Problem with NIntegrate over a highly-oscillatory integrandIntegrate an Interpolating function with Integrate command












3












$begingroup$


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}_{ntimes 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.



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.










share|improve this question









New contributor




Sahand Tabatabaei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    Welcome to Mathematica.SE, Sahand! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
    $endgroup$
    – Chris K
    2 hours ago










  • $begingroup$
    This is an answer to a related a question.
    $endgroup$
    – Anton Antonov
    13 mins ago


















3












$begingroup$


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}_{ntimes 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.



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.










share|improve this question









New contributor




Sahand Tabatabaei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    Welcome to Mathematica.SE, Sahand! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
    $endgroup$
    – Chris K
    2 hours ago










  • $begingroup$
    This is an answer to a related a question.
    $endgroup$
    – Anton Antonov
    13 mins ago
















3












3








3





$begingroup$


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}_{ntimes 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.



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.










share|improve this question









New contributor




Sahand Tabatabaei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




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}_{ntimes 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.



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.







differential-equations numerical-integration numerics numerical-value






share|improve this question









New contributor




Sahand Tabatabaei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Sahand Tabatabaei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 3 hours ago







Sahand Tabatabaei













New contributor




Sahand Tabatabaei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 5 hours ago









Sahand TabatabaeiSahand Tabatabaei

1185




1185




New contributor




Sahand Tabatabaei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Sahand Tabatabaei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Sahand Tabatabaei is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $begingroup$
    Welcome to Mathematica.SE, Sahand! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
    $endgroup$
    – Chris K
    2 hours ago










  • $begingroup$
    This is an answer to a related a question.
    $endgroup$
    – Anton Antonov
    13 mins ago




















  • $begingroup$
    Welcome to Mathematica.SE, Sahand! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
    $endgroup$
    – Chris K
    2 hours ago










  • $begingroup$
    This is an answer to a related a question.
    $endgroup$
    – Anton Antonov
    13 mins ago


















$begingroup$
Welcome to Mathematica.SE, Sahand! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
$endgroup$
– Chris K
2 hours ago




$begingroup$
Welcome to Mathematica.SE, Sahand! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
$endgroup$
– Chris K
2 hours ago












$begingroup$
This is an answer to a related a question.
$endgroup$
– Anton Antonov
13 mins ago






$begingroup$
This is an answer to a related a question.
$endgroup$
– Anton Antonov
13 mins ago












2 Answers
2






active

oldest

votes


















5












$begingroup$

As the error message says, the problem is that mat2[0.0795732] is not numerical. It is instead a 2x2 matrix of numbers. You could do something like:



mat2[t_?NumericQ] := mat1[g, 10][t][[1,1]]
NIntegrate[mat2[t], {t, 0, 10}]



36.4662




On the other hand, it is much simpler to just have NDSolveValue do the integration for you:



mat1[G_,tfinal_] := NDSolveValue[
{
int'[t] == u[t], int[0] == ConstantArray[0, Dimensions[G[0]]],
u'[t]==G[t].u[t], u[0]==IdentityMatrix[Dimensions[G[0]][[1]]]
},
{u, int},
{t,0,tfinal}
]


Then:



mat1[g, 10]



enter image description here




and:



mat1[g, 10][[2]][10]



{{36.4662, 0.}, {3.69638*10^20, 5.23821*10^20}}




agreeing with the above result.






share|improve this answer









$endgroup$









  • 1




    $begingroup$
    I see! So Mathematica also counts matrices with numerical elements as "non-numerical". So I have to integrate it element-wise, or use NDSolve. So is there no direct way to integrate matrices with NIntegrate itself?
    $endgroup$
    – Sahand Tabatabaei
    3 hours ago












  • $begingroup$
    @SahandTabatabaei Yes, there is a direct way to integrate matrices with NIntegrate. I will post a related answer after a day or two. (I plan to extend the rule described here or program new one...)
    $endgroup$
    – Anton Antonov
    10 mins ago





















4












$begingroup$

You can use Integrate to directly antidifferentiate an interpolating function. If $f(t)$ is an interpolating function with domain $(a,b)$, Integrate[f[t], t] returns an interpolating function with the same domain equal to
$$int_a^t f(tau) ; dtau,.$$



To get the definite integral, plug the end point:



Integrate[mat1[g, 10][t], t] /. t -> 10
(* {{36.4662, 0.}, {3.69611*10^20, 5.23781*10^20}} *)





share|improve this answer









$endgroup$









  • 1




    $begingroup$
    You could also use Derivative[-1][mat1[g, 10]] to construct the interpolating function without an argument, e.g., Derivative[-1][mat1[g, 10]][10] so that no ReplaceAll is needed.
    $endgroup$
    – Carl Woll
    2 hours ago












  • $begingroup$
    @CarlWoll Thanks. I was going to add that if the OP wanted greater accuracy, using InterpolationOrder -> Allin NDSolve would likely produce a more accurate integral (by either of our methods, I suppose), but that option does not work with matrix ODEs and certain methods, such "ExplicitRungeKutta" with a difference order greater than 3. (Just reported as [CASE:4249898].)
    $endgroup$
    – Michael E2
    1 hour ago












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Sahand Tabatabaei is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f195709%2fnintegrate-on-a-solution-of-a-matrix-ode%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









5












$begingroup$

As the error message says, the problem is that mat2[0.0795732] is not numerical. It is instead a 2x2 matrix of numbers. You could do something like:



mat2[t_?NumericQ] := mat1[g, 10][t][[1,1]]
NIntegrate[mat2[t], {t, 0, 10}]



36.4662




On the other hand, it is much simpler to just have NDSolveValue do the integration for you:



mat1[G_,tfinal_] := NDSolveValue[
{
int'[t] == u[t], int[0] == ConstantArray[0, Dimensions[G[0]]],
u'[t]==G[t].u[t], u[0]==IdentityMatrix[Dimensions[G[0]][[1]]]
},
{u, int},
{t,0,tfinal}
]


Then:



mat1[g, 10]



enter image description here




and:



mat1[g, 10][[2]][10]



{{36.4662, 0.}, {3.69638*10^20, 5.23821*10^20}}




agreeing with the above result.






share|improve this answer









$endgroup$









  • 1




    $begingroup$
    I see! So Mathematica also counts matrices with numerical elements as "non-numerical". So I have to integrate it element-wise, or use NDSolve. So is there no direct way to integrate matrices with NIntegrate itself?
    $endgroup$
    – Sahand Tabatabaei
    3 hours ago












  • $begingroup$
    @SahandTabatabaei Yes, there is a direct way to integrate matrices with NIntegrate. I will post a related answer after a day or two. (I plan to extend the rule described here or program new one...)
    $endgroup$
    – Anton Antonov
    10 mins ago


















5












$begingroup$

As the error message says, the problem is that mat2[0.0795732] is not numerical. It is instead a 2x2 matrix of numbers. You could do something like:



mat2[t_?NumericQ] := mat1[g, 10][t][[1,1]]
NIntegrate[mat2[t], {t, 0, 10}]



36.4662




On the other hand, it is much simpler to just have NDSolveValue do the integration for you:



mat1[G_,tfinal_] := NDSolveValue[
{
int'[t] == u[t], int[0] == ConstantArray[0, Dimensions[G[0]]],
u'[t]==G[t].u[t], u[0]==IdentityMatrix[Dimensions[G[0]][[1]]]
},
{u, int},
{t,0,tfinal}
]


Then:



mat1[g, 10]



enter image description here




and:



mat1[g, 10][[2]][10]



{{36.4662, 0.}, {3.69638*10^20, 5.23821*10^20}}




agreeing with the above result.






share|improve this answer









$endgroup$









  • 1




    $begingroup$
    I see! So Mathematica also counts matrices with numerical elements as "non-numerical". So I have to integrate it element-wise, or use NDSolve. So is there no direct way to integrate matrices with NIntegrate itself?
    $endgroup$
    – Sahand Tabatabaei
    3 hours ago












  • $begingroup$
    @SahandTabatabaei Yes, there is a direct way to integrate matrices with NIntegrate. I will post a related answer after a day or two. (I plan to extend the rule described here or program new one...)
    $endgroup$
    – Anton Antonov
    10 mins ago
















5












5








5





$begingroup$

As the error message says, the problem is that mat2[0.0795732] is not numerical. It is instead a 2x2 matrix of numbers. You could do something like:



mat2[t_?NumericQ] := mat1[g, 10][t][[1,1]]
NIntegrate[mat2[t], {t, 0, 10}]



36.4662




On the other hand, it is much simpler to just have NDSolveValue do the integration for you:



mat1[G_,tfinal_] := NDSolveValue[
{
int'[t] == u[t], int[0] == ConstantArray[0, Dimensions[G[0]]],
u'[t]==G[t].u[t], u[0]==IdentityMatrix[Dimensions[G[0]][[1]]]
},
{u, int},
{t,0,tfinal}
]


Then:



mat1[g, 10]



enter image description here




and:



mat1[g, 10][[2]][10]



{{36.4662, 0.}, {3.69638*10^20, 5.23821*10^20}}




agreeing with the above result.






share|improve this answer









$endgroup$



As the error message says, the problem is that mat2[0.0795732] is not numerical. It is instead a 2x2 matrix of numbers. You could do something like:



mat2[t_?NumericQ] := mat1[g, 10][t][[1,1]]
NIntegrate[mat2[t], {t, 0, 10}]



36.4662




On the other hand, it is much simpler to just have NDSolveValue do the integration for you:



mat1[G_,tfinal_] := NDSolveValue[
{
int'[t] == u[t], int[0] == ConstantArray[0, Dimensions[G[0]]],
u'[t]==G[t].u[t], u[0]==IdentityMatrix[Dimensions[G[0]][[1]]]
},
{u, int},
{t,0,tfinal}
]


Then:



mat1[g, 10]



enter image description here




and:



mat1[g, 10][[2]][10]



{{36.4662, 0.}, {3.69638*10^20, 5.23821*10^20}}




agreeing with the above result.







share|improve this answer












share|improve this answer



share|improve this answer










answered 3 hours ago









Carl WollCarl Woll

75.1k3100197




75.1k3100197








  • 1




    $begingroup$
    I see! So Mathematica also counts matrices with numerical elements as "non-numerical". So I have to integrate it element-wise, or use NDSolve. So is there no direct way to integrate matrices with NIntegrate itself?
    $endgroup$
    – Sahand Tabatabaei
    3 hours ago












  • $begingroup$
    @SahandTabatabaei Yes, there is a direct way to integrate matrices with NIntegrate. I will post a related answer after a day or two. (I plan to extend the rule described here or program new one...)
    $endgroup$
    – Anton Antonov
    10 mins ago
















  • 1




    $begingroup$
    I see! So Mathematica also counts matrices with numerical elements as "non-numerical". So I have to integrate it element-wise, or use NDSolve. So is there no direct way to integrate matrices with NIntegrate itself?
    $endgroup$
    – Sahand Tabatabaei
    3 hours ago












  • $begingroup$
    @SahandTabatabaei Yes, there is a direct way to integrate matrices with NIntegrate. I will post a related answer after a day or two. (I plan to extend the rule described here or program new one...)
    $endgroup$
    – Anton Antonov
    10 mins ago










1




1




$begingroup$
I see! So Mathematica also counts matrices with numerical elements as "non-numerical". So I have to integrate it element-wise, or use NDSolve. So is there no direct way to integrate matrices with NIntegrate itself?
$endgroup$
– Sahand Tabatabaei
3 hours ago






$begingroup$
I see! So Mathematica also counts matrices with numerical elements as "non-numerical". So I have to integrate it element-wise, or use NDSolve. So is there no direct way to integrate matrices with NIntegrate itself?
$endgroup$
– Sahand Tabatabaei
3 hours ago














$begingroup$
@SahandTabatabaei Yes, there is a direct way to integrate matrices with NIntegrate. I will post a related answer after a day or two. (I plan to extend the rule described here or program new one...)
$endgroup$
– Anton Antonov
10 mins ago






$begingroup$
@SahandTabatabaei Yes, there is a direct way to integrate matrices with NIntegrate. I will post a related answer after a day or two. (I plan to extend the rule described here or program new one...)
$endgroup$
– Anton Antonov
10 mins ago













4












$begingroup$

You can use Integrate to directly antidifferentiate an interpolating function. If $f(t)$ is an interpolating function with domain $(a,b)$, Integrate[f[t], t] returns an interpolating function with the same domain equal to
$$int_a^t f(tau) ; dtau,.$$



To get the definite integral, plug the end point:



Integrate[mat1[g, 10][t], t] /. t -> 10
(* {{36.4662, 0.}, {3.69611*10^20, 5.23781*10^20}} *)





share|improve this answer









$endgroup$









  • 1




    $begingroup$
    You could also use Derivative[-1][mat1[g, 10]] to construct the interpolating function without an argument, e.g., Derivative[-1][mat1[g, 10]][10] so that no ReplaceAll is needed.
    $endgroup$
    – Carl Woll
    2 hours ago












  • $begingroup$
    @CarlWoll Thanks. I was going to add that if the OP wanted greater accuracy, using InterpolationOrder -> Allin NDSolve would likely produce a more accurate integral (by either of our methods, I suppose), but that option does not work with matrix ODEs and certain methods, such "ExplicitRungeKutta" with a difference order greater than 3. (Just reported as [CASE:4249898].)
    $endgroup$
    – Michael E2
    1 hour ago
















4












$begingroup$

You can use Integrate to directly antidifferentiate an interpolating function. If $f(t)$ is an interpolating function with domain $(a,b)$, Integrate[f[t], t] returns an interpolating function with the same domain equal to
$$int_a^t f(tau) ; dtau,.$$



To get the definite integral, plug the end point:



Integrate[mat1[g, 10][t], t] /. t -> 10
(* {{36.4662, 0.}, {3.69611*10^20, 5.23781*10^20}} *)





share|improve this answer









$endgroup$









  • 1




    $begingroup$
    You could also use Derivative[-1][mat1[g, 10]] to construct the interpolating function without an argument, e.g., Derivative[-1][mat1[g, 10]][10] so that no ReplaceAll is needed.
    $endgroup$
    – Carl Woll
    2 hours ago












  • $begingroup$
    @CarlWoll Thanks. I was going to add that if the OP wanted greater accuracy, using InterpolationOrder -> Allin NDSolve would likely produce a more accurate integral (by either of our methods, I suppose), but that option does not work with matrix ODEs and certain methods, such "ExplicitRungeKutta" with a difference order greater than 3. (Just reported as [CASE:4249898].)
    $endgroup$
    – Michael E2
    1 hour ago














4












4








4





$begingroup$

You can use Integrate to directly antidifferentiate an interpolating function. If $f(t)$ is an interpolating function with domain $(a,b)$, Integrate[f[t], t] returns an interpolating function with the same domain equal to
$$int_a^t f(tau) ; dtau,.$$



To get the definite integral, plug the end point:



Integrate[mat1[g, 10][t], t] /. t -> 10
(* {{36.4662, 0.}, {3.69611*10^20, 5.23781*10^20}} *)





share|improve this answer









$endgroup$



You can use Integrate to directly antidifferentiate an interpolating function. If $f(t)$ is an interpolating function with domain $(a,b)$, Integrate[f[t], t] returns an interpolating function with the same domain equal to
$$int_a^t f(tau) ; dtau,.$$



To get the definite integral, plug the end point:



Integrate[mat1[g, 10][t], t] /. t -> 10
(* {{36.4662, 0.}, {3.69611*10^20, 5.23781*10^20}} *)






share|improve this answer












share|improve this answer



share|improve this answer










answered 2 hours ago









Michael E2Michael E2

151k12203483




151k12203483








  • 1




    $begingroup$
    You could also use Derivative[-1][mat1[g, 10]] to construct the interpolating function without an argument, e.g., Derivative[-1][mat1[g, 10]][10] so that no ReplaceAll is needed.
    $endgroup$
    – Carl Woll
    2 hours ago












  • $begingroup$
    @CarlWoll Thanks. I was going to add that if the OP wanted greater accuracy, using InterpolationOrder -> Allin NDSolve would likely produce a more accurate integral (by either of our methods, I suppose), but that option does not work with matrix ODEs and certain methods, such "ExplicitRungeKutta" with a difference order greater than 3. (Just reported as [CASE:4249898].)
    $endgroup$
    – Michael E2
    1 hour ago














  • 1




    $begingroup$
    You could also use Derivative[-1][mat1[g, 10]] to construct the interpolating function without an argument, e.g., Derivative[-1][mat1[g, 10]][10] so that no ReplaceAll is needed.
    $endgroup$
    – Carl Woll
    2 hours ago












  • $begingroup$
    @CarlWoll Thanks. I was going to add that if the OP wanted greater accuracy, using InterpolationOrder -> Allin NDSolve would likely produce a more accurate integral (by either of our methods, I suppose), but that option does not work with matrix ODEs and certain methods, such "ExplicitRungeKutta" with a difference order greater than 3. (Just reported as [CASE:4249898].)
    $endgroup$
    – Michael E2
    1 hour ago








1




1




$begingroup$
You could also use Derivative[-1][mat1[g, 10]] to construct the interpolating function without an argument, e.g., Derivative[-1][mat1[g, 10]][10] so that no ReplaceAll is needed.
$endgroup$
– Carl Woll
2 hours ago






$begingroup$
You could also use Derivative[-1][mat1[g, 10]] to construct the interpolating function without an argument, e.g., Derivative[-1][mat1[g, 10]][10] so that no ReplaceAll is needed.
$endgroup$
– Carl Woll
2 hours ago














$begingroup$
@CarlWoll Thanks. I was going to add that if the OP wanted greater accuracy, using InterpolationOrder -> Allin NDSolve would likely produce a more accurate integral (by either of our methods, I suppose), but that option does not work with matrix ODEs and certain methods, such "ExplicitRungeKutta" with a difference order greater than 3. (Just reported as [CASE:4249898].)
$endgroup$
– Michael E2
1 hour ago




$begingroup$
@CarlWoll Thanks. I was going to add that if the OP wanted greater accuracy, using InterpolationOrder -> Allin NDSolve would likely produce a more accurate integral (by either of our methods, I suppose), but that option does not work with matrix ODEs and certain methods, such "ExplicitRungeKutta" with a difference order greater than 3. (Just reported as [CASE:4249898].)
$endgroup$
– Michael E2
1 hour ago










Sahand Tabatabaei is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Sahand Tabatabaei is a new contributor. Be nice, and check out our Code of Conduct.













Sahand Tabatabaei is a new contributor. Be nice, and check out our Code of Conduct.












Sahand Tabatabaei is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Mathematica Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f195709%2fnintegrate-on-a-solution-of-a-matrix-ode%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

(145452) 2005 RN43 Классификация | Примечания | Ссылки |...

Щит и меч (фильм) Содержание Названия серий | Сюжет |...

Энтрерриос (город) Содержание История | Географическое...