MATLAB: Random points between lines

between linesMATLABrandom points

Can anyone tell me how can I go about plotting random points between two straight lines?

Best Answer

Try this, adapting as needed for your particular equations of lines:
% Create x axis:
x = linspace(-10, 10, 1000);
% Create line 1
m1 = 0.13;
b1 = 0.5;
y1 = m1 .* x + b1;
plot(x, y1, 'b-', 'LineWIdth', 2);
grid on;
% Create line 2
m2 = 0.5;
b2 = 1;
y2 = m2 .* x + b2;
hold on;
plot(x, y2, 'b-', 'LineWIdth', 2);
grid on;
% Get random points along x axis.
numRandomPoints = 100;
randomXValues = min(x) + (max(x)-min(x)) * rand(1, numRandomPoints);
% Get y1 and y2 at those x values
y1r = interp1(x, y1, randomXValues);
y2r = interp1(x, y2, randomXValues);
% Construct random y values for each random x value.
upperValues = max([y1r; y2r], [], 1); % Upper boundary in y direction.
lowerValues = min([y1r; y2r], [], 1); % Lower boundary in y direction.
yr = lowerValues + (upperValues - lowerValues) .* rand(1, numRandomPoints);
% All done! Now just simply plot the random points.
plot(randomXValues, yr, 'r.', 'MarkerSize', 12);
xlabel('x', 'FontSize', 15);
ylabel('y', 'FontSize', 15);
0000 Screenshot.png