You are working on monte_carlo function
 
search
 


Contents:


swap function:
Swapping two values to change their addresses.
a

max function:

The largest number among two.
a

absolute value function:

The positive value of a number.
a

sorting function:

How to sort an array.
a

bisection function:

Bissection method used to solve equations by computing, example of cable strung ..
a

Newton-Raphson function:

To solve equations numerically, example of free fall motion ..
a

Secant function:

To solve equations numerically, example of free fall motion ..
a

isPrime function:

This function tests whether an integer is prime..
a

abundant function:

This function tests whether an integer is abundant..
a

factorial function:

Factorial of an integer..
a

quadratic function:

solving a quadratic equation ..
a

even_odd function:

To know if an integer is even or odd ..
a

mean value function:

To calculate the arithmetic mean of some values..
a

reverse function:

To reverse an integer and to know whether a string is palindrome..
a

least squares function :

To calculate the slope and the y_intercept of a linear equation ..
a

Monte Carlo method:

To calculate integrales or probabilities using Monte Carlo technique ..
a

2 and 3 equations systems solving :

To solve the 2 and 3 equations systems with 2 and 3 variables ..
a

Interpolation and Extrapolation :

To solve for lacking values in graphs or tables ..
a

Integer divisors :

Gives the divisors fo an integer and tests whether it is prime ..
a

Amicable pair :

Gives, if it exists, the amicable number of a given integer ..
a

Perfect numbers :

Tests whether a positive integer is perfect, and more ..
a

Periodic table of elements:

Periodic table of elements in C language, and more ..
a

Playing TicTacToe

Using some Fortran90 language funtionalities, and more ..
a


a
home

   Monte Carlo method

	

Monte Carlo function

1. Definition: Monte Carlo technique is generally used to intergrate functions using random numbers. Here is the method: 2. Examples in C language: //1.Integral: //------------- #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> #define pi 3.1416 int main() { float a ; float b ; int number; printf("n Enter two limit numbers and a long integer : --> "); scanf("%f %f %d", &a,&b,&number); float n = 1.0/number; float y; float random_x; float random_y; int count=0; int i = 0; for (i =1; i <= number; i++) { unsigned int seed = (unsigned int)time(NULL); srand (i*seed); random_x = a + (rand()/(RAND_MAX +1.0))*(b - a); random_y = a + (rand()/(RAND_MAX +1.0))*(3.0 + 1.0); // choose a function //------------------ y = 2*random_x; if(y >= random_y ) { count =count + 1; } } printf("The rate is %d n",count); printf("n"); float m; m = count*n; float s = m*8; printf("The rate is %2.3fn",m); printf("The area is %4.3fn",s); return 0; } //2. Other function sin(x) //------------------------- #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> #define pi 3.1416 int main() { float a ; float b ; int number; float surface; printf("n Enter two limit numbers, a long integer, and the surface : --> "); scanf("%f %f %d %f", &a,&b,&number,&surface); //In this example, the surface = pi float n = 1.0/number; float y; float random_x; float random_y; int count=0; int i = 0; for (i =1; i <= number; i++) { unsigned int seed = (unsigned int)time(NULL); srand (i*seed); random_x = a + (rand()/(RAND_MAX +1.0))*(b - a); random_y = a + (rand()/(RAND_MAX +1.0))*(0.0 + 1.0); y = sin(random_x); //chosen function : sin(x) //------------------------- if(y >= random_y ) { count = count + 1; } } printf("The rate is %d n",count); printf("n"); float m; m = count*n; float s = m*surface; printf("The rate is %2.3fn",m); printf("The area is %4.3fn",s); return 0; } //3. Calculating the number pi: //------------------------------- #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> int main() { int number; printf("n Enter a long integer : --> n"); scanf("%d" , &number); float n = 1.0/number; float yo; float y; float random_x; float random_y; int count=0; int i = 0; for (i =1; i <= number; i++) { unsigned int seed = (unsigned int)time(NULL); srand (i*seed); //generating random numbers between 0 and 1 random_x = 0.0 + (rand()/(RAND_MAX +1.0))*(0.0 + 1.0); random_y = 0.0 + (rand()/(RAND_MAX +1.0))*(0.0 + 1.0); yo = pow(random_x,2) + pow(random_y,2); y = sqrt(yo); if(y < = 1)/*condition to be within the circle: the distance y must be lower or egal to the radius if of the circle which is equal to 1. */ { count = count + 1; } } printf("The hit count is %d n", count); printf("n"); float m; m = count*n; float surface = 1; // = 1 x 1 , the surface of the square float s = m*surface; printf("The rate count/number is %2.3fn",m); printf("n"); printf("The value of pi/4 is %4.3fn",s); printf("n"); printf("The value of pi is %4.3fn",4*s); return 0; } //4. Using Gaussain distribution to calculate a probability // ------------------------------------------------------------- #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> #define pi 3.1416 int main() { float Po = 2*sqrt(pi); float P = 1.0/Po; float a ; float b ; int number; printf("n Enter two limit numbers, and a long integer : --> "); scanf("%f %f %d", &a,&b,&number); float n = 1.0/number; float y; float random_x; float random_y; int count=0; int i = 0; for (i =1; i<= number; i++) { unsigned int seed = (unsigned int)time(NULL); srand (i*seed); random_x = a + (rand()/(RAND_MAX +1.0))*(b - a); random_y = 0.0 + (rand()/(RAND_MAX +1.0))*(0.0 + 1.0); float w = 0.5*pow(random_x,2); y = exp (-w); if(y >= random_y ) { count = count + 1; } } printf("The hit count is %d n",count); printf("n"); float m; float surface; surface = 1*(b-a); m = count*n; float s = P*m*surface ; printf("The rate count/number is %6.6fn",m); printf("The area is %4.3fn",s); return 0; } /* Notes: -------- 1. count = ∑ f(random_x) (i: from 1 to number) the function f is the hit-count function P = 1/Po = 1/sqrt(2*pi) for normalisation f = P*count s P*m*(b-a) = P*count*n*(b-a)= P*count*(b-a)/number Thus the area is: s = [(b-a)/number] * ∑ f(random_x) (i: from 1 to number) 2. Here, the area is the probability to find an event between a and b, using Gauss probability distribution. */


Custom Search


© 2007. The scientificsentence . All rights reserved.