我使用随机点来确定曲线下方的区域(蒙特卡罗):
我想将RP分成RPA和RPB,具体取决于它在曲线的上方或下方。然后,想法是以不同的颜色将RPA和RPB对X进行绘制。此代码不起作用,因为RPA和RPB列数与X不同:
clf
f = @(x) sin(x/10) + cos(x/60); % Function
xMin = 1; xMax = 100; % x interval
X = [xMin:xMax];
Y = f(X);
plot(X,Y), hold on % Plot function
yMin = min(Y); yMax = max(Y); % Axes limits
set(gca, 'xlim', [xMin, xMax], 'ylim', [yMin, yMax])
m = 20; % Random points per x value
RP = rand(m, columns(X)) .* (yMax-yMin) .+ yMin;
% Split points (doesn't work)
RPA = RP(RP>Y);
RPB = RP(RP<=Y);
br = size(RPB) / size(RP) % Ratio of points below
a = (xMax - xMin) * (yMax - yMin) * br % Area below
% Plot points
plot(X, RPA, 'r.') % Above
plot(X, RPB, 'g.') % Below
是否有可能构建RPA和RPB,使它们的大小与RP相同,排除的点y为NaN或类似的东西,可以计算和绘制?