Intersection of two planes.

what is the code to find the intersection of the plane x+y+z=3 and x+2y+2z=4.? I have this:
clc
clear
format compact
syms x y z
ekv1=x+y+z==3
ekv2=x+2*y+2*z==4

 Accepted Answer

Try this:
syms x y z
ekv1=x+y+z==3
ekv2=x+2*y+2*z==4
xyz = solve([ekv1, ekv2])
x = xyz.x
y = xyz.y
x =
2
y =
1 - z
Your system is under-determined (three variables and two equations), so there are potentially an infinity of solutions.

More Answers (2)

Jan
Jan on 9 Nov 2017
Edited: Jan on 9 Nov 2020
Do you want to solve this symbolically? It is easy to do it numerically (or with pencil and paper):
Start with converting the plane to the normal form:
v1 = [1;1;1];
c1 = 3;
v2 = [1;2;2];
c2 = 4;
n1 = v1 / norm(v1);
d1 = c1 / norm(v1);
n2 = v2 / norm(v2);
d2 = c2 / norm(v2);
Now the line is in parameter form:
r = q + t * cross(n1, n2);
with q is a point on the line:
n12 = n1.' * n2;
q = (n1 * (d1 - d2 * n12) + n2 * (d2 - d1 * n12)) / (1 - n12^2)

6 Comments

Thanks for this solution! Helped me a lot :) Unfortunately I'm dealing with singularity of n12 matrix and than q is NaN or Inf. Any solution to this? And where equation for q came from? I know it's basic algebra, but I somehow don't see it...Thanks in advance!
Jan
Jan on 6 Apr 2018
Edited: Jan on 9 Nov 2020
n12 is a scalar, not a matrix. Then for n12=0 the value of q gets:
q = (n1 * (d1 - d2 * n12) + n2 * (d2 - d1 * n12)) / (1 - n12)
= (n1 * d1 + n2 * d2) / 1.0
There is no source of Infs or NaNs. Please post the data of your problem and your code.
Thank you! Now I see my problem...my v1 and v2 vectors were transposed :D
I have tried to compare both solutions: symbolic and yours, and I think you made a mistake. There is an * after the first d1. Can you review? Thank you in advance you are a great user here.
Formula fo q is wrong. Right formula
q = (n1 * (d1 - d2 * n12) + n2 * (d2 - d1 * n12)) / (1 - n12^2)
Can check that
n1'*q-d1 = 0
n2'*q-d2 = 0
Thanks Sergey, I've fixed my answer.

Sign in to comment.

