How to plot a quadratic equation?

85 views (last 30 days)
Hello everybody!
I've been struggling with this for a while and I don't know how to do that..
I would like to plot this: x²+y²+z²-2xy-4x+4y-1=0
thanks for your help!

Accepted Answer

John Chilleri
John Chilleri on 9 Feb 2017
Edited: John Chilleri on 9 Feb 2017
Hello,
If you have a newer release of Matlab (I believe R2016b), you can use the new fimplicit3 function,
fimplicit3(@(x,y,z) x.^2+y.^2+z.^2-2*x.*y-4*x+4*y-1)
which will default to plotting on the interval [-5,5] (for all axes) but you can change this interval with the second input argument.
Hope this helps!
  3 Comments
John Chilleri
John Chilleri on 9 Feb 2017
Noted - I don't have R2016b, so I wasn't able to test it, but thank you, I'll change my answer!
Also, I just remembered how I learned of this - you can see a similar question and answer here provided by Benjamin Kraus.
Loris Chavée
Loris Chavée on 10 Feb 2017
Thanks for your help! It Will probably help me to pass my analytic geometry exam.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 9 Feb 2017
Edited: John D'Errico on 9 Feb 2017
It is not a quadratic equation in the classic use of that phrase. It is the implicit equation of a conic form in 3 three dimensions. The only thing quadratic about it is that there are squares of the variables in it.
I'll give you a hint though;
Can you isolate z easily?
z^2 = -x^2 - y^2 + 2*x*y + 4*x - 4*y + 1
If you take the sqrt, then there are TWO solutions. So, use meshgrid to generate a set (grid) of values for x and y.
Then compute the TWO possible values for z. for ANY value of x and y. For some of those combinations of x and y, there may be no real solution of course. But that is ok.
Use surf to plot the two surfaces created from those two branches of the sqrt. (Use hold on in between the calls to surf, to overlay the two surfaces in one plot.)
Really pretty easy. Looks like 6 lines of code. If you want to be more intelligent about it, you can use a little high school math to determine the intrinsic shape of that conic form in advance. That might give you a set of intelligent limits on x and y to put into meshgrid.
If you wanted to be REALLY lazy, you might see how the isosurface tool would give you the desired manifold directly. Well, it would take some work with meshgrid again.

Community Treasure Hunt

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

Start Hunting!