这个作业是用C++完成欧拉方法来求解常微分方程的编码

ACMS 20210 Assignment 6

1. In this problem, we will implement Euler’s method to solve an ordinary differential equation

for an arbitrary real-valued function f(x, y) as the right hand side over the interval [0, b]. We

wish to approximate the solution to the ordinary differential equation:

y

0 = f(x, y)

y(0) = y0

using Euler’s method and N subintervals.

Your program should take four command line arguments: a double representing y0, a double

representing the endpoint b, an integer N for the number of subintervals, and the name of an

output file into which to write the pairs (xi

, yi), with one pair per line, with xi and yi separated

by a space. (See the appropriate directory for a sample of what the output might look like.)

The function f will be declared in the header file hw 6 ex 1 f.h and defined in the file

hw 6 ex 1 f.cpp. You will need to #include ‘‘hw 6 ex 1 f.h’’ in your main routine. The exact

header file that you should use will appear in the folder for this assignment, and an example of

what the corresponding .cpp file might look like will also appear. A different definition for the

function f will be used in testing your program, and you should try alternative definitions for f

to see if they yield reasonable results.

I have provided a sample of what the output should look like for the function f(x, y) = xy

using 1024 subintervals in the directory for this assignment on Sakai.

See the last page of the assignment for an explanation of Euler’s method.

2. In this problem, we will 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

X

N

i=1

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].

Your program should report the value I back to the user.

Your 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. The exact header file that you should use will

appear in the folder for this assignment, and an example of what the corresponding .cpp file

might look like will also appear. A different definition for the function f will be used in testing

your program, and you should try alternative definitions for f to see if they yield reasonable

results.

3. In this problem, we will use a Monte Carlo method to determine experimentally the expected

distance of a randomly selected point in the unit disk from a given point (a, b). To simulate

this, we will draw N points (x1, y1), . . . ,(xN , yN ) from the unit disk, uniformly at random. For

each point, we will compute the distance of the point to (a, b). The average of the distances of

the selected points to (a, b) is an approximation of the expected distance of a randomly

selected point in the unit disk to (a, b).

Your program should take three command line arguments: A double for the coordinate a, a

double for the coordinate b, and an integer N representing the number of trials.

The exact average distance of a point selected uniformly at random from the disk to the origin

(0, 0) is 2

3

. The approximate average distance of a point selected uniformly at random from the

disk to the point (1, 0) is 1.1318.

Hint: to select uniformly at random from the disk, consider using rejection sampling. (This is

not the most efficient approach, but it is an easy approach if you do not want to reason about

probability density functions.)