Matlab 怎麼填充曲線相交的區域呢?

比如這個問題里的陰影部分(http://www.zhihu.com/question/20626673)

我只會畫成下面的樣子

代碼如下:

x=0:0.01:1;
hold on
plot(x, sqrt(2.*x-x.^2))
plot(x, 1-sqrt(2.*x-x.^2))

x=0:0.01:0.5;
plot(x, (1+sqrt(1-4.*x.^2))/2 )
plot(x, (1-sqrt(1-4.*x.^2))/2 )

line([0 0], [0 1])
line([0 1], [0 0])
line([1 1], [0 1])
line([1 0], [1 1])

axis([-0.2,1.2,-0.2,1.2])
axis equal;


@吳運生

t = [-50:50 -50]/100*pi;
[x1,y1] = poly2cw(cos(t)/2, sin(t)/2 + .5);
t = [50:100 200 50]/100*pi;
[x2,y2] = poly2cw(cos(t).*(t~=2*pi) + 1, sin(t));
[x3,y3] = poly2cw(x2, -y2 + 1);
[Ex, Ey] = polybool("intersection", x1, y1, x2, y2);
[Ex, Ey] = polybool("intersection", Ex, Ey, x3, y3);

plot(x1, y1, "r", x2, y2, "g", x3, y3, "b", "linewidth", 2)
patch(Ex, Ey, "y", "linewidth", 3), axis image off


瀉藥,這種填充指定區域的作圖一般我會選用「patch」或「fill」函數來實現:

%%
hold on
grid on
axis equal

f1 = @(x) -sqrt(0.25-x.^2) + 0.5;
f2 = @(x) 1 - f1(x);
f3 = @(x) 1 - sqrt(2*x-x.^2);
f4 = @(x) 1 - f3(x);

x1 = linspace(0, 0.5, 500);
plot(x1, f1(x1), "-.", "LineWidth", 1.2)
plot(x1, f2(x1), "LineWidth", 1.2)

x2 = linspace(0, 1, 500);
plot(x2, f3(x2), "LineWidth", 1.5)
plot(x2, f4(x2), "LineWidth", 1.5)

xlabel("$x$", "interpreter", "latex", "FontSize", 13)
ylabel("$y$", "interpreter", "latex", "FontSize", 13)
title("Illustration to show the usage of function patch", ...
"FontName", "Arial", "FontSize", 14)
labels = {"$f_1(x)$", "$f_2(x)$", "$f_3(x)$", "$f_4(x)$"};
legend(labels, "interpreter", "latex", "FontSize", 12)

x_range = x2((f4(x2)&>f3(x2)) (x2&<=0.5)); % 確定待填充區域的x範圍 [y1, y2, y3, y4] = deal(f1(x_range), f2(x_range), f3(x_range), f4(x_range)); y_upper = min(y2, y4); % 填充區域的上邊界 y_lower = max(y1, y3); % 填充區域的下邊界 patch([x_range, fliplr(x_range)], [y_lower, fliplr(y_upper)], "r") alpha(0.65)

如果不要坐標軸的話,稍微改改也行:


x1=0:0.01:1;

hold on

y11=sqrt(2.*x1-x1.^2);

y12=1-sqrt(2.*x1-x1.^2);

plot(x1,y11)

plot(x1,y12)

x2=0:0.01:0.5;

y21=(1+sqrt(1-4.*x2.^2))/2;

y22=(1-sqrt(1-4.*x2.^2))/2;

plot(x2,y21)

plot(x2,y22)

x3=[0.13403 x1(15:41) x2(41:end) x2(end:-1:41) x1(41:-1:15) 0.13403];

y3=[0.5 y11(15:41) y21(41:end) y22(end:-1:41) y12(41:-1:15) 0.5];

fill(x3,y3,[1 1 0])

line([0 0], [0 1])

line([0 1], [0 0])

line([1 1], [0 1])

line([1 0], [1 1])

axis([-0.2,1.2,-0.2,1.2])

axis equal;


FILL(X,Y,C) fills the 2-D polygon defined by vectors X and Y

with the color specified by C. The vertices of the polygon

are specified by pairs of components of X and Y. If necessary,

the polygon is closed by connecting the last vertex to the first.

If C is a single character string chosen from the list "r","g","b",

"c","m","y","w","k", or an RGB row vector triple, [r g b], the

polygon is filled with the constant specified color.

fill()函數,可以填充由X,Y向量相交形成的區域。

作水平線y=0.5,可以得到與弧線相交的兩個點A,B.

你可以先求出A,B的坐標,然後把 上弧AB和下弧AB的點,分別放在向量X,Y裡面

然後fill(X,Y,"y")


推薦閱讀:

Mathematica如何導入某個csv或者excel文件的指定行和列?
如何看待工科生因為現在數學軟體太強了,進而對數學不重視的觀念?
R語言繪圖相比其他軟體主要是強在哪裡?
Mathematica 里如何查看函數內部代碼?
如何用最簡單的代碼說明Mathematica裡面的各個水平層次?

TAG:MATLAB | WolframMathematica | 繪圖 |