how to find intersection between 3 functions?

조회 수: 11 (최근 30일)
Eric
Eric 2016년 12월 16일
답변: Walter Roberson 2016년 12월 17일
Hello I have the following code which has 3 functions and plots all 3 of them. My question is is there anyway to get some type of mark on the graph that shows where all 3 functions intersect? also is there a way to change the color of the 3 functions? Thank you for your help.
[X,Y] = meshgrid(-10:0.2:10,-10:0.2:10);
Z1 = X.^2+Y-37;
Z2=X-Y.^2-5;
Z3=3-X-Y;
figure
surface(X,Y,Z1)
hold on
surface(X,Y,Z2)
surface(X,Y,Z3)
view(3)
xlabel('x')
ylabel('y')
zlabel('z')

답변 (2개)

John D'Errico
John D'Errico 2016년 12월 16일
Easier than you think.
First of all, if your goal is to plot three surfaces with different colors on each, then use handle graphics.
H1 = surface(X,Y,Z1);
hold on
H2 = surface(X,Y,Z2);
H3 = surface(X,Y,Z3);
set(H1,'facecolor','r')
set(H2,'facecolor','g')
set(H3,'facecolor','b')
Next, where do the surfaces intersect? First, find where surfaces 1 and 2 intersect, and plot that curve. Then find where surfaces 1 and 3 intersect, plotting that curve. Any point of intersection of those curves in the (x,y) plane MUST be a point of intersection of all three surfaces.
figure
contour(Z1-Z2,[0 0])
hold on
contour(Z1-Z3,[0 0])
contour(Z2-Z3,[0 0])
The implication being there are exactly two points where the three surfaces intersect. I could have introduced the contours of z2-z3 too. But the above plot is entirely adequate for this problem.
You can access the contour paths themselves, by use of the output arguments to contour, or you could have used the contourc function to give you the contour path without a plot at all.
Finally, find the intersection points along the paths using Doug Schwarz's nice tool - intersections, as found on the file exchange for download. Other tools are also on the FEX for this purpose.
  댓글 수: 3
John D'Errico
John D'Errico 2016년 12월 16일
편집: John D'Errico 2016년 12월 16일
You did not think about what I did here. Next time, try reading what I wrote. Then think about what you read.
Each of your functions is a function of TWO variables. The same two variables, (X,Y), over the same domain. The third variable, Z, is the dependent variable for each function.
What I showed you how to do locates the intersections, in the (x,y) plane. That location is in TWO DIMENSIONS. That is what contour does nicely. If you then want to identify those points on the surfaces, all three functions have the same value of Z at the located points, although that value need not be the same for each point.
So, again, think. You asked to find the point(s) where z1(x,y)=z2(x,y)=z3(x,y). I showed you exactly how to find those locations.
Eric
Eric 2016년 12월 17일
you should read what was asked for again what you did isn't what i want so its useless to me but thanks for the input

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2016년 12월 17일
syms X Y Z real
Z1 = X.^2+Y-37 == Z;
Z2 = X-Y.^2-5 == Z;
Z3 = 3-X-Y == Z;
sols = solve(Z1, Z2, Z3);
sols.X, sols.Y and sols.Z will then be the exact locations of intersection. For example one of the X coordinates is
(35*(294*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3) + 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3) + 30049)^(1/2))/(288*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/6)) - ((294*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3) + 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3) + 30049)^(1/2)/(6*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/6)) + (588*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3)*(294*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3) + 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3) + 30049)^(1/2) - 30049*(294*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3) + 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3) + 30049)^(1/2) - 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3)*(294*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3) + 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3) + 30049)^(1/2) + 1152*2^(1/2)*6^(1/2)*(288*3^(1/2)*32626735^(1/2) + 5937263)^(1/2))^(1/2)/(6*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/6)*(294*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3) + 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3) + 30049)^(1/4)) - 3)^3/96 - (3*((294*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3) + 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3) + 30049)^(1/2)/(6*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/6)) + (588*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3)*(294*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3) + 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3) + 30049)^(1/2) - 30049*(294*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3) + 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3) + 30049)^(1/2) - 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3)*(294*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3) + 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3) + 30049)^(1/2) + 1152*2^(1/2)*6^(1/2)*(288*3^(1/2)*32626735^(1/2) + 5937263)^(1/2))^(1/2)/(6*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/6)*(294*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3) + 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3) + 30049)^(1/4)) - 3)^2)/32 + (35*(588*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3)*(294*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3) + 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3) + 30049)^(1/2) - 30049*(294*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3) + 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3) + 30049)^(1/2) - 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3)*(294*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3) + 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3) + 30049)^(1/2) + 1152*2^(1/2)*6^(1/2)*(288*3^(1/2)*32626735^(1/2) + 5937263)^(1/2))^(1/2))/(288*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/6)*(294*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(1/3) + 9*((32*3^(1/2)*32626735^(1/2))/3 + 5937263/27)^(2/3) + 30049)^(1/4)) + 49/16
If you work this through... you will find that it gives the same answer that John D'Errico was suggesting.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by