       Re: Piecewise function involving matrices

• To: mathgroup at smc.vnet.net
• Subject: [mg91230] Re: Piecewise function involving matrices
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Sun, 10 Aug 2008 01:53:41 -0400 (EDT)
• Organization: The Open University, Milton Keynes, UK
• References: <g7k0gn\$ieh\$1@smc.vnet.net>

```kaveh wrote:

> I am trying to define a piecewise matrix function of time with
> Mathematica that I later want to use to transform another matrix as a
> function of time and integrate it. To make it simple I have
>
> In:= A={{1,.1},{.1,-1}};
> In:= H0={{0,b},{b,0}};
> In:= F[t_]:=Piecewise[{{exp[-I t] A,0<t<1},{exp[+I t] A,1<t<2}}]

Syntax error: Except if you have already defined the function exp
somewhere else, exp should be written Exp, which is the standard
exponential function in Mathematica.

> In:= F[0.1]
> Out= {{exp[-0.1 \[ImaginaryI]],0.1 exp[-0.1 \[ImaginaryI]]},{0.1
> exp[-0.1 \[ImaginaryI]],-exp[-0.1 \[ImaginaryI]]}}
>
> In:= H[t_]:= Inverse[F[t]].H0.F[t]+H0.H0
>
> In:= H[0.4]
> Out= {{0.19802 b+b^2,-0.980198 b},{-0.980198 b,-0.19802 b+b^2}}
>
> The problem is that when I evaluate H[t] for a given number, it
> returns the right value but if t is a symbol Mathematica naturally
> leaves it as unevaluated. This by itself is nice, however when I want
> to integrate H[t], I run into problems.
> Say
> In:= Integrate[H[x],{x,0,1}]

<snip>

> Notice that the output is not a 2x2 matrix but has dimensions
> {2,2,2,2}!
> Same with:
> In:= Integrate[H[x][[1,1]],{x,0,1}]
> Out= {{0.00990099 (20. b+101. b^2),0.00990099 (-99. b+101. b^2)},
> {0.00990099 (-99. b+101. b^2),0.00990099 (-20. b+101. b^2)}}
> which should be just a number but is a 2x2 matrix!
>
> Obviously there is something wrong with the treatment of piecewise as
> an array instead of a flat thing.

Since Mathematica 6.0, Integrate works well with *continuous* piecewise
functions.

In your case, you should use *Boole* to write the piecewise function.
For instance,

In:= A = {{1, .1}, {.1, -1}};

H0 = {{0, b}, {b, 0}};

F[t_] := Boole[0 < t < 1] Exp[-I t] A + Boole[1 < t < 2] Exp[+I t] A

F[0.1]

H[t_] := Inverse[F[t]].H0.F[t] + H0.H0

H[0.4]

Out= {{0.995004- 0.0998334 I,
0.0995004- 0.00998334 I}, {0.0995004- 0.00998334 I, -0.995004 +
0.0998334 I}}

Out= {{(0.19802- 6.93889*10^-18 I) b +
b^2, (-0.980198 - 4.33681*10^-19 I) b}, {(-0.980198 -
4.33681*10^-19 I) b, (-0.19802 + 6.93889*10^-18 I) b + b^2}}

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

Out= {{0.00990099 (20. b + 101. b^2), -0.980198 b}, {-0.980198 b,
0.00990099 (-20. b + 101. b^2)}}

In:= % // Dimensions

Out= {2, 2}

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

Out= 0.00990099 (20. b + 101. b^2)

In:= % // Dimensions

Out= {2}

Regards,
-- Jean-Marc

```

• Prev by Date: Printing and Exporting graphics images from Mathematica 6 to PDF?
• Next by Date: Simulating a Mouseover Event with EventHandler
• Previous by thread: Piecewise function involving matrices
• Next by thread: Re: Piecewise function involving matrices