MATLAB Answers

How to easily plot equations in one, two and multi variables

339 views (last 30 days)
Saurabh Mehta
Saurabh Mehta on 16 May 2016
This might be a simple question to ask, but the idea for me is to understand plotting of equations (not functions..but expressions with (in)equality sign) in 3 cases:
  1. one variable
  2. two variables
  3. more than two variables
I've studied several posts, did several experiments but dont get a full picture. So please help.
In summary, my questions are to know simple ways for quick plotting in these cases:
  1. Question1: How do I plot equations in one variable with conditions?e.g. how to plot y=x where x is even
  2. Question2: How do I plot equation of a circle (x^2+y@ = 4) without using ezplot?
  3. Question3: How do I plot equations in multiple variables like (x^2+y^2+z^2 == 100) or (x^2+y^2+z^2 <= 100)?
One variable:
  • Its possible to plot function of one variables using fplotfplot(@(x) (x^2), [-20,10])
But not possible to:
  • plot function in 2 or more variables using fplot (as per documentation)
  • plot equalitiesfplot(@(x) x/2==0, [-20,20]);
(The statement also looks weird to me and ofcoure has no result)
  • plot inequalities (y = x if x>0)fplot(@(x) x>=0, [-20,20]);
(Again it also looks weird to me and has no result)
Two variables
Say,I want to plot an equation of a circle x^2 + y^2 = 4 It is possible if I use ezplot which supports even two variable equations. But Matlab says it is deprecated.
syms x y
However, it can not scale to multi variables and it can not plot inequality
-- error
Is there a simple and intuitive way or do i need to go to surf and mesh?
More than two variables
Here also, how to plot equations?
x=[10,10]; y=[-10,10]; [X,Y]=meshgrid(x,y); Z=(X.^2+Y.^2)<=2
this obviously will not work. Then how?

Answers (1)

Prateek Khandelwal
Prateek Khandelwal on 8 Aug 2016
Hi Siddharth,
As far as the question of plotting equations (not inequalities) is concerned, it is possible to plot 2d and 3d functions using fplot and fplot3 function calls, provided you can comeup with a parameteric representation of the equation.
Taking the example of plotting the circle, say, x^2 + y^2 = 4, the following function call would do the job:
(since each point on the circle can be represented as (cos(theta),sin(theta)).)
Similar appraoch should work for the case of 3D curves too.
About plotting inequalities, I am not sure whether an approach of plotting directly using function exists, however, you can use patch to create the faces provided you know the bounds of the axes and the individual vertices on the curve.
Here are some references that you might find useful.
1. ineqplot: "Plotting inequalities, simple and easy" ( ineqplot )
2. Plot 2D/3D regions: for plotting 2D/3D regions ( plot 2d/3d regions )
3. plot::inequalities: "Display areas where inequalities are fulfilled" ( plot::inequalities )

Community Treasure Hunt

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

Start Hunting!