# Solution of 3d linear system

Hello, good morning.
Im trying to solve this 3d system, that will plot a plane in surface. I have a matlab book and I’m porting some exercises.

The solution is a line between the two planes as the book shows:

In my code I did this way:

``````x = y = collect(-5:.5:5);
f(x, y) = 2x - 3y + 2;
g(x, y) = -2x + 3y;

gui(surface!(x, y, g, display_option=Plots.GR.OPTION_SHADED_MESH, camera=(3, 3)))
``````

But in my results the planes dont overlap showing the intersect line that is the solution of system

I can’t run the code (missing some using lines perhaps?).
It looks like you need to add transparency though.

1 Like

I just added LinearAlgebra, Plots and Im running on repl. I reset my repl and used the same code here

Using the camera seems the solution is not correct.

What do you mean by “solution” here? You are not actually solving the system (unless you want to trace out the line by hand on your screen…) - maybe a more descriptive title would be better.

It doesn’t seem to me that there’s anything wrong in what Plots is doing here, but rather that Matlab (or whatever produced the plot in the book) is doing something more complicated: the plane which is increasing in z from left to right is plotted below the other plane on the left hand side of the figure, but plotted above on the right hand side. I don’t think you can achieve this automatically, maybe you have to plot one plane first, then the second over it, and then those bits of the first again which should be visible above the second?

1 Like

The solution is only the intersection of the two planes. The matlab code is quite simple:

``````[x,y] = meshgrid(-5:0.5:5);
z = 2*x - 3*y + 2;
surf(x,y,z)

hold on
z = -2*x + 3*y;
surf(x,y,z)
``````

I dont know if there are more behind the scenes. Just the linear system and the functions. Matlab only plots one plane after another. I think I did the same.

or maybe the difference is the meshgrid function?

You might also consider using a different plotting backend; these will give different looking results based on which one you use. For example, using PlotlyJS:

``````using Plots; plotlyjs()
x = y = collect(-5:0.5:5)
f(x, y) = 2x - 3y + 2
g(x, y) = -2x + 3y

surface(x, y, f)
surface!(x, y, g)
``````

This will allow you to move the camera at your will and get plots that look similar to what you desire (by clicking and dragging).

If you haven’t installed the PlotlyJS backend yet, you will have to do `] add PlotlyJS` first.

This is an example of the output:

1 Like

That worked,

but Im thinking about gr backend. Why it cant cross the two planes. I had already changed the view using camera=(x, y, z)… but the planes aparent are apart.

Well, thats it. I don’t know why gr cant draw this, but plotly works fine.

I agree that it’s challenging to get something nice looking with GR. I’ve been playing around with the camera and the alpha of each curve, and the axes aren’t quite cooperating…

``````using Plots; gr()
x = y = collect(-5:0.5:5)
f(x, y) = 2x - 3y + 2
g(x, y) = -2x + 3y

surface(x, y, f, alpha = 0.7, camera = (-5, -7))
surface!(x, y, g, alpha = 0.9)
``````

produces

which is not quite as nice as what PlotlyJS produces. One of the reasons PlotlyJS is nice in my opinion is that it allows for intuitive camera movement with the click-and-drag, and the axes don’t get jumbled up.

1 Like

Yes, for this solution really gr not worked for me. Thanks for the help. It worked flowlessly.

1 Like

It might be easier to see the intersection if you colored
the surfaces by the value of f-g rather than f and g.

fill_z seems to be the attribute but I don’t know how
one would use it.

1 Like

You can also do a `wireframe` on top of the `surface`:

``````using Plots; plotlyjs()
x = y = collect(-5:0.1:5)
f(x, y) = 2x - 3y + 2
g(x, y) = -2x + 3y

surface(x, y, f)
surface!(x, y, g)
wireframe!(x, y, f)
wireframe!(x, y, g)
``````

2 Likes

It really doesnt cut another plane

For a better display, you could also plot the intersection line:

Plots.jl plotly code
``````using Plots; plotly(size=(600,600))

p1(x,y) =  2x - 3y + 2
p2(x,y) = -2x + 3y

l0(x) = (1 + 2x)/3      # solve for y
x = y = -5:0.01:5
surface( x, y, p1, alpha=0.8);
surface!(x, y, p2, alpha=0.8);
plot!(x, l0.(x), p1.(x, l0.(x)), c=:red, lw=3)
``````
2 Likes