This preview shows pages 1–5. Sign up to view the full content.
1.00 Lecture 24
Integration
Reading for next time: Numerical Recipes, pp. 347368
(Get as far as you’re comfortable)
Numerical Integration
•
Classical methods are of historic interest only
–
Rectangular, trapezoid, Simpson’s
–
Work well for integrals that are very smooth or can be
computed analytically anyway
•
Extended Simpson’s method is only elementary
method of some utility for 1D integration
•
Multidimensional integration is tough
–
If region of integration is complex but function values are
smooth, use Monte Carlo integration (first exercise)
–
If region is simple but function is irregular, split integration
into regions based on known sites of irregularity
–
If region is complex and function is irregular, or if sites of
function irregularity are unknown, give up
•
We’ll cover 1D extended Simpson’s method only
–
See Numerical Recipes chapter 4 for more
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Monte Carlo Integration
x
y
z=f(x,y)
Integrate f(x,y) over Circular Area
r
2r
2r
Randomly generate
points in square 4r
2
.
Odds that they’re in the
circle are
π
r
2
/ 4r
2
, or
π
/ 4.
This is Monte Carlo
integration, with f(x,y)= 1
If f(x,y) varies slowly, then
evaluate f(x,y) at each
sample point in limits of
integration and sum
(0,0)
Integration over Circular Area
public class MonteCarloIntegration {
public static double circularIntegral() {
int nIter= 1000000;
double sum= 0.0, radius= 0.5;
for (int i=0; i < nIter; i++) {
// Math.random() returns double d: 0 <= d <= 1
double x= Math.random()  radius;
// Ctr at 0,0
double y= Math.random()  radius;
double f= 1.0;
// f(x,y)—constant here
if ((x*x + y*y) < radius*radius)
// If in region
sum += f;
// Increment integral sum
}
return sum/nIter;
// Integral value
}
public static void main(String[] args) {
System.out.println(“Result: “ +circularIntegral() );
System.out.println(“Pi: “+ 4.0*circularIntegral() );
}
}
Integration over Circular Area, 2
// To integrate f(x,y) = exp (x)/(y*y+1) over this area:
public class MonteCarloIntegration2 {
public static double circularIntegral() {
// for loop, random x, y same as previous slide
// …
double f= Math.exp(x)/(y*y+1);
if ((x*x + y*y) < radius*radius)
// If in region
sum += f;
// Increment integral sum
}
return sum/nIter;
// Integral value
}
public static void main(String[] args) {
System.out.println(“Result: “ +circularIntegral() );
}
}
// Numerical integration is used when functions and areas
// of integration are really complex and ugly!
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentExercises: Take Your Pick
•
Find the shaded area within circles below:
–
Use circularIntegral() as your starting point
–
Use f(x,y)= 1 to find the areas below using integration
–
Equation of circle is (xx
c
)
2
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 11/29/2011 for the course CIVIL 1.00 taught by Professor Georgekocur during the Spring '05 term at MIT.
 Spring '05
 GeorgeKocur

Click to edit the document details