# How do I plot a system of equations onto a single plot?

21 views (last 30 days)
Marc Adams on 6 Feb 2018
Commented: Walter Roberson on 6 Feb 2018
So I have multiple equations that I need to plot on a single 3d plot. They can all be expressed as Z=f(x,y), and I know I can use surf(X,Y,Z) to plot one, but how do I put multiple equations on a single plot?
Also, is it possible to somehow highlight the intersection of these equations or make the non-intersecting parts of the system invisible?

Birdman on 6 Feb 2018
Edited: Birdman on 6 Feb 2018
You may use hold on command between surf commands.
.
.
surf(X,Y,Z1);
hold on;
surf(X,Y,Z2)
.
.
Something like above. A numeric example:
Z=@(x,y) x.^2+y.^3;
T=@(x,y) -x.^3+y.^2;
[X,Y]=meshgrid(1:5,1:5);
zFun=Z(X,Y);
tFun=T(X,Y);
surf(X,Y,zFun);hold on;surf(X,Y,tFun)

Walter Roberson on 6 Feb 2018
Use
hold on
after the first one.
I recommend that you add the option 'edgecolor', 'none' to the surf() call.
"Also, is it possible to somehow highlight the intersection of these equations or make the non-intersecting parts of the system invisible?"
No. You would need to compute the intersections and draw them. That can be difficult for non-linear functions.
Walter Roberson on 6 Feb 2018
Sometimes the easiest approach to computing the intersections is to create a 3D "occupancy" array. You divide up the x range and the y range into subsets, and divide the potential z range into fixed subsets. Then for each x and y combination from the subsets, you calculate the corresponding z value and you quantize that z value as one of the potential subsets, take the index of that, and you set the 3D array to 1 at that combination, leaving all of the other locations as 0. Then you go through the same set of x and y combinations for the second function, but this time at the locations, you add 1 instead of setting the array to 1. Once you have done this, you can then search for the array locations where the count is more than 1: somewhere in each of those voxels is probably an intersection.
You can then optionally use the x and y and z represented by the voxel locations as starting points for a search for simultaneous solutions, hoping that the position information is close enough to be within the "basin of attraction" of solutions for whatever search function you are using -- you would do this if you needed to know more exactly where the intersection point is, or if you needed to verify that there is actually an intersection there.
It is possible that there is no actual intersection. Consider for example that your two curves could be two branches of a hyperbola
You can bring the two curves arbitrarily close together without them touching, so given any particular non-zero quantization in x, y, z, the approach I set out above could end up with false hits for intersections.
If your equations are implicit equations, you can set up an x y z volume similar to what I describe above, and then you can evaluate the functions at each combination and test to see whether the equation holds to within tolerance, adding 1 where it is for each function, and again test for locations > 1 afterwards to detect the intersections.
This general method of quantization of locations can be used with non-uniform spaces, but if you do that then you should probably adjust your notion of "within tolerance".
Quantizing the range like this is unfortunately not reliable for functions with rapidly increasing oscillation frequency such as sin(exp(x))