Monte Carlo

I have a monte carlo problem to solve to;
1) Determine the uncertainty
2) generate 10^3 values for normally distributed x and y variables
3) Calculate the variable q for each (x,y) pair
4) Plot a histogram

The given question is
x=6.0+-0.1
y=3.0+-0.1
find q= xy + x^2 / y (Find answer with uncertainty using monte carlo)

I feel that my problem is mostly in terms of not understanding how the statistics work, rather than the programming itself. Any help? Below is the code I have so far



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
  #include <iostream>
#include <stdlib.h>
#include <cmath>
#include <time.h>
#include <stdio.h>

using namespace std;


int main()
{
    srand(time(NULL));
    int i = 1;
    double num = 0;
    double sum =0;
    double mean;
     double xSum =0;
    double ySum = 0;
    double diff;
    cout<< "How many times would you like to run?" << endl;
    cin >> num;

    while (i <=num)
    {
    double x = 5.9;
    double y = 2.9;
    double xError;
    double yError;
    double q = 0;
    xError = ((double)rand()/RAND_MAX)/5.0;
    yError = ((double)rand()/RAND_MAX)/5.0;
    x = x+ xError;
    y = y + yError;
    q = x*y+ x*x/y;
    diff = abs(30-q);
    // cout <<"x = "<< x << endl<< "y = " << y << endl << "q = "<< q << endl << endl;
    i++;

    xSum = xSum + x;
    ySum = ySum +y;
    sum = sum+ q;
    }
    mean = sum / num;
    cout << "mean = " << mean << endl << "x mean = " << xSum/ num<< endl<< "y mean = " << ySum / num << endl;
    return 0;
}
The code looks good to me. I guess the next step is to figure out how to do a histogram. Should that be of x values? y values? q values? All three?
The histogram is for the q values.

How should I go about
Calculating the variable q for each (x, y) pair, and determine the uncertainty of q? Can this be integrated into the program?
You're already calculating q for each pair at line 34. What is the formula for the uncertainty?

As for the histogram, figure out the minimum and maximum values of q. Then figure out the number of buckets for the histogram and which bucket a value goes into.
Topic archived. No new replies allowed.