EP 208 Computational Methods in Physics
 


Computer Homeworks

  • For *all* ep208 students, here we give two homeworks whose weights are additional 10% to your final average.
  • Deadline of both homeworks is June 2015
 *** HOMEWORK 1 ***

Implement a Discrete Random Variable Class (DRV).
Each object of this class will represent a discrete random variable.
The class must include

    * a default constructor function whose prototype is
      DRV(vector<double> X, vector<double> pmf);
      to set (initialize) the random variable X and its corresponding probability mass function (PMF) and
      to check if sum of the pmf values is unity (1.0) or not.
    * a member function named double mean()        that returns the mean value of the random variable.
    * a member function named double RMS()         that returns the RMS  value of the random variable.
    * a member function named double stddev()      that returns the standard deviation of the random variable.
    * a member function named double variance()    that returns the variance of the random variable.
    * a member function named double moment(int n) that returns n'th moment of the random variable.
    * a member function named double P(double a, double b)
      that returns either the probability between [a, b] (including a and b) of the random variable 
      or 0 if the [a, b] is out of range.

Example usage of the DRV class is given below:
________________________________________________________________
#include <iostream>
#include <vector>
using namespace std;

#include "DRV.h"

int main(){

   vector x(5), y(5);
   x[0]=1.0; x[1]=2.0; x[2]=3.0; x[3]=4.0; x[4]=5.0;
   y[0]=0.1; y[1]=0.3; y[2]=0.4; y[3]=0.1; y[4]=0.1;

   DRV myrv(x, y);

   cout << "mean  = " << myrv.mean() << endl;
   cout << "RMS   = " << myrv.RMS()  << endl;
   cout << "P(1,3)= " << myrv.P(1.0, 3.0) << endl;

   return 0;
}

 *** HOMEWORK 2 ***

Implement a Continuous Random Variable Class (CRV).
Each object of this class will represent a continuous random variable.
The class must include

    * a default constructor function whose prototype is
      CRV(double (*pdf)(double x), double xmin, double xmax);
      to set (initialize) probability distribution function (PDF) for the range [xmin,xmax] and
      to normalize the function such that the area under the pdf curve is unity (1.0).
    * a member function named double mean()        that returns the mean value of the distribution.
    * a member function named double RMS()         that returns the RMS  value of the distribution.
    * a member function named double stddev()      that returns the standard deviation of the distribution.
    * a member function named double variance()    that returns the variance of the distribution.
    * a member function named double moment(int n) that returns n'th moment of the distribution.
    * a member function named double P(double a, double b)
      that returns either the probability of the distribution between [a, b] (including a and b)
      or 0 if the [a, b] is out of range.

Use Trapezoidal Method for the integrations.

Example usage of the CRV class is given below:
________________________________________________________________
#include <iostream>
using namespace std;

#include "CRV.h"

// Probability Density Function
double function f(double x){
  return 2*x;
}

int main(){

   CRV myrv(f, 0.0, 4.0);

   cout << "mean  = " << myrv.mean() << endl;
   cout << "RMS   = " << myrv.RMS()  << endl;
   cout << "P(1,3)= " << myrv.P(1.0, 3.0) << endl;

   return 0;
}
University of Gaziantep - Department of Engineering Physics (2009 - 2024)