[SOLVED] CPP Assignment 3

35.00 $

Category:

Description

Rate this product

This part of the assignment is due by the 9/12.
In general always write your name and date of creation as comments in the files and
try to be “user friendly” when you write the code:
● Add comments on the code to remember yourself and the other what
the code does
● Manage the possible errors in the input given by the user
The 70% of this assignment consists in completing part 1) (a,b and c, e )
1. Define a class that includes the cauchy function and members to calculate the
integral of the function and sample random points on it.
a. Define the function, with a constructor that takes as argument the extremes of
the linear dominion where the function is defined and a string. These 3 things
should initialize private member of the class.
b. Define a private member function that is the cauchy function: f(x)= 1
π(1+x2)
c. Define a public member function of the class that evaluates the integral of the
function, on the range stored as private members. The integral can be
calculated as I= R*( N ) where xi are random points extracted from the
Σ (xi)
N
i
f
range and R is the length of the range. The integral should be 0.874 if you
use -5,5 as extremes of the range.
d. Add a member function that sample random numbers from the Cauchy
distribution. To sample random number from a not-uniform distribution you
can use the Metropolis algorithm.
i. Generate a random number (x) on the range where the function is
defined, sampled from a uniform function. This will be the first number
you generated (x 1 =x)
ii. Iterative:
1. Generate a random number y from the uniform distribution
2. Compute A=min( f(y) , ) , where f is the function and x t is the
f(xt) 1
previous number extracted
3. Accept y with probability A.
4. If you accepted y x t+1 = y otherwise x t+1 =x t
5. Write x t+1 on a file, called as the string that you passed as
argument to the constructor
e. Write a separate file where you have your main
i. Build an object of your new class
ii. Call the function that calculates the integral ( you could pass as only
argument of the function the number of points)
iii. Call the metropolis algorithm member function( you could pass as only
argument of the function the number of points). This should create a
file
iv. You can include in your main file cauchy_test.h: #include
“cauchy_test.h” and compile also adding cauchy_test.cc to the g++
list, then call in your main CreateHistogramFile(outputfile,low,high),
where outputfile is the file created in point 3 and low and high are the
ranges given to the constructor. This will create a file called
input_plotting.csv that you can open with excel/gnuplot to check if you
generated points according to a Cauchy distribution