Question
need help with matLab
Question 1 (20 Points) Write a well-documented MATLAB script hmwk7Q1.m that simulates tossing 100 coins into a unit square. A
0 0
Add a comment Improve this question Transcribed image text
Answer #1

MATLAB CODE:

% Perform housekeeping
clear;
clc;
clf;

% Number of coins
n = 100;

% Generate random variables x, y
x = rand(1, n);
y = rand(1, n);

% Scatter plot
plot(x, y, 'o');
title('Location of Simulated Coins in Unit Square');
xlabel('x position');
ylabel('y position');
hold on;

% Inscribe a quarter circle x^2 + y^2 = 1
theta = linspace(0, pi/2);
% Draw circle using x = r*cos(theta) and y = r*sin(theta)
xx = cos(theta);% Here r = 1
yy = sin(theta);% Here r = 1
plot(xx, yy);
hold off;

% Vector of distance of each coin to the origin
r = sqrt(x.^2 + y.^2);

% Count number of coins inside the quarter circle
insideCount = length(find(r < 1));

% Ratio of the number of coins inside the quarter circle to the total
% number of coins
ratio = insideCount/n;

% Associate Pi with 4 * The Ratio of the Number of Coins inside the quarter
% circle to the total number of coins
result = pi*4*ratio;

SAMPLE PLOT:

Location of Simulated Coins in Unit Square 0000 y position 0.2 0.6 0.8 0.4 x position

FOR ANY HELP JUST DROP A COMMENT

Add a comment
Know the answer?
Add Answer to:
need help with matLab Question 1 (20 Points) Write a well-documented MATLAB script hmwk7Q1.m that simulates...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT