3
$\begingroup$

Is there a way to numerically integrate a vector function defined via Piecewise?

Example:

 test[s_] := Piecewise[{
                      {{s, s^2, s^3}, s < -2},
                      {{0, s - 1, Sin[s]}, -2 <= s < 1}, 
                      {{1 - s^2, Exp[s], 0},1 <= s}}]
 NIntegrate[test[s],{s,-2,2}]

This gives NIntegrate::inum: Integrand … is not numerical at … as error.

$\endgroup$
  • 5
    $\begingroup$ @Sektor There's nothing wrong with it, it just confuses NIntegrate. NIntegrate does support vector arguments if its explicitly given as a vector, e.g. NIntegrate[{Sin[x],Cos[x]},{x,0,1}]. But if it can't see that it's a vector before evaluating the function it will expect a single number, get confused and fail: f[x_?NumericQ]:={Sin[x],Cos[x]}; NIntegrate[f[x],{x,0,1}] (this fails). $\endgroup$ – Szabolcs Apr 10 '14 at 14:52
  • $\begingroup$ @Szabolcs nicely explained +1 $\endgroup$ – Vitaliy Kaurov Apr 10 '14 at 15:17
7
$\begingroup$

I suppose I dislike mixing structural operations - like Thread - inside algebraic expressions. My objections are two-fold:

1) I prefer to keep Mathematica operations distinct from actual algebra for aesthetic reasons.

2) If an expression is cut and pasted and changed, operations like Thread can all too easily end up chewing into mathematical expressions, and generate nonsense!

IMHO, a better approach is to write a VectorPiecewise function that checks if it is handling the scalar case, and returns it if it is, rejects inconsistent vector expressions, and returns the vector case as a list of scalar Piecewise expressions, e.g.:

VectorPiecewise::syntax = 
  "Invalid VectorPiecewise construct - vector expressions are either of \
incompatible lengths or some are not vectors";

VectorPiecewise[expr_] := Module[{vv, hv, vc},
  vv = expr[[All, 1]];
  hv = Map[(Head[#] === List) &, vv];
  If[! Or @@ hv,
   Piecewise[expr],
   If[(! And @@ hv ) || (! Equal @@ Map[Length, vv]),
    Message[VectorPiecwise::syntax];,
    vc = expr[[All, 2]];
    vvt = Transpose[vv];
    Map[Piecewise, MapIndexed[{#1, vc[[#2[[2]]]]} &, vvt, {2}]]
    ]
   ]
  ];

test[s_] := 
 VectorPiecewise[{{{s, s^2, s^3}, 
    s < -2}, {{0, s - 1, Sin[s]}, -2 <= s < 1}, {{1 - s^2, Exp[s], 0},
     1 <= s}}]

 NIntegrate[test[s], {s, -2, 2}]
 {-1.33333, 0.170774, -0.956449}

Plot[test[s] // Evaluate, {s, -2, 2}, Filling -> {1 -> {2}, 2 -> {3}}]

Mathematica graphics

If your complete expression contains several such VectorPiecewise expressions, as you describe, the normal operation of the Listable attribute will ensure the expression will work as expected, except that multiplying two vectors together needs you to specify either Dot or Cross - default multiplication of vectors gives you nothing useful.

$\endgroup$
5
$\begingroup$

If this is a vector function then we expect its components to get integrated independently? If this is what you want, then see that vector Piecewise and Piecewise vector are not the same things. NIntegrate, Plot, etc. need to see a List as an outer wrapper to know what to do with inside stuff.

intervals = {s < -2, -2 <= s < 1, 1 <= s};

test[s_] := Piecewise[Thread[#]] & /@ {
   {{s, 0, 1 - s^2}, intervals},
   {{s^2, s - 1, Exp[s]}, intervals},
   {{s^3, Sin[s], 0}, intervals}
   }

In[1]:= NIntegrate[test[s], {s, -2, 2}]
Out[1]= {-1.33333, 0.170774, -0.956449}

Plot[test[s] // Evaluate, {s, -2, 2}, Filling -> {1 -> {2}, 2 -> {3}}]

enter image description here

$\endgroup$
  • $\begingroup$ When I tried to plot this function in the definition of Tomas and in yours I got the same though. $\endgroup$ – Alexei Boulbitch Apr 10 '14 at 14:54
  • $\begingroup$ @AlexeiBoulbitch not really - I bet you won't be able to do proper different coloring and filling options - because Plot will see Tomas' function "as one thing" - not a vector of different things. Try it - am I right? $\endgroup$ – Vitaliy Kaurov Apr 10 '14 at 15:01
  • $\begingroup$ Yes, you are right. This was in fact my question: where will I see the difference. Thank you. $\endgroup$ – Alexei Boulbitch Apr 10 '14 at 15:05
  • $\begingroup$ Yes, that's what I want. I was aware that vector Piecewise and Piecewise vector are not the same thing, but I couldn't figure out a way to transpose the two. Now my actual function is a composition of several Piecewise-functions multiplied by stuff and added up. Can I somehow make one Piecewise-function from that and then convert it to a vector of Piecewise functions so that I can numerically integrate the result? $\endgroup$ – Thomas Apr 10 '14 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.