0% found this document useful (0 votes)
4 views

AGH Computer Science C Programming Laboratory 5

Uploaded by

michalgach.0ff
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

AGH Computer Science C Programming Laboratory 5

Uploaded by

michalgach.0ff
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Laboratory 05 – loops part2 30.10.

2024

1. [6points] Monte Carlo methods are a broad class of computational algorithms that rely on
repeated random sampling to obtain numerical results. One of the basic examples of getting started
with the Monte Carlo algorithm is the estimation of Pi.
Estimation of Pi
The idea is to simulate random (x, y) points in a 2D plane with domain as a square of side 1 unit.
Imagine a circle inside the same domain with same diameter and inscribed into the square. We then
calculate the ratio of number points that lied inside the circle and total number of generated points.

We know that area of the square is 1


1 2 𝜋
unit sq while that of circle is𝜋 ⋅ (2) = 4 . Now for a very large number of generated points,
area of the circle number of points inside the circle number of points inside the circle
= number of points inside the squarethat is, 𝜋 = 4 ∗ number of points inside the square.
area of the square

The algorithm:
a) Initialize a variable that counts the number of points inside the circle.
b) In the loop:
c) Generate a pseudo-random x coordinate between -1 and 1.
d) Generate a pseudo-random y coordinate between -1 and 1.
e) Check if the drawn point lies inside the circle with a radius of 1.
f) If so, increase the appropriate variable.
g) Repeat the calculation in loop 7000.
h) Calculate and print out your estimate for pi.

Generate pseudo-random number


Function int rand (void) returns a pseudo-random integral number in the range between 0 and
RAND_MAX. RAND_MAX is a constant defined in <stdlib.h>.

Test data:
2. [3points] Let's see how the estimate value depends on the number of pseudo-random number
generations. Add a loop to the previous program that will control the number of pseudo-random
number generations.

Test data:

3. [6 points] Numerical integration comprises a broad family of algorithms for calculating the
numerical value of a definite integral. The basic problem in numerical integration is to compute an
𝑏
approximate solution to a definite integral ∫𝑎 ⬚ 𝑓(𝑥)𝑑𝑥to a given degree of accuracy. Rectangular
integration (a.k.a. The Midpoint Rule) is a numerical integration technique that approximates the
integral of a function with a rectangle. It uses rectangles to approximate the area under the curve.

𝐴4

∫ ⬚ 𝑥𝑑𝑥 ≈ 𝐴𝑟𝑒𝑎𝑜𝑓𝑅𝑒𝑐𝑡1 + 𝐴𝑟𝑒𝑎𝑜𝑓𝑅𝑒𝑐𝑡2 + 𝐴𝑟𝑒𝑎𝑜𝑓𝑅𝑒𝑐𝑡3 + 𝐴𝑟𝑒𝑎𝑜𝑓𝑅𝑒𝑐𝑡4


𝐴0
𝐴4

∫ ⬚ 𝑥 2 𝑑𝑥 ≈ 𝐴𝑟𝑒𝑎𝑜𝑓𝑅𝑒𝑐𝑡1 + 𝐴𝑟𝑒𝑎𝑜𝑓𝑅𝑒𝑐𝑡2 + 𝐴𝑟𝑒𝑎𝑜𝑓𝑅𝑒𝑐𝑡3 + 𝐴𝑟𝑒𝑎𝑜𝑓𝑅𝑒𝑐𝑡4


𝐴0

Area of Rectangle = base * height


The base of all rectangles is the same length.
𝐴4−𝐴0
base = A1-A0 = A2-A1 = A3-A2 = A4-A3 = 𝑛𝑢𝑚𝑏𝑒𝑟𝑜𝑓𝑅𝑒𝑐𝑡𝑎𝑛𝑔𝑙𝑒𝑠
The heights of the rectangles change.
The height of the first rectangle at the point M0 is f(M0).
We use the midpoint rule because the position of the M0 point is halfway between A0 and A1.
The height of the second rectangle at the point M1 which is halfway between A1 and A2 is f(M1),
and so on.

Now just use a loop to calculate the sum of all rectangles to calculate the numerical approximation
of the definite integral.

We will calculate the integrals of the following functions. Insert the following code into the
program, above the main function.

double f1(double x){


return 1;
}
double f2(double x){
return x;
}
double f3(double x){
return x*x;
}

a) In the main function, create two variables that will be the beginning and end of the integration
interval.
b) Create a variable that will hold the number of rectangles.
c) Create a variable that is equal to the base of the rectangles. Calculate its value using previously
defined variables.
d) Create a variable that will hold the result of the integral calculation.

e) Create a loop that iterates through all of the rectangles.


f) In the loop calculate the value of Mi, then use it to calculate the height value of the i-th rectangle
and its area.
g) Add the calculated area to the result.

h) After the loop is complete, print the result of the integral.

Calculate the integral for f1 (x) in the range 0 to 4 for 10 rectangles.


Calculate the integral for f2 (x) in the range 0 to 4 for 10 rectangles.
Calculate the integral for f3 (x) in the range 0 to 4 for 10 rectangles.

Test data:

4. [5 points] Let's check how the value of the definite integral depends on the number of rectangles.
Add a loop to the previous program that will control the number of rectangles. Here's a function that
we're going to integrate in the range 0 to 1.

double f(double x){


return x*x*x*x + x*x*x + x*x + x + 1;
}

Perform calculations for 10, 100, 1000 rectangles.


Test data:

Exact solution from https://www.wolframalpha.com

Next time:
Laboratory 06 – Arrays

You might also like