Plotting contours with additional condition

7 次查看(过去 30 天)
Let f(z) be a complex function. Suppose I wanted to plot the contours where the imaginary part, imag(f(z)) = 0, but the real part positive. An example code would be
% Mesh
x = linspace(-2, 2, 50);
y = linspace(-2, 2, 50);
[xmat, ymat] = meshgrid(x, y);
z = xmat + 1i*ymat;
% Function
f = sqrt(z);
% Contour
[C, h] = contour(x, y, imag(f), [0 0]);
This gives me the contours with imag(f) == 0. However, I'd like to only plot contours with imag(f) == 0 && real(f) >= 0. Is there an easy way to do this?

回答(1 个)

Rick Rosson
Rick Rosson 2011-7-13
Please try the following:
thresh = 0.02;
idx = ( (abs(imag(f)) < thresh) & (real(f)>=0) );
[C, h] = contour(x, y, idx, [ 1 1 ]);
This approach is highly dependent on the choice of value for thresh. So it's not a perfect solution, but it is a possibility.
HTH.

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by