Question

Write a function of the form function [x1pts,x2pts] = unif_over_rect(a1,b1,a2,b2,n) which provides the coordinates (x1pts(i),x2pts(i)), 1...

Write a function of the form function [x1pts,x2pts] = unif_over_rect(a1,b1,a2,b2,n) which provides the coordinates (x1pts(i),x2pts(i)), 1 ≤ _i ≤ _n, of n random darts (more precisely, realizations of random darts) thrown at the rectangle a1 ≤ x1 ≤ b1, a2 ≤ x2 ≤ b2. (The lower left corner of the rectangle is a1,a2; the upper right corner of the rectangle is b1,b2.) You may assume that the darts are drawn from the bivariate uniform distribution over the rectangle and hence correspond to independent random variables x1pts(i) and x2pts(i). Note that x1pts and x2pts should each be single-index row arrays of length n — two separate outputs. The inputs a1, b1, a2, b2, and n are all scalars. Then write a script which calls unif_over_rect for a1 = −1, b1 = 1, a2 = −1, b2 = 1, n = 2000 and calculates and displays frac_in_circ, the fraction of darts that fall inside the unit circle (radius unity) centered at the origin. Of course, frac_in_circ should be close to π/4.

in MATLAB

Homework Answers

Answer #1

Function Code:

function [x1,x2] = unif_over_rect(a1,b1,a2,b2,n);

x1 = (b1-a1).*rand(n,1) + a1;
x2 = (b2-a2).*rand(n,1) + a2;

end

Script Code:

a1 = -1;
a2 = -1;
b1 = 1;
b2 = 1;
n = 2000;

[x,y] = unif_over_rect(a1,b1,a2,b2,n);

s = 0;
b = 0;
X1 = [];
Y1 = [];
var1=1;
var2=1;

X2 = [];
Y2 = [];
for i=1:1:n;
for j=1:1:n;
if x(i).^2+y(j).^2 < 1
s = s+1;
X1(var1)=x(i);
Y1(var1)=y(j);
var1=var1+1;
else
b = b+1;
X2(var2)=x(i);
Y2(var2)=y(j);
var2=var2+1;
end
end
end

num1 = s/(n*n);
display(num1)
num2 = pi/4;
display(num2)

---------------------------------------

PLEASE GIVE AN UPVOTE!!!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions