tags:

views:

73

answers:

2

why when i use ezplot in for example [1 1.5] interval, a discontinuity will appear in some pieces of lines but when i use a closer interval like [1.3 1.5], the discontinuity will be annihilated?

+1  A: 

EZPLOT is a general purpose plotting function that will automatically select a set of points at which to evaluate and plot the function you pass to it. Most of the time, things work fine. But there are some special cases where EZPLOT can have some trouble. It may not render well near discontinuities or points where there are rapid changes in the function (which it may mistake for a discontinuity).

That's the drawback of a function that is designed to be general enough to accept any function you give it: it's hard to make it general enough to handle everything exactly right, so some special edge cases look a little funny. In such cases, you should avoid functions like EZPLOT (which make a lot of choices for you) and plot things yourself by evaluating your functions at points you choose and plotting those points using the PLOT function. Here's a helpful link for this.

gnovice
+1  A: 

The problem is that ezplot() is useful, but not that robust.

A better option for plotting a function without discrete points is fplot(). Check out the documentation for it.

Here is an example of how to use it compared with ezplot():

lowerBound = 0;
upperBound = 1;

%# The ezplot way:
ezplot('y=sin(1/x)',[lowerBound,upperBound,-1,1])

%# The fplot way:
fplot('sin(1/x)',[lowerBound,upperBound])

fplot() will evaluate more points where the function changes more rapidly. Thus discontinuities will still cause problems in the graph if you look closely, but it will try harder to plot them accurately.

To plot a level curve of a function with three variables requires a little more typing:

%# First create a grid where you want the function to be drawn
[x,y]=meshgrid(-2:.01:2);
     %# Remember that -2:.01:2 creates a vector with values from -2 to 2
     %# in steps of .01

%# Then define your function
z=-3*y./(x.^2+y.^2+1);

%# Now graph the level curve of the function.  I chose the level z=0.5:
contour(x,y,z,[0.5])
James
but the two variables of my function are not seperable.how to use fplot for expressions like this: sqrt(x^-y^2)+cos(x*y)=0
Alireza
This is different than plotting a function. Your function is really of three variables: x, y, and z. Instead of plotting a 3D figure you are just holding z constant at 0. So you are plotting the level curve of f(x,y)=z at f(x,y)=0.
James
A contour plot is definitely the way to go for the example in the comment. However, I think your call to contour should be `contour(x,y,z,[0.5 0.5]);`
gnovice
`contour(x,y,z,[.5])` and `contour(x,y,z,[.5 .5]` gave the same graph for me. The second would maybe draw the same line twice?
James
@James: The documentation says that a scalar value for the fourth argument will be interpreted as a number of contour levels to generate, and that to generate a single contour level the argument should be a vector with that value repeated. Perhaps since the argument isn't an integer value, CONTOUR is able to interpret it as a contour level value instead of rounding it off to a number of contour levels.
gnovice
Please note that `.5` (scalar) is not the same as `[.5]` (vector). `contour()` evaluates the function along curves at every value in the vector. In this case, .5 is treated as both the first and last value of the vector.
James
@James: That is a misinterpretation. `0.5` and `[0.5]` *both* evaluate to a scalar (i.e. a 1-by-1 array). There is *no* differentiation between the two in MATLAB. If you type something like `[0.5]` in the MATLAB Editor, it will even give you a warning saying the square brackets are unnecessary. As I said above, CONTOUR may simply be smart enough to recognize a non-integer value as a contour level value as opposed to a number of contour levels.
gnovice
For me (r2009b) I get no warning about unnecessary brackets. Also, `contour(x,y,z,[2])` and `contour(x,y,z,2)` do not produce the same plot. Perhaps this is just a variation amongst revisions.
James
@James: I've been using R2009a and I haven't noticed *anything* like what you've found. If `2` and `[2]` really are represented differently in newer versions, that would represent a *significant* departure from the typical MATLAB behavior. Could you post the complete code you are using to illustrate the different plotting behavior?
gnovice

related questions