How to easily plot equations in one, two and multi variables
30 次查看(过去 30 天)
显示 更早的评论
Hi,
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:
- one variable
- two variables
- 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:
- Question1: How do I plot equations in one variable with conditions?e.g. how to plot y=x where x is even
- Question2: How do I plot equation of a circle (x^2+y@ = 4) without using ezplot?
- 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
ezplot(x^2+y^2==4,[-10,10,-10,10]);
However, it can not scale to multi variables and it can not plot inequality
ezplot(x^2+y^2<=4,[-10,10,-10,10]);
-- 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?
0 个评论
回答(1 个)
Prateek Khandelwal
2016-8-8
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:
fplot(@(t)(2*cos(t)),@(t)(2*sin(t)),[0,2*pi])
(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.
cheers.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!