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

Vertical ranges of Column Plots in 12

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

Is this Kuo-toa homebrew race balanced?

Twin's vs. Twins'

Find general formula for the terms

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

Did any compiler fully use 80-bit floating point?

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

Understanding piped command in Gnu/Linux

What does 丫 mean? 丫是什么意思?

Can two people see the same photon?

What does the writing on Poe's helmet say?

Does the Rock Gnome trait Artificer's Lore apply when you aren't proficient in History?

Relating to the President and obstruction, were Mueller's conclusions preordained?

How could a hydrazine and N2O4 cloud (or it's reactants) show up in weather radar?

Does the main washing effect of soap come from foam?

Pointing to problems without suggesting solutions

systemd and copy (/bin/cp): no such file or directory

.bashrc alias for a command with fixed second parameter

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

How to ask rejected full-time candidates to apply to teach individual courses?

Was the pager message from Nick Fury to Captain Marvel unnecessary?

Google .dev domain strangely redirects to https

How much damage would a cupful of neutron star matter do to the Earth?



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
    1 hour 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
    1 hour 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 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 4 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
    1 hour 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
    1 hour 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
1 hour 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
1 hour 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
    2 hours ago











  • $begingroup$
    @SahandTabatabaei Yes, see MSE this answer.
    $endgroup$
    – Anton Antonov
    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
    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











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
    2 hours ago











  • $begingroup$
    @SahandTabatabaei Yes, see MSE this answer.
    $endgroup$
    – Anton Antonov
    1 hour 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
    2 hours ago











  • $begingroup$
    @SahandTabatabaei Yes, see MSE this answer.
    $endgroup$
    – Anton Antonov
    1 hour 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
    2 hours ago











  • $begingroup$
    @SahandTabatabaei Yes, see MSE this answer.
    $endgroup$
    – Anton Antonov
    1 hour 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
    2 hours ago











  • $begingroup$
    @SahandTabatabaei Yes, see MSE this answer.
    $endgroup$
    – Anton Antonov
    1 hour 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
2 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
2 hours ago













$begingroup$
@SahandTabatabaei Yes, see MSE this answer.
$endgroup$
– Anton Antonov
1 hour ago




$begingroup$
@SahandTabatabaei Yes, see MSE this answer.
$endgroup$
– Anton Antonov
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
    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















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
    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













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
    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












  • 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
    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







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
1 hour 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
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




$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

Era Viking Índice Início da Era Viquingue | Cotidiano | Sociedade | Língua | Religião | A arte | As primeiras cidades | As viagens dos viquingues | Viquingues do Oeste e Leste | Fim da Era Viquingue | Fontes históricas | Referências Bibliografia | Ligações externas | Menu de navegação«Sverige då!»«Handel I vikingetid»«O que é Nórdico Antigo»Mito, magia e religião na volsunga saga Um olhar sobre a trajetória mítica do herói sigurd«Bonden var den verklige vikingen»«Vikingatiden»«Vikingatiden»«Vinland»«Guerreiras de Óðinn: As Valkyrjor na Mitologia Viking»1519-9053«Esculpindo símbolos e seres: A arte viking em pedras rúnicas»1679-9313Historia - Tema: VikingarnaAventura e Magia no Mundo das Sagas IslandesasEra Vikinge

What's the metal clinking sound at the end of credits in Avengers: Endgame?What makes Thanos so strong in Avengers: Endgame?Who is the character that appears at the end of Endgame?What happens to Mjolnir (Thor's hammer) at the end of Endgame?The People's Ages in Avengers: EndgameWhat did Nebula do in Avengers: Endgame?Messing with time in the Avengers: Endgame climaxAvengers: Endgame timelineWhat are the time-travel rules in Avengers Endgame?Why use this song in Avengers: Endgame Opening Logo Sequence?Peggy's age in Avengers Endgame

Are there legal definitions of ethnicities/races? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Legal definitions in the United StatesAre there truly legal limits on US interest rates?Are gender identity and sexual orientation federally protected?Why is there an apparent legal bias against digital services?What limits are there to the powers of individual judges in the United States legal system?Are women only scholarships legal under Irish / EU law?Is the term “race” defined by Public Law enacted by Congress of the United StatesIs there a legal definition of race in the US?Neighbors are spying for landlord on Renters is it legal?Are Protected Classes Bi-directional?