Matlab Implementation of Monte Carlo Algorithms

Source: Internet
Author: User

The Monte Carlo (Monte Carlo) method, also known as the computer random simulation method, is a calculation method based on "random number.

Suppose we have an expression y = x ^ 2. How can we use the MC method to obtain the definite integral of a function in the range [0, 1?

You can use area to determine the points, that is, the area under the arrow.

To facilitate the connection and take care of new users, let's provide a plotting program.

X = 0: 0. 01:1; y = x. ^ 2; plot (x, y );


The implementation of the MC method is very simple. You can use the following code
Staus = 10;
For I = % 4 simulation times
Point = staus. ^ I; % Number of simulated random points
RandData = rand (2, point); % random (x, y) scatter plots are generated based on random points. If you do not know, try % scatter (RandData (1 ,:), randData (2 ,:))
Below = find (RandData (1, :). ^ 2> RandData (2, :)); % find the scatter plot under the curve
Outcome (I) = length (Below)/length (RandData); % representation of the final result
End
Outcome =

0.3000 0.3600 0.3180 0.3311
From the Outcome perspective, by increasing the number of instant points, the result is more consistent with the actual value.
When the number of Scattered Points is 10 ^ 4, see the following figure.
BelowData = RandData (:, Below );
Hold on
Scatter (BelowData (1, :), BelowData (2 ,:))


If the number of scattered points we select is 10 ^ 5, the set point value is 0.3335. See the following figure for the obtained graph (Code omitted, same as above)


The above is the simplest understanding of the MC method, but it is integrated in mind and suitable for beginners to learn. Therefore, MC is a probability-based random simulation method.

Reference address:Http://blog.sciencenet.cn/blog-316653-375888.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.