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

How to achieve cat-like agility?

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

Is the Mordenkainens' Sword spell underpowered?

Flight departed from the gate 5 min before scheduled departure time. Refund options

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

Short story about astronauts fertilizing soil with their own bodies

Weaponising the Grasp-at-a-Distance spell

How does TikZ render an arc?

Why are two-digit numbers in Jonathan Swift's "Gulliver's Travels" (1726) written in "German style"?

By what mechanism was the 2017 UK General Election called?

Who said what about *meanings*?

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

NIntegrate on a solution of a matrix ODE

Why can't fire hurt Daenerys but it did to Jon Snow in season 1?

First paper to introduce the "principal-agent problem"

Vertical ranges of Column Plots in 12

Sally's older brother

How do Java 8 default methods hеlp with lambdas?

Why are current probes so expensive?

Is the time—manner—place ordering of adverbials an oversimplification?

Did pre-Columbian Americans know the spherical shape of the Earth?

.bashrc alias for a command with fixed second parameter

Why complex landing gears are used instead of simple, reliable and light weight muscle wire or shape memory alloys?

Can the Haste spell grant both a Beast Master ranger and their animal companion extra attacks?



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) = mathrmid_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
    4 hours ago










  • $begingroup$
    This is an answer to a related a question.
    $endgroup$
    – Anton Antonov
    2 hours 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) = mathrmid_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
    4 hours ago










  • $begingroup$
    This is an answer to a related a question.
    $endgroup$
    – Anton Antonov
    2 hours 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) = mathrmid_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) = mathrmid_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 5 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 6 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
    4 hours ago










  • $begingroup$
    This is an answer to a related a question.
    $endgroup$
    – Anton Antonov
    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
    4 hours ago










  • $begingroup$
    This is an answer to a related a question.
    $endgroup$
    – Anton Antonov
    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
4 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
4 hours ago












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





$begingroup$
This is an answer to a related a question.
$endgroup$
– Anton Antonov
2 hours 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
    5 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
    2 hours 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
    4 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
    3 hours 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
    5 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
    2 hours 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
    5 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
    2 hours 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 5 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
    5 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
    2 hours 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
    5 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
    2 hours 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
5 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
5 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
2 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
2 hours 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
    4 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
    3 hours 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
    4 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
    3 hours 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 4 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
    4 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
    3 hours 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
    4 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
    3 hours 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
4 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
4 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
3 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
3 hours 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