# Calculating the expectation value of an observable over a finite region rather than an an entire distribution

Given an operator $$\hat{Q}(x)$$, the expectation value of the observable $$\langle Q \rangle$$ is calculated by integrating the probability distribution over the entire space as

$$$$\langle Q \rangle = \int_{-\infty}^{\infty} \psi^{*}(x) \hat{Q}(x) \psi(x) dx$$$$

Is it possible to calculate the expectation value $$\langle Q \rangle_{ab}$$ over just a finite range $$x \in [a,b]$$ such that

$$$$\langle Q \rangle_{ab} = \int_{a}^{b} \psi^{*}(x) \hat{Q}(x) \psi(x) dx.$$$$

It would seem that the answer is yes. For example, a detector in the lab usually takes measurements only in a finite space rather than over an infinite space. Furthermore, as as example, it seems that for a piece-wise potential that gives three separate wave functions $$\psi_{1}(x), \psi_{2}(x) \text{ and } \psi_{3}(x)$$, the above calculation would be performed due to

$$$$\langle Q \rangle = \int_{-\infty}^{a} \psi_{1}^{*}(x) \hat{Q}(x) \psi_{1}(x) dx + \int_{a}^{b} \psi_{2}^{*}(x) \hat{Q}(x) \psi_{2}(x) dx + \int_{b}^{\infty} \psi_{3}^{*}(x) \hat{Q}(x) \psi_{3}(x) dx.$$$$

The first question has positive answer. The second one has again positive answer if $$Q$$ preserves pairwise orthogonality of the three functions $$\psi_j$$. Roughly speaking, it happens if $$Q$$ commutes with the position operator.
The below seems to long to add as a comment, so, just trying to mathematically formalize the @Valter Moretti 's answer, namely, there is a useful approach consisting using Heaviside function. Precisely, the piece-wise your wave function is $$$$\psi(x) = \psi_1(x) H(a-x) + \psi_2 ( H(x-a) + H(x-b)) + \psi_3 H(x-b)$$$$ when calculating the expected value of $$\hat{Q}$$, it is seen that:
\begin{align} &\int\limits_{-\infty}^{\infty} \psi(x) \hat{Q} \psi(x) \mathrm{d} x = \int\limits_{-\infty}^{\infty} \left(\psi_1(x) H(a-x) + \psi_2 ( H(x-a) + H(x-b)) + \psi_3 H(x-b) \right) \hat{Q} \psi(x) \mathrm{d} x = \\ & = \int\limits_{-\infty}^{\infty} \psi_1(x) H(a-x) \hat{Q} \psi(x) \mathrm{d} x + \int\limits_{-\infty}^{\infty} \psi_2 ( H(x-a) + H(x-b)) \hat{Q} \psi(x) \mathrm{d} x \\ & + \int\limits_{-\infty}^{\infty} \psi_3 H(x-b) \hat{Q} \psi(x) \mathrm{d} x = \\ & = \int\limits_{-\infty}^{a} \psi_1(x) \hat{Q} \psi(x) \mathrm{d} x + \int\limits_{a}^{b} \psi_2 \hat{Q} \psi(x) \mathrm{d} x + \int\limits_{b}^{\infty} \psi_3 \hat{Q} \psi(x) \mathrm{d} x \end{align} Next, the $$\hat{Q}$$ operates somehow on the Heaviside function so if the function on the right commutes with the operator you can write the equation at the end of the question.