Re: Integrate a matrix-function

• To: mathgroup at smc.vnet.net
• Subject: [mg27519] Re: Integrate a matrix-function
• From: "Allan Hayes" <hay at haystack.demon.co.uk>
• Date: Sat, 3 Mar 2001 03:40:00 -0500 (EST)
• References: <97cemm\$d9s@smc.vnet.net> <97fepk\$fu5@smc.vnet.net> <97l2vg\$jf2@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Martin,
The following demonstrates the problem and its solution

In[48]:=
lst= {x,x^2};

In[52]:=
NIntegrate[{x,x^2}, {x,0,1}]

Out[52]=
{0.5,0.333333}

In[53]:=
NIntegrate[{lst[[1]],lst[[2]]}, {x,0,1}]

Out[53]=
{0.5,0.333333}

In[54]:=
NIntegrate[lst, {x,0,1}]

NIntegrate::inum: Integrand {0.5,0.25} is not numerical at {x} = {0.5}.

Out[54]=
NIntegrate[lst,{x,0,1}]

What happens is that NIntegrate looks at   lst   before it is evaluated
(NIntgegrate has attribute HoldAll), finds that it is not a list and then
works on the assumption that   lst  will be numerical when x is numerical:
this is false -- it is then a list of two numbers.
The solution is to make   lst  evaluate before NIntegrates inspects its
integrand

In[55]:=
NIntegrate[Evaluate[lst], {x,0,1}]

Out[55]=
{0.5,0.333333}

The problem does not arise with Integrate, which evaluates   lst  before
inspecting it.

In[56]:=
Integrate[lst, {x,0,1}]

Out[56]=
\!\({1\/2, 1\/3}\)

The same kind of problem arises with other functions that hold their
entries, for example Plot.

--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"Martin Richter" <mrMICE.fi at cbs.dk> wrote in message
news:97l2vg\$jf2 at smc.vnet.net...
> Hi
>
> Yes I should have started with a simple example and the one you gave me is
> working. I must say that in my case it looks like a Mathematica BUG. I
know
> that in 99,999% of the cases your are the dummy but here is my example
(the
> code is of course take out of content but you should be able to just paste
> it into Mathematica, if you/anyone has the time)
>
> So if anyone can explain, it would be great:
> Martin
>
> Code:
> A := {{0, -1, -1/2}, {0, -\[Alpha], 0}, {0, 0, -\[Kappa]}}
> B[t_] := {r, \[Alpha]*\[Beta][t], \[Kappa]*\[Theta]}
> IntegrandMean[u_, t_] := Simplify[MatrixExp[A*(t - u)].B[u]]
> X := {logP, \[Delta], v}
>
>
> \[Alpha] := 1; \[Kappa] := 5; \[Theta] := 1/5; r := 1/15;
> \[Beta]11 := 1/10; \[Beta]12 := 1/10; \[Beta]21 := 1/10; \[Beta]22 :=
1/10;
> logP := 4; \[Delta] := 1/5; v := 1/20;
>
> \[Beta][t_] := \[Beta]11*Cos[2*Pi*t] + \[Beta]12*Sin[2*Pi*t] + \[Beta]21*
>       Cos[4*Pi*t] + \[Beta]22*Sin[4*Pi*t]
>
>
> // Case one: Defining using entries:
> EX[s_, t_] := (MatrixExp[A*(t - s)] - IdentityMatrix[3]).X + {NIntegrate[
>         IntegrandMean[s, u][[1]], {u, s, t}],
>       NIntegrate[IntegrandMean[s, u][[2]], {u, s, t}],
>       NIntegrate[IntegrandMean[s, u][[3]], {u, s, t}]}
> N[EX[1, 2]]
>
> ---------
> Output:
> \!\({\(-0.21843440253831964`\), 2.7755575615628914`*^-17,
>     0.14898930795013723`}\)
>
> file://Case two: Using vector/matrix formulation
> EX1[s_, t_] := (MatrixExp[A*(t - s)] - IdentityMatrix[3]).X +
>     NIntegrate[IntegrandMean[s, u], {u, s, t}]
> N[EX1[1, 2]]
>
> ----------
> Output:
> \!\(NIntegrate::"inum" \(\(:\)\(\ \)\)
>     "Integrand \!\({\(\(-0.10381870152841678`\)\), 0.1213061319425267`, \
> 0.0820849986238988`}\) is not numerical at \!\({u}\) = \!\({3\/2}\)."\)
> And so one:
>
>
>
> "Allan Hayes" <hay at haystack.demon.co.uk> wrote in message
> news:97fepk\$fu5 at smc.vnet.net...
> > Martin,
> >
> > With
> > f[s_,t_] := {{s t, s t^2},{s^2 t,s^2 t^2}}
> >
> > The integration that you  use seems to do what you want: the outpur is a
> > matrix of the same size as the integrand.
> >
> > Integrate[f[u,t],{u,s,t}]
> >
> > {{t*(-(s^2/2) + t^2/2), t^2*(-(s^2/2) + t^2/2)},
> >   {t*(-(s^3/3) + t^3/3), t^2*(-(s^3/3) + t^3/3)}}
> >
> > --
> > Allan
> > ---------------------
> > Allan Hayes
> > Mathematica Training and Consulting
> > Leicester UK
> > www.haystack.demon.co.uk
> > hay at haystack.demon.co.uk
> > Voice: +44 (0)116 271 4198
> > Fax: +44 (0)870 164 0565
> >
> > "Martin Richter" <mrMICE.fi at cbs.dk> wrote in message
> > news:97cemm\$d9s at smc.vnet.net...
> > > Hi
> > >
> > > I'm using Mathematical 4.0. The problem is the following.
> > >
> > > I have a function, say f[s_,t_], there s,t are real numbers and the
> output
> > > is a matrix of size (n,n). I need to integrate each component in the
> > matrix,
> > > i.e. make a new function, like this:
> > > g[s_,t_] := Integrate[f[u,t], {u,s,t}] // error
> > >
> > > So right now I have a line for each element but I think there is a
> > one-line
> > > way of doing this (using Map or Apply) ?
> > >
> > > Thanks IA for any help
> > > Martin
> > >
> > >
> > > ----------------------------------------------
> > >
> > >
> > >
> >
> >
> >
>
>
>

```

• Prev by Date: Re: Cobweb Plot
• Next by Date: RE: Cobweb Plot
• Previous by thread: Re: Integrate a matrix-function
• Next by thread: Re: converting hex strings