John D'Errico
John D'Errico on 6 Apr 2018
Edited: John D'Errico on 6 Apr 2018
Let me explain in some depth. You have two non-parallel planes.
So they will intersect in a line. Any point on that line is a solution, so there will be infinitely many solutions.
So first, see how to solve it by hand. Just subtract the two equations. That results in
y + z = 1
This is just a diagonal line in the (y,z) plane.
If we now subtract that from equation 1, we get
x = 2
And we can rewrite the line equation as
y = 1 - z
So the general solution, parameterized in terms of z is the triad
(x,y,z) = (2,1-z,z)
That is a line. Pick any value for z, and we get a point on that line.
You can use solve to give the solution.
syms x y z
ekv1=x+y+z==3
ekv2=x+2*y+2*z==4
xyz = solve(ekv1,ekv2)
xyz =
struct with fields:
x: [1×1 sym]
y: [1×1 sym]
Look at the solution it gave us:
xyz.x
ans =
2
xyz.y
ans =
1 - z
Look back at what I did before by hand. The result is exactly the same.
Ok, could I have gotten the same result using linear algebra? Of course. You have the linear system of equations...
A = [1 1 1;1 2 2];
b = [3;4];
You want to solve the problem
A*X = b
for a 3x1 vector X.
We can use backslash to find a particular solution. Thus:
X0 = A\b
X0 =
2
1
0
But that is only ONE specific solution. The complete solution is a line. We can add any multiple of 0 to a solution. In terms of linear algebra, the result is a line. We write a line in the form
X0 + t*Xnull
for some parameter t, and where Xnull is given as
Xnull = null(A)
Xnull =
-8.7561e-17
-0.70711
0.70711
Xnull is a vector such that when we multiply A*Xnull, we get a vector of zeros! Test that out to convince you.
A*Xnull
ans =
0
0
So the line is just the locus of points
[2;1;0] + t*[0;-1;1]
If you inspect that, you will in fact see is it identical to the other two solutions, except that here I used t as the line parameter. A nice thing is the linear algebra solution is extensible to much larger problems.
For example, try this one:
A = rand(5,7)
A =
0.84072 0.34998 0.35166 0.28584 0.075854 0.12991 0.16218
0.25428 0.1966 0.83083 0.7572 0.05395 0.56882 0.79428
0.81428 0.25108 0.58526 0.75373 0.5308 0.46939 0.31122
0.24352 0.61604 0.54972 0.38045 0.77917 0.011902 0.52853
0.92926 0.47329 0.91719 0.56782 0.93401 0.33712 0.16565
b = rand(5,1)
b =
0.60198
0.26297
0.65408
0.68921
0.74815
So now we have 7 unknowns, in 5 equations. The solution locus will be a two (7-5=2) dimensional space that lives in R^7.
X0 = A\b
X0 =
0.74439
0
-0.32714
0
0.40806
-0.42166
0.70921
Xnull = null(A)
Xnull =
-0.16647 0.24429
0.59226 -0.33548
-0.19255 -0.15678
-0.17916 -0.61558
-0.05193 0.24935
0.70681 0.31961
-0.22375 0.50983
The completely general solution is just:
X0 + Xnull(:,1)*s + Xnull(:,2)*t
Thus for ANY pair of parameters s and t, this is the solution. For example, pick s=2, t=5. Does it solve the problem?
X0 + Xnull(:,1)*2 + Xnull(:,2)
ans =
0.65576
0.84904
-0.86902
-0.9739
0.55356
1.3116
0.77154
A*X0 - b
ans =
-1.1102e-16
5.5511e-17
2.2204e-16
1.1102e-16
0
As you can see, it does. Well, except for some floating point trash.
For those of you who are more comfortable with differential equations, you can think of it as solving an ODE problem. X0 is the particular solution, for the linear operator A. Xnull is the solution (or set of solutions) to the homogeneous problem. The combination is the general solution.
Finally, in the case where the matrix A is not full rank, then there will in general be no exact solution. An exact solution will exist only when
rank(A) == rank([A,b])

5 Comments

Is it going to work also with general surfaces of n-th order?
And can I solve it with vectors (as answered by Jan)? I can take two normal vectors and get cross product vector (= direction of intersection line) and then get just some point of intersection to locate the line. Why am I still getting n12=n1.'*n2 as a singular matrix?
Are you asking about two completely general nonlinear surfaces? In n dimensions? Of course not. But then if you have nonlinear surfaces, none of the answers posed here will suffice.
Are you asking how to solve a problem with two LINEAR equations in n-dimensions? Of course it works. I showed how to solve a fully general linear problem.
Just write the problem in matrix form, then do as I did. The intersection of two LINEAR equations in n-dimensions will be a subspace of dimension n-2. So there will be n-2 unspecified parameters that you can choose freely.
As far as why Jan's solution did not work for you, I have no idea what you did. I can guess that you did not follow Jan's solution carefully, because as he wrote it, n12 will be a scalar, NOT a matrix.
ZuzkaT
ZuzkaT on 7 Apr 2018
Edited: ZuzkaT on 7 Apr 2018
So is there any solution for nonlinear cases? I probably won't need it, I am just curious :) I am working on point clouds filtering with RMLS algorithm and trying to solve filtering of sharp features (edges or corners) and my general approximation (regression) of pc can be nonlinear.
In the completely general case? No, not really.
If you are asking for two general nonlinear functions, how to solve this problem? Thus,
f(X) = 0
g(X) = 0
where X is a vector of length more than 2? So you have n>2 unknowns. Then the solution locus will usually be some nonlinear n-2 dimensional manifold, if any solution exists. (Yes, I know that sounds impressive. In the case of 2 equations in three unknowns, the resulting 3-2 dimensional manifold will be some path through your space, so some usually curved line.)
You might be lucky, IF the functions f and g are simple enough. But things are rarely simple in any kind of real problem. For example, you might be able to do as I did in my first solution by paper. Thus subtracting the two equations allowed me to eliminate one variable (x), thus deriving the manifold of interest.
There are often things you can do. But no simple approach will work for all problems when nonlinear relationships are involved.

Sign in to comment.

Categories

Products

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!