View
215
Download
0
Category
Preview:
Citation preview
8/14/2019 Phys102 Lecture03!08!10Fall Integration
1/11
8/14/2019 Phys102 Lecture03!08!10Fall Integration
2/11
2
Integration procedure:
We will need to define an operatorwhich will
take as input a function limits of integration
possibly, desired accuracy
give as output the result of integratingthe function between the two limits
8/14/2019 Phys102 Lecture03!08!10Fall Integration
3/11
3
From Calculus:
Rectangular rule: Evaluate the function at
the left endpoint of each
interval.Multiply by the width of
the interval.
Equivalent to sum of
rectangular areas.
1
0
( ') ' ( )x n
kL
f x dx x f L k x
8/14/2019 Phys102 Lecture03!08!10Fall Integration
4/11
4
Alternate: Midpoint rule
Midpoint rule. Evaluate the function at the left
endpoint of each interval.
Multiply by the width of the interval.
Equivalent to sum of rectangularareas, but has a better discrete errorbehavior.
1
0
( ') ' ( ( 0.5) )
x n
kL
f x dx x f L k x
8/14/2019 Phys102 Lecture03!08!10Fall Integration
5/11
5
A first Implementation: Procedural
#include #include
using namespace std;
double square(double aX) {return aX*aX;
}
double integrate(double aLeftLimit, double aRightLimit,int aNumberOfIntervals, double integrand(double)) {
double dx = (aRightLimit - aLeftLimit)/aNumberOfIntervals;double x = aLeftLimit;double sum = 0;for (int i=0; i loLimit >> upLimit;cout nIntervals;cout
8/14/2019 Phys102 Lecture03!08!10Fall Integration
6/11
6
Implementing code with a desired
accuracy.int main() {double loLimit, upLimit, epsilon, result;int nIntervals;cout loLimit >> upLimit;cout nIntervals >> epsilon;
double oldResult = 1.0e30; // large number, at least iterate onceint maximumIterations = 25;for (int j=0; j
8/14/2019 Phys102 Lecture03!08!10Fall Integration
7/11
7
Using a class: IntegralCalculator.hh#ifndef IntegralCalculator_hh#define IntegralCalculator_hh
class IntegralCalculator {
public:IntegralCalculator(double aIntegrandFunction(double));void setNumberOfIntervals(int aNumberOfIntervals)
{mNumberOfIntervals=aNumberOfIntervals;}void setLeftLimit(int val) {mLeftLimit=val;}void setRightLimit(int val) {mRightLimit=val;}void readInputData();void integrate();void print();
int numberOfIntervals() {return mNumberOfIntervals;}int leftLimit() {return mLeftLimit;}
int rightLimit() {return mRightLimit;}int result() {return mResult;}
private:int mNumberOfIntervals;double mLeftLimit;double mRightLimit;double mResult;double (*mIntegrandFunction) (double);
};#endif
8/14/2019 Phys102 Lecture03!08!10Fall Integration
8/11
8
The source file: IntegralCalculator.cc
#include "IntegralCalculator.hh"
#include #include
using namespace std;
IntegralCalculator::IntegralCalculator(doubleaIntegrandFunction(double)) {
mIntegrandFunction = aIntegrandFunction;}void IntegralCalculator::readInputData() {double loLimit, upLimit;int nIntervals;cout loLimit >> upLimit;cout nIntervals;setLeftLimit(loLimit);setRightLimit(upLimit);setNumberOfIntervals(nIntervals);return;
}
void IntegralCalculator::integrate() {double dx = (mRightLimit -
mLeftLimit)/mNumberOfIntervals;double x = mLeftLimit;double sum = 0;
for (int i=0; i
8/14/2019 Phys102 Lecture03!08!10Fall Integration
9/11
9
The new main() using the class.
#include "IntegralCalculator.hh"
double square(double aX) {
return aX*aX;
}
int main() {
IntegralCalculator IC1(square);IC1.readInputData();
IC1.integrate();IC1.print();
return 0;}
8/14/2019 Phys102 Lecture03!08!10Fall Integration
10/11
10
Compiling and linking with a class
Compiling the class to create amachine readable object file: g++ IntegralCalculator.cc c
Compiling the file containing the mainfunction and linking with thepreviously created object file: g++ integWithClass.cc o integWithClass
IntegralCalculator.o
8/14/2019 Phys102 Lecture03!08!10Fall Integration
11/11
11
For Homework: Numerical Integration
Chapter 13, Section 13.4
Assigment 1 Complete the coding of the IntegratorCalculator classusing the midpoint rule. Verify the convergence should be quadraticin the step length. In particular, plot graphs of the error againstthe inverse of the number of points, n, for the integral of x3 from 0to 1 for both the midpoint rule and the rectangle rule.
Assignment 2 Replace the midpoint rule by the Simpson rule, anddetermine graphically the order of error of this procedure.
Asignment 5 Write a program to take the integral of thenumerically evaluated derivative function you wrote in the previoushomework, use it for the function x4 to show that you recover theoriginal function.
11
1
0
( ) ( ) 4 ( )
6 2
R ni i
i i
iL
x xx f x dx f x f f x
Recommended