0
$\begingroup$

I am trying to integrate a hat function for a project that I am doing and have found a method to do so but I find it sloppy. Currently I have the basis function

\[Psi][z_] := z - Subscript[Z, i]/ \[CapitalDelta]z + 1;

which I am trying to integrate from $z_{i-1}$ to $z_{i+1}$. I break the basis function up into two pieces and integrate the left side from $z_{i-1}$ to $z_{i}$ and then the right side from $z_i$ to $z_{i+1}$. My first question is, is there a way to integrate piecewise functions? The second question I have is, is there a way to set global assumptions like $z_{i-1} < z_i < z_{i+1}$, $z_i - z_{i-1} = \Delta z$ , etc?

Edit: This is the piece wise function taken directly from my code I am trying to integrate

\[Psi][z_, c_] :=  Piecewise[{{(z - c)/\[CapitalDelta]z + 1, 
 z <= c}, {-(z - c)/\[CapitalDelta]z + 1, z > c}}];

where $c$ is the center of the hat function. Here is my attempt to integrate the piece wise function

 FullSimplify[
 Integrate[\[Psi][z, Subscript[Z, i]], {z, Subscript[Z, i - 1], 
   Subscript[Z, i + 1]}], 
 Assumptions -> {-(Subscript[Z, i + 1] - Subscript[Z, 
       i ]) == -\[CapitalDelta]z, 
   Subscript[Z, i + 1] - Subscript[Z, 
     i ] == \[CapitalDelta]z, -(Subscript[Z, i] - Subscript[Z, 
       i - 1 ]) == -\[CapitalDelta]z, 
   Subscript[Z, i] - Subscript[Z, i - 1 ] == \[CapitalDelta]z}]

I do not get a usable answer. Am I doing something wrong (ie can one integrate a piece wise function)?

$\endgroup$
4
  • $\begingroup$ Why are using using global variables not good? I would never know the value of $Z_i$ in the scope of this project and $\Delta z$ can have many values so I don't really see how else I could express the function. $\endgroup$
    – rioneye
    Feb 13, 2013 at 4:58
  • 1
    $\begingroup$ Have you looked at Piecewise? Did you know that Integrate can take symbolic values (such as Subscript[z,i-1]) in its integration limits? $\endgroup$
    – Xerxes
    Feb 13, 2013 at 5:46
  • $\begingroup$ @Nasser: Thanks for the tip! However, the value of $Z_i$ is some undeclared constant in my code, as are $Z_{i+1}$, $Z_{i-1}$. $\endgroup$
    – rioneye
    Feb 13, 2013 at 22:38
  • $\begingroup$ @Xerxes: Yes I have. See the update. $\endgroup$
    – rioneye
    Feb 13, 2013 at 22:38

2 Answers 2

3
$\begingroup$

Short answer: Mathematica has no problem integrating piecewise or hat functions.

Your notation seems to me to be needlessly complex. Why bother to define $Z_i$ when it's just $Z_0+i\Delta z$? Isn't your $\psi$ just 1-Abs[z-c]/Δz? However, I'll try to adhere to the spirit of your notation. Here's some code that works for me:

Zdefs = {Subscript[Z, i] - Subscript[Z, i - 1] == Δz, 
   Subscript[Z, i + 1] - Subscript[Z, i] == Δz};
Integrate[ψ[z, Subscript[Z, i]],
   {z, Subscript[Z, i - 1], Subscript[Z, i + 1]} /. 
  Solve[Zdefs, {Subscript[Z, i + 1], Subscript[Z, i - 1]}][[1]], 
 Assumptions -> Δz > 0 && 
   Subscript[Z, i] ∈ Reals]
$\endgroup$
2
  • $\begingroup$ Thanks a bunch Xerxes! I was emulating the notation used in our analytical solution. It makes the code easier to compare to our results. I do have a few questions for you though. I don't understand why you use this : /. Solve[Zdefs, {Subscript[Z, i + 1], Subscript[Z, i - 1]}][[1]] ? Also what does this [[1]] mean? Sorry if these questions are especially basic. $\endgroup$
    – rioneye
    Feb 13, 2013 at 23:32
  • 1
    $\begingroup$ Since your definitions of $Z_{i\pm 1}$ can be reduced to functions of $Z_i$ and $\Delta z$, Solve will do that for us. The [[1]] tells Mathematica to take the first (and only) solution to those equations. You don't have to do that; you can instead add Subscript[Z, i - 1] < Subscript[Z, i] < Subscript[Z, i + 1] to your Assumptions. But then you'll have to make the substitution later anyway. $\endgroup$
    – Xerxes
    Feb 13, 2013 at 23:39
2
$\begingroup$

Your integral would have worked too, if you had added assumptions that all variables are real:

(* New assumptions so Integrate can do its work *)
Assuming[{z \[Element] Reals, 
  c \[Element] Reals, \[CapitalDelta]z \[Element] Reals, 
  Subscript[Z, i - 1] \[Element] Reals, 
  Subscript[Z, i + 1] \[Element] Reals},

 (* The rest is just copied from the question *)
FullSimplify[
 Integrate[\[Psi][z, Subscript[Z, i]], {z, Subscript[Z, i - 1], 
   Subscript[Z, i + 1]}], 
 Assumptions -> {-(Subscript[Z, i + 1] - Subscript[Z, 
       i ]) == -\[CapitalDelta]z, 
   Subscript[Z, i + 1] - Subscript[Z, 
     i ] == \[CapitalDelta]z, -(Subscript[Z, i] - Subscript[Z, 
       i - 1 ]) == -\[CapitalDelta]z, 
   Subscript[Z, i] - Subscript[Z, i - 1 ] == \[CapitalDelta]z}]]

Result:

$\begin{array}{cc} \{ & \begin{array}{cc} \frac{3 \left(Z_i-Z_{i+1}\right){}^2}{\text{$\Delta $z}} & Z_i>Z_{i+1} \\ \frac{\left(Z_i-Z_{i+1}\right){}^2}{\text{$\Delta $z}} & Z_i<Z_{i+1} \\ \end{array} \\ \end{array}$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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