# How to calculate the intersection of two hyperplanes in MATLAB?

Assume you have two affine spaces defined as follows:

$$S_1: v^{*} + \sum \alpha_i \hat{v}_{i}$$

where $v^{*}$ is a vector in $R^n$ and $\alpha$'s are coefficients and $\hat{v}$'s are basis vectors for spanning.

and the second space is defined similarly

$S_1: u^{*} + \sum \beta_j \hat{u}_{i}$

So the question is, how to calculate the intersection of the two spaces in MATLAB. I know how to calculate it by hand: just set $S_1=S_2$ and derive some relationships for $\alpha$ and $\beta$.

The solution (or let's say the output of the matlab script) should be the intersection basis. That is,

$S_1\bigcap S_2 : z^{*} + \sum \gamma_k\hat{z}_k$.

So the output is vectors $z^{*}$ and $\hat{z}_k$. Of course it is not unique.

Let me make it absolutely clear. Here's an example:

$S_1 : \left( \begin{array}{c} 1\\ 1\\ 1 \\ \end{array} \right)+ \alpha_1\left( \begin{array}{c} 1\\ 2\\ 1\\ \end{array} \right)+\alpha_2 \left( \begin{array}{c} 1\\ 1\\ 2\\ \end{array} \right)$.

And

$S_2 : \left( \begin{array}{c} 0\\ 0\\ 1 \\ \end{array} \right)+ \beta_1\left( \begin{array}{c} 0\\ 1\\ 1\\ \end{array} \right)+\beta_2 \left( \begin{array}{c} 1\\ 0\\ 1\\ \end{array} \right)$.

Basically two planes, so the intersection would be a line. And when I work out (on paper!) I come up with the intersection (after row-elimination) :

$S_1\bigcap S_2 : \left( \begin{array}{c} 1\\ 0\\ 2\\ \end{array} \right)+ \gamma_1 \left( \begin{array}{c} 1\\ 1\\ 2\\ \end{array} \right)$.

How can you do the whole procedure automatically in Matlab?

• Create a matrix formulation of your solution process. The componenets of the basis vectors will form the elements of your matrices and your unknowns will involve $\alpha$'s and $\beta$'s. Your solutions will have some parameters in them so you will need to work symbolically.
– Paul
Oct 30, 2015 at 12:02
• @Paul that's exactly the procedure. But how to write it to matlab (or basically the algorithm) is the question. I mean the part to handle the parametric solution. Oct 30, 2015 at 12:04
• We expect to get the solution in the following easy form. something like: $S_1\bigcap S_2 : z^{*} + \sum \gamma_k\hat{z}_k$. Oct 30, 2015 at 12:07

Let U, V be the $n \times (n-1)$ matrices of the spanning vectors of $S_1$ and $S_2$ and let u0, v0 be the particular vectors that are also given. You can find a particular vector z0 in the intersection with the Matlab command

z0 = [U,V]*([U,V]\(v0 - u0))

and you can get a $n \times (n-2)$ matrix of spanning vectors of the intersection with the command

Z = null([null(U),null(V)]) .

• your proposal seems promising but unfortunately didn't work when I tried. Note that my spaces are affine so there's a shift by $v^*$ ( and $u^*$). And what are the spanning vectors you mean? Do you mean $\hat{v}_k$'s (and $\hat{u}_k$'s) Oct 30, 2015 at 12:27

you can do something similar to bellow

    s1 = [1 1; 2 1; 1 2;];
s1 = s1';
n1 = null(s1);
x1 = [1; 1; 1];

s2 = [ 0  1; 1 0; 1 1;];
s2 = s2';
n2 = null(s2);
x2 = [0; 0; 1];

drawVector(n1, {'n1'});
hold on
drawSpan(s1', 'r');

drawVector(n2, {'n2'});
drawSpan(s2', 'r');

n3 = null([n1 n2]')
drawVector(n3, {'n3'});


For visualization, I used: drawLA