0% found this document useful (0 votes)
5 views4 pages

Matlab 14

Uploaded by

ABHAY THAKUR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Matlab 14

Uploaded by

ABHAY THAKUR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment: Random Number Generation and Monte Carlo Methods in MATLAB

Objective

To understand random number generation in MATLAB and use Monte Carlo methods for numerical
integration and simulation.

Theory

Monte Carlo methods involve repeated random sampling to solve problems that may be
deterministic in nature. It is widely used in numerical integration, optimization, and simulations of
random processes.

1. Random Number Generation in MATLAB:

o rand: Generates uniformly distributed random numbers between 0 and 1.

o randi: Generates uniformly distributed random integers within a specified range.

o randn: Generates normally distributed random numbers with a mean of 0 and a


standard deviation of 1.

2. Monte Carlo Methods:

o Based on the Law of Large Numbers.

o Approximate results improve with the number of iterations.

Example Applications:

o Estimation of π\piπ using random sampling.

o Numerical integration of a function.

Materials Required

 MATLAB Software

 A computer system with MATLAB installed

Procedure

Part 1: Random Number Generation

1. Generate random numbers using MATLAB functions:

rand_num = rand(1, 10); % 10 random numbers between 0 and 1

rand_int = randi([1, 100], 1, 10); % 10 random integers between 1 and 100

rand_norm = randn(1, 10); % 10 random numbers with normal distribution

2. Display and analyze the random numbers:

disp('Uniform random numbers:'); disp(rand_num);

disp('Random integers:'); disp(rand_int);

disp('Normally distributed random numbers:'); disp(rand_norm);


Part 2: Monte Carlo Method for Estimating π\piπ

1. Use a square of side length 2 and an inscribed circle of radius 1.

2. Generate random points in the square and count those inside the circle:

N = 10000; % Number of points

x = rand(1, N) * 2 - 1; % Random x-coordinates between -1 and 1

y = rand(1, N) * 2 - 1; % Random y-coordinates between -1 and 1

inside_circle = (x.^2 + y.^2 <= 1);

pi_estimate = 4 * sum(inside_circle) / N;

fprintf('Estimated value of pi: %.4f\n', pi_estimate);

3. Visualize the random points:

figure;

plot(x(inside_circle), y(inside_circle), 'g.', x(~inside_circle), y(~inside_circle), 'r.');

axis equal;

title('Monte Carlo Estimation of \pi');

legend('Inside Circle', 'Outside Circle');

Part 3: Monte Carlo Integration

1. Define the function f(x)=x2f(x) = x^2f(x)=x2 over the interval [0, 1].

2. Compute the integral using random sampling:

N = 10000; % Number of random points

x = rand(1, N); % Random x-coordinates between 0 and 1

f_x = x.^2; % Function values

integral_estimate = mean(f_x); % Average value approximates the integral

fprintf('Estimated integral of x^2 over [0, 1]: %.4f\n', integral_estimate);

Observations

Method Task Result

Random Number Generate random data Uniform, integer, normal numbers

Monte Carlo for π\piπ Estimate value of π\piπ Approximation improves with more points

Monte Carlo Integration Integrate f(x)=x2f(x) = x^2f(x)=x2 Approximates true integral value (1/3)

Conclusion
Monte Carlo methods provide an efficient way to solve problems using random sampling. With
increasing sample size, the accuracy of results improves. MATLAB simplifies the implementation with
built-in random number generation functions.

Code

% Part 1: Random Number Generation

rand_num = rand(1, 10); % 10 random numbers between 0 and 1

rand_int = randi([1, 100], 1, 10); % 10 random integers between 1 and 100

rand_norm = randn(1, 10); % 10 random numbers with normal distribution

disp('Uniform random numbers:'); disp(rand_num);

disp('Random integers:'); disp(rand_int);

disp('Normally distributed random numbers:'); disp(rand_norm);

% Part 2: Monte Carlo Estimation of Pi

N = 10000; % Number of points

x = rand(1, N) * 2 - 1; % Random x-coordinates

y = rand(1, N) * 2 - 1; % Random y-coordinates

inside_circle = (x.^2 + y.^2 <= 1);

pi_estimate = 4 * sum(inside_circle) / N;

fprintf('Estimated value of pi: %.4f\n', pi_estimate);

% Visualization

figure;

plot(x(inside_circle), y(inside_circle), 'g.', x(~inside_circle), y(~inside_circle), 'r.');

axis equal;

title('Monte Carlo Estimation of \pi');

legend('Inside Circle', 'Outside Circle');

% Part 3: Monte Carlo Integration

N = 10000; % Number of random points

x = rand(1, N); % Random x-coordinates between 0 and 1

f_x = x.^2; % Function values


integral_estimate = mean(f_x); % Average value approximates the integral

fprintf('Estimated integral of x^2 over [0, 1]: %.4f\n', integral_estimate);

You might also like