简体   繁体   中英

Plot a complex function in Matlab

Could you please help me figure out how I can get a figure of the following function?

 (y - x^2)(2x^2 - y) >= 0

x >= 0

If you want to achieve that plot in MATLAB, simply define a meshgrid of coordinates and limiting the range of the x and y values to some predetermined limits. You'd simply substitute each pair of x and y values in the above equation and plot a map that tells you which values of x and y satisfy that inequality.

Something like this:

xmax = 5; ymax = 5; step_size = 0.01;
xval = -xmax:step_size:xmax;
yval = -ymax:step_size:ymax;
[x,y] = meshgrid(-xmax:step_size:xmax, -ymax:step_size:ymax);
z = ((y - x.^2).*(2*x.^2 - y) >= 0) & (x >= 0);
figure; 
imagesc(z); colormap gray; shading interp; axis xy;
set(gca, 'XTickLabel', xval(101:100:end));
set(gca, 'YTickLabel', yval(101:100:end));
xlabel('x'); ylabel('y');

The first line establishes the boundaries of the x and y values that you want to plot as well as the step size in between successive x or y values. The next line defines a grid of 2D coordinates that span the space defined by the first line of code. I'm assuming a symmetric range going from -N to N for both coordinates. The line after that computes a logical map that describes the above equation where false means that a pair of x and y values didn't satisfy the equation and true means it did. The last few lines are all for producing the plot. I use imagesc to display this region for us, enforcing that the colour map be gray, the shading be interpolated and imagesc makes the y axis going downwards as positive by default. To flip this to what we're used to, I used axis xy .

The last part of the code is primarily for clean up. Because imagesc is displaying an image, I'd like the horizontal and vertical axes to display the actual coordinate data rather than the indices of each pair of x and y values. As such, I simply relabel the x and y labels so that they are what we expect and I also give the appropriate titles for each the dimensions.

We get this:

在此处输入图片说明

Here, white means that the pair of x and y coordinates you are looking at satisfy the inequality and black means that the pair didn't.

reformat equation z = 2x^4 - 3x^2y +y^2

3D网格countour

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM