# Integrate of 2D piecewise function with constants?

Posted 8 months ago
661 Views
|
2 Replies
|
3 Total Likes
|
 My question is about letting Mathematica automatically find the constants for the different pieces of a piecewise defined function. This works fine in case of 1D piecewise function, but not for a 2D function. At least I do not know what I'm doing wrong. Here is the integration of the 1D piecewise function that does work: tent[x_] = Piecewise[{{x + 1, -1 <= x < 0}, {-x + 1, 0 <= x <= 1}}] itent[x_] = Integrate[tent[x], x] itent evaluates nicely to: Piecewise[{ {0, x <= -1}, {1/2 + x + x^2/2, -1 < x <= 0}, {1/2 + x - x^2/2, 0 < x <= 1}}, 1] However when I apply (single) vertical integration on a 2D function the constants are not found anymore: tent2D[x_, y_] = tent[x]*tent[y] HyNoConst[x_, y_] = Integrate[tent2D[x, y], y] Have a look to the attache notebook file (Mathematica version 9). Here output of above HyNoConst is printed and plotted. It is clear that the pieces in the piecewise function are integrated as if they were separately integrated; without taking account for the integrated value of adjoining piecewise parts. Find at the bottom of the attached notebook the HyExpected, where I manually added the vertical integration constants, which I expect Mathematica to find automatically. Also a plot of HyExpected is shown which makes it clear why this is the expected outcome. Attachments:
2 Replies
Sort By:
Posted 2 months ago
 You expect Mathematica to find the continuation because Piecewise says in Examples -> Scope Integration constants are chosen to make the result continuous: which is correct in one dimension. In 2 or more dimensions two problems arise (as you found) the integral is given only on the actual support of the piecewise function it's generally not the job of indefinite Integrate[] to fix integration constants But one reaches the intended result without labour: In[1]:= tent[x_] := Piecewise[{{x + 1, -1 <= x < 0}, {1 - x, 0 <= x <= 1}}] In[2]:= tent2[x_, y_] := PiecewiseExpand[tent[x] tent[y]] In[3]:= Clear [stamm] stamm[x_, y_] := Evaluate[Integrate[tent2[x, y1], y1] /. y1 -> y] In[5]:= Clear[stammC] stammC[x_, y_] := stamm[x, y] - If[y < -1, 0, If[y < 1, stamm[x, -1], stamm[x, -1] - stamm[x, 1]]] In[8]:= Plot3D[PiecewiseExpand[stammC[x, y]], {x, -1.6, 1.6}, {y, -1.6, 2.6}, AxesLabel -> {"X", "Y", "Z"}] which is your Plot3D[HyExpected[x, y], {x, -1.6, 1.6}, {y, -1.6, 2.6}, PlotRange -> Full]:If one muses about it one will see that's in general not easy to find the intended integration constants (Piecewise[] can go in irregular shapes (i.e. not axis-parelle) etc. etc.), but the problem owner can do.
 Hi Dent,Thank you for your answer. I went through it. What I ment with "adding the constants manually" was actually resembling your approach; using the functions defined in the attached notebook, I recreated the "manual steps", being: HyWithFirstConstant[x_, y_] = HyNoConst[x, y] - Piecewise[{{HyNoConst[x, -1], -1 <= y <= 1}}]; HyWithAllConstant[x_, y_] = HyWithFirstConstant[x, y] + Piecewise[{{HyWithFirstConstant[x, 1], 1 < y}}]; Sorry for being unclear about what I ment with the manual step. And indeed this is doable (but I would not say "without labor", the number of piecewise parts might be way higher). What I've learned from your answer are two things: The use of PiecewiseExtend, which produces a flattened-condition form of the function that shows the "conditional-areas" in the function clearly; including condition-border mistakes I made. The use of "If" i.s.o. "Piecewise", which is shorter and seems less error prone regarding condition-borders. I do get your point in that it is not easy in general to find the intended integration constantants in 2D (or nD). However I do not see if the integration is simply along the y-axis, how this can lead to non-axis parallel constants.I still feel that the (rather mechanical an repetitive procedure when there are a lot of piecewice parts of the) manual steps that we seem to have do should be performed automatically by Mathematica. If the function would be to complex/irregular for Mathematica to do it automatically a certain feed back to the user should indicate it then (instead of not finding constants for simple cases). I'm curious if other symbolic solvers like Maple can derive the constants for 2D integrals in an automatic way.Thanks again for your answer, Koen