I am developing a code where I am using the least squares method to compute gradients.
Generally, we use least squares to obtain some model based on a set of data (${q_1 \cdots q_N}$) at locations (${x_1 \cdots x_N}$), and use this model to predict $q_c$ at $x_c$.
In my case, I am looking for $\nabla{q}$ at $x_c$ using least squares. Should the $q$ value at $x_c$ be included in a least squares model for its gradient at the location, or only the neighboring values?
I would like use the weighted least squares method. It seems the weighting matrix is defined as a diagonal matrix with the inverse of the variance squared along the main diagonal. The popular choice for the variance seems to be the distance between the neighboring data point and the point in question. So if I include the $q_c$ value at $x_c$ in my least squares model, then the variance would be zero for this data point, which would be problematic unless I add some $\epsilon$ value to the variance before taking it's inverse.
The least squares equation is
\begin{align} \nabla q = q^TZM^T\nabla\phi(x) \\ \\ M=(Z^TZ)^{-1}\\ \\ \phi = \begin{bmatrix} 1 & x & y & z & xy & xz & yz & 0.5x^2 & 0.5y^2 & 0.5z^2 \\ \end{bmatrix}\\ \\ Z_{unweight} = \begin{bmatrix} 1 & x_1 & y_1 & z_1 & x_1y_1 & x_1z_1 & y_1z_1 & 0.5x_1^2 & 0.5y_1^2 & 0.5z_1^2 \\ \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots &\vdots & \vdots \\ 1 & x_N & y_N & z_N & x_Ny_N & x_Nz_N & y_Nz_N & 0.5x_N^2 & 0.5y_N^2 & 0.5z_N^2 \\ \end{bmatrix} \\ \\ q = \begin{bmatrix} q_1 & \cdots & q_N \end{bmatrix} \end{align}
$q_c$ is known, but I am not sure if this should be included in the above model.