generated from github/welcome-to-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonte Carlo Sphere
75 lines (62 loc) · 1.79 KB
/
Monte Carlo Sphere
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
double x, y, z;
double zupper, zlower, yupper, ylower, xupper, xlower;
double montevol,volume;
double totalexpvol, totalvol;
int j;
int iterations;
printf("enter lower x limit\n");
scanf("%lf", &xlower);
printf("enter upper x limit\n");
scanf("%lf", &xupper);
printf("xlower %lf xupper %lf\n", xlower, xupper);
printf("enter lower y limit\n");
scanf("%lf", &ylower);
printf("enter upper y limit\n");
scanf("%lf", &yupper);
printf("ylower %lf yupper %lf\n", ylower, yupper);
printf("enter lower z limit\n");
scanf("%lf", &zlower);
printf("enter upper z limit\n");
scanf("%lf", &zupper);
printf("zlower %lf zupper %lf\n", zlower, zupper);
volume = (xupper - xlower)*(yupper - ylower)*(zupper - zlower);
printf("volume is %lf\n", volume);
printf("enter iterations up to 1000000\n");
scanf("%d", &iterations);
totalvol = 0;
totalexpvol = 0;
for (j = 1;j < iterations;j++)
{
/* find random numbers for x,y and z */
x = rand() % 1000;
y = rand() % 1000;
z = rand() % 1000;
y = y / 1000;
x = x / 1000;
z = z / 1000;
/* x,y and z will have numbers between 0 and 1 */
/* so multiply by the user's entered ranges for x,y and z */
x = xlower + (xupper - xlower)*x;
y = ylower + (yupper - ylower)*y;
z = zlower + (zupper - zlower)*z;
if (x >= xlower && z >= zlower && y >= ylower)
{
totalvol = totalvol + 1; /* This contains the total number of entries */
if ((pow(y, 2) + pow(x, 2) + pow(z, 2)) < 4)
{
totalexpvol = totalexpvol + 1;/* This contains number of entries within desired vol */
}
}
}
if (totalvol != 0)
{
montevol = volume * (totalexpvol / totalvol);/* Monte Carlo volume os the fraction of the cube volume */
}
printf("monte carlo volume is %lf\n", montevol);
}