I have a question when I view other's matlab FEM code.
You know, after generating global stiffness matrix, we should apply boundary conditions to the stiffness matrix and force vector. Specifically, if the n degree of freedom is fixed with zero general displacement(displacement, rotation,etc.), then I have to process the global stiffness matrix K in the following way(Matlab).
K(n,:) = 0;
K(:,n) = 0;
K(n,n) = 1;
F(n,1) = 0;
This is the theoretical way. Obviously, when we process the global stiffness matrix, we should consider the value of each elements in order to avoid singular stiffness matrix. And following is other's code
K(n,:) = 0
K(:,n) = 0
K(n,n) = bcwt*speye(length(n)); (You can just regard it as K(n,n) = bcwt )
F(n,1) = bcwt*0;
where bcwt = trace(K)/N, N is the dimension of K. I know it is effect on avoiding singular stiffness matrix, but I want to know Why. Why use this bcwt instead of others, and if there are other choices.
Okay let's consider 1D FEM with linear elements (basis functions), the conceptually easiest set-up. For $N$ nodes with one degree of freedom (1D) your stiffness matrix $K$ is in $\mathbb{R}^{N \times N}$ and displacements $\boldsymbol{u}$ as well as forces $\boldsymbol{f}$ both in $\mathbb{R}^N$.
Now assume you now the k'th displacement. First thing to know is that you cannot prescribe simultaneously the force at that node, otherwise you have an ill-posed problem, with likely no solution for general $u_i, f_i$. But okay, lets assume we have $u_i, \boldsymbol{f} \setminus f_i$ and wish to know $f_i$ and $\boldsymbol{u} \setminus u_i$.
Let's start by looking at out-of-the-box formulation of the system: $$ K \boldsymbol{u} = \boldsymbol{f} = \begin{pmatrix} k_{1,1} & \dots & k_{1, i-1} & k_{1, i} & k_{1, i + 1} & \dots & k_{1, N}\\ \vdots & \dots & \vdots & \vdots & \vdots & \dots & \vdots \\ \dots & \dots & k_{i - 1, i-1} & k_{i - 1, i} & k_{i - 1, i + 1} & \dots & \vdots\\ \dots & \dots & k_{i, i-1} & k_{i, i} & k_{i, i + 1} & \dots & \vdots \\ \dots & \dots & k_{i + 1, i-1} & k_{i + 1, i} & k_{i + 1, i + 1} & \dots & \vdots \\ \vdots & \dots & \vdots & \vdots & \vdots & \dots & \vdots \\ k_{N,1} & \dots & k_{N, i-1} & k_{N, i} & k_{N, i + 1} & \dots & k_{N, N} \end{pmatrix} \begin{pmatrix} u_1 \\ \vdots \\ u_{i-1} \\ u_i \\ u_{i + 1} \\ \vdots \\ u_N \end{pmatrix} = \begin{pmatrix} f_1 \\ \vdots \\ f_{i-1} \\ \color{red}{f_i} \\ f_{i + 1} \\ \vdots \\ f_N \end{pmatrix}$$
Then we have to find a way to modify our system $K \boldsymbol{u} = \boldsymbol{f}$ such that we have only known quantities in the RHS, i.e., we have to get rid of $f_i$. This can be done by removing the $i$'th row from the linear system:
$$\begin{pmatrix} k_{1,1} & \dots & k_{1, i-1} & k_{1, i} & k_{1, i + 1} & \dots & k_{1, N}\\ \vdots & \dots & \vdots & \vdots & \vdots & \dots & \vdots \\ \dots & \dots & k_{i, i-1} & k_{i, i} & k_{i, i + 1} & \dots & \vdots \\ \dots & \dots & k_{i + 1, i-1} & k_{i + 1, i} & k_{i + 1, i + 1} & \dots & \vdots \\ \vdots & \dots & \vdots & \vdots & \vdots & \dots & \vdots \\ k_{N,1} & \dots & k_{N, i-1} & k_{N, i} & k_{N, i + 1} & \dots & k_{N, N} \end{pmatrix} \begin{pmatrix} u_1 \\ \vdots \\ u_{i-1} \\ \color{red} {u_i} \\ u_{i + 1} \\ \vdots \\ u_N \end{pmatrix} = \begin{pmatrix} f_1 \\ \vdots \\ f_{i-1} \\ f_{i + 1} \\ \vdots \\ f_N \end{pmatrix}$$ Note that now $K \in \mathbb{R}^{\color{red}{N - 1}, N}, \boldsymbol{f} \in \mathbb{R}^{\color{red}{N -1}}$, but still $\boldsymbol{u} \in \mathbb{R}^{\color{red}{N}}$! (Matrix multiplication: $\text{Row} \times \text{Column}$). This system is underdetermined, unless we impose our knowledge of $u_i$. Note that for every $j$ it holds (writing out the matrix multiplication): \begin{align}\sum_{l = 1}^N k_{j, l} u_l & = f_j \\ \Rightarrow \sum_{l \neq i }^N k_{j, l} u_l & = f_j - k_{j, i} u_i \end{align} This idea is now utilized to transform our system into $$\begin{pmatrix} k_{1,1} & \dots & k_{1, i-1} & k_{1, i + 1} & \dots & k_{1, N}\\ \vdots & \dots & \vdots & \vdots & \dots & \vdots \\ \dots & \dots & k_{i, i-1} & k_{i, i + 1} & \dots & \vdots \\ \dots & \dots & k_{i + 1, i-1} & k_{i + 1, i + 1} & \dots & \vdots \\ \vdots & \dots & \vdots & \vdots & \dots & \vdots \\ k_{N,1} & \dots & k_{N, i-1} & k_{N, i + 1} & \dots & k_{N, N} \end{pmatrix} \begin{pmatrix} u_1 \\ \vdots \\ u_{i-1} \\ u_{i + 1} \\ \vdots \\ u_N \end{pmatrix} = \begin{pmatrix} f_1 \\ \vdots \\ f_{i-1} \\ f_{i + 1} \\ \vdots \\ f_N \end{pmatrix} \color{red}{ - u_i \begin{pmatrix} k_{1, i} \\ \vdots \\ k_{i - 1, i} \\ k_{i + 1, i} \\ \vdots \\ k_{N, i} \end{pmatrix}} $$
Now we have again a nice square system with $N-1$ unkowns, giving us $\boldsymbol{u} \setminus u_i$. Once we solved that system, we can compute $f_i$ from the formula already posted above: $$ \sum_{l = 1}^N k_{i, l} u_l = f_i $$