Question: Wrtie a C++ program to approximate the integral of a function f(x) over an interval using a Monte Carlo integration procedure. The form of Monte
Wrtie a C++ program to approximate the integral of a function f(x) over an interval using a Monte Carlo integration procedure. The form of Monte Carlo integration over the interval [a, b] that we will use works in the following way:
(a) Select N points x1, . . . , xN from the interval [a, b], uniformly at random.
(b) Using these N points, compute I = (b a)/N * (sum from i=1 to N of f(xi))
As a consequence of the law of large numbers, the quantity I will approximate the integral of f(x) over the interval [a, b]. The program should report the value of I back to the user. The program should take three command line arguments: a double representing a, a double representing b, and an integer representing the number of points selected in the interval [a, b]. The function f(x) to be integrated will be defined in a separate .cpp named hw_6_ex_2_f.cpp, with a corresponding header file hw 6_ex_2_f.h. You will need to include the header file with this name in the file for your main routine.
Header file:
#ifndef HW_6_EX_2_F_H #define HW_6_EX_2_F_H double f(double x); #endif
example .cpp:
#include "hw_6_ex_2_f.h" #includedouble f(double x){ return x * x; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
