The Rectangle Method. Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with.

Slides:



Advertisements
Similar presentations
Chapter 7: Integration JMerrill, 2009.
Advertisements

Applications of Integration
Integral as Accumulation What information is given to us by the area underneath a curve?
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Lecture 3: Integration. Integration of discrete functions
Nested conditional statements. Previously discussed Conditional statements discussed so far: Syntax of the if-statement: if-statement if-else-statement.
The switch statement: an N-way selection statement.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
Shorthand operators.
Integrals 5.
D MANCHE Finding the area under curves:  There are many mathematical applications which require finding the area under a curve.  The area “under”
The character data type char
ME 2304: 3D Geometry & Vector Calculus Dr. Faraz Junejo Double Integrals.
Section 6.1 Area Between Two Curves. All graphics are attributed to:  Calculus,10/E by Howard Anton, Irl Bivens, and Stephen Davis Copyright © 2009 by.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Parameter passing mechanism: pass-by-value. Introduction In the last webpage, we discussed how to pass information to a method I have kept it (deliberately)
The dangling-else ambiguity. Previously discussed The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
1 §12.4 The Definite Integral The student will learn about the area under a curve defining the definite integral.
What does a computer program look like: a general overview.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
Working with arrays (we will use an array of double as example)
Introduction to programming in the Java programming language.
Assignment statements using the same variable in LHS and RHS.
Maybe add in chad’s idea of the area of polygons limiting to the area of a circle. Nice animation to link to online At
Random Sampling Approximations of E(X), p.m.f, and p.d.f.
Mixing integer and floating point numbers in an arithmetic operation.
Recap of Functions. Functions in Java f x y z f (x, y, z) Input Output Algorithm Allows you to clearly separate the tasks in a program. Enables reuse.
In this section, we will investigate how to estimate the value of a definite integral when geometry fails us. We will also construct the formal definition.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
The Bisection Method. Introduction Bisection Method: Bisection Method = a numerical method in Mathematics to find a root of a given function.
The life time of local variables (in a method). Local variables Local variable: A local variable is used to store information that is relevant for the.
Integer numerical data types. The integer data types (multiple !) The integer data types use the binary number system as encoding method There are a number.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Chapter 15 – Multiple Integrals 15.1 Double Integrals over Rectangles 1 Objectives:  Use double integrals to find volumes  Use double integrals to find.
The Quick Guide to Calculus. The derivative Derivative A derivative measures how much a function changes for various inputs of that function. It is.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
Arithmetic expressions containing Mathematical functions.
2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.
C.1.3b - Area & Definite Integrals: An Algebraic Perspective Calculus - Santowski 1/7/2016Calculus - Santowski 1.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Creating Web Services Presented by Ashraf Memon Presented by Ashraf Memon.
Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Simple algorithms on an array - compute sum and min.
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
To find the area under the curve Warm-Up: Graph. Area under a curve for [0, 3]  The area between the x-axis and the function Warm-up What is the area.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
The if-else statement. Introducing the if-else statement Programming problem: Re-write the a,b,c-formula program to solve for complex number solutions.
[5-4] Riemann Sums and the Definition of Definite Integral Yiwei Gong Cathy Shin.
NUMERICAL INTEGRATION
Lesson 47 - Area under Curves as Limits and Sums
Converting a Definite Integral to a limit of a Riemann Sum and converting a limit of a Riemann Sum to a Definite Integral This template can be used as.
L 12 application on integration
The Area Question and the Integral
The Quick Guide to Calculus
TECHNIQUES OF INTEGRATION
The Boolean (logical) data type boolean
Area & Riemann Sums Chapter 5.1
MATLAB Tutorial ECE 002 Professor S. Ahmadi.
The for-statement.
Presentation transcript:

The Rectangle Method

Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with fixed end point a and b: The integral of a function f(x) is equal to the area under the graph of f(x). Graphically explained:

Introduction (cont.) Rectangle Method: The rectangle method (also called the midpoint rule) is the simplest method in Mathematics used to compute an approximation of a definite integral.

Rectangle Method: explained Rectangle Method: 1.Divide the interval [a.. b] into n pieces; each piece has the same width:

Rectangle Method: explained (cont.) The width of each piece of the smaller intervals is equal to: b - a width = n

Rectangle Method: explained (cont.) 2. The definite integral (= area under the graph is approximated using a series of rectangles:

Rectangle Method: explained (cont.) The area of a rectangle is equal to: We (already) know the width of each rectangle: area of a rectangle = width × height b - a width = n

Rectangle Method: explained (cont.) The height of the rectangles: The different rectangles has different heights Heights of each rectangle: The heights of each rectangle = the function value at the start of the (small) interval

Rectangle Method: explained (cont.) Example: first interval First (small) interval: [a.. (a + width)] (remember that: width = (b − a)/n) Height of first (small) interval:

Rectangle Method: explained (cont.) Therefore: height of first rectangle = f(a) Area of the rectangle = f(a) × width

Rectangle Method: explained (cont.) Example: second interval the second (small) interval is [(a+width).. (a+2width)] (remember that: width = (b − a)/n) Height of first (small) interval:

Rectangle Method: explained (cont.) Therefore: height of first rectangle = f(a+width) Area of the rectangle = f(a+width) × width

Rectangle Method: explained (cont.) Example: third interval the third (small) interval is [(a+2width).. (a+3width)] (remember that: width = (b − a)/n) Height of first (small) interval:

Rectangle Method: explained (cont.) Therefore: height of first rectangle = f(a+2width) Area of the rectangle = f(a+2width) × width

Rectangle Method: explained (cont.) We see a pattern emerging: Height of rectangle 1 = f(a + 0×width) Height of rectangle 2 = f(a + 1×width) Height of rectangle 3 = f(a + 2×width)... Height of rectangle n−1 = f(a + (n−2)×width) Height of rectangle n = f(a + (n−1)×width)

Rectangle Method: explained (cont.) Note: there are n (smaller) interval in total. Conclusion: Height of rectangle i = f( a + (i-1)*width ) = the function value at the point "a + (i-1)*width" b-a Recall that: width = n

Rectangle Method: explained (cont.) The area of the rectangles: This figure helps you to visualize the computation:

Rectangle Method: explained (cont.) The width of rectangle i is equal to: b - a width = n

Rectangle Method: explained (cont.) The height of rectangle i is equal to: height = f( a + (i-1)×width )

Rectangle Method: explained (cont.) The area of rectangle i is equal to: area = width × height = width × f( a + (i-1)×width )

Rectangle Method: explained (cont.) The approximation of the definite integral: Approximation = sum of the area of the rectangles = area of rectangle 1 + area of rectangle area of rectangle n = width × f( a + (1-1) × width ) + width × f( a + (2-1) × width ) width × f( a + (n-1) × width )

The general running sum algorithm We have seen a running sum computation algorithm previously that adds in simple series of numbers: Compute the sum: n Running sum algorithm: sum = 0; // Clear sum for ( i = 1; i <= n ; i++ ) { sum = sum + i; // Add i to sum }

The general running sum algorithm (cont.) The running sum algorithm can be generalized to add a more general series Example: compute i n 2 The i th term in the sum = i 2 Therefore, the running sum algorithm to compute this sum is: sum = 0; // Clear sum for ( i = 1; i <= n ; i++ ) { sum = sum + i*i; // Add i 2 to sum }

Algorithm to compute the sum of the area of the rectangles We can use the running sum algorithm to compute the sum of the area of the rectangles

Algorithm to compute the sum of the area of the rectangles (cont.) Recall: Approximation = sum of the area of the rectangles = width × f( a + (1-1) × width ) + width × f( a + (2-1) × width ) width × f( a + (i-1) × width ) width × f( a + (n-1) × width ) Where: width = (b - a) / n

Algorithm to compute the sum of the area of the rectangles (cont.) The i th term of the running sum is equal to: i th term = width × f( a + (i-1)×width )

Algorithm to compute the sum of the area of the rectangles (cont.) Algorithm to compute the sum of the area of the rectangles: Variables: double w; // w contains the width double sum; // sum contains the running sum Algorithm to compute the sum: w = (b - a)/n; // Compute width sum = 0.0; // Clear running sum for ( i = 1; i <= n; i++ ) { sum = sum + w*f( a + (i-1)*w ); }

The Rectangle Method in Java Rough algorithm (pseudo code): input a, b, n; // a = left end point of integral // b = right end point of integral // n = # rectangles used in the approximation Rectangle Method: w = (b - a)/n; // Compute width sum = sum of area of the n rectangles; // Compute area print sum; // Print result

The Rectangle Method in Java (cont.) Algorithm in Java: public class RectangleMethod01 { public static void main(String[] args) { double a, b, w, sum, x_i; int i, n; **** Initialize a, b, n **** /* The Rectangle Rule Algorithm */

The Rectangle Method in Java (cont.) w = (b-a)/n; // Compute width sum = 0.0; // Clear running sum for ( i = 1; i <= n; i++ ) { x_i = a + (i-1)*w; // Use x_i to simplify formula... sum = sum + ( w * f(x_i) ); // width * height of rectangle i } System.out.println("Approximate integral value = " + sum); }

The Rectangle Method in Java (cont.) Example 1: compute 0 ∫ 1 x 3 dx (the exact answer = 0.25) public class RectangleMethod01 { public static void main(String[] args) { double a, b, w, sum, x_i; int i, n; a = 0.0; b = 1.0; // 1 ∫ 2 x 3 dx n = 1000; // Use larger value for better approximation /* The Rectangle Rule Algorithm */

The Rectangle Method in Java (cont.) w = (b-a)/n; // Compute width sum = 0.0; // Clear running sum for ( i = 1; i <= n; i++ ) { x_i = a + (i-1)*w; sum = sum + ( w * (x_i * x_i * x_i) ); // f(x_i) = (x_i) 3 } System.out.println("Approximate integral value = " + sum); }

The Rectangle Method in Java (cont.) Example Program: (Demo above code) –Prog file: RectangleMethod01.java How to run the program: Right click on link and save in a scratch directory To compile: javac RectangleMethod01.java To run: java RectangleMethod01 Output: Approximate integral value = Exact answer: 0.25

The Rectangle Method in Java (cont.) Example: compute 1 ∫ 2 (1/x) dx (the answer = ln(2)) public class RectangleMethod02 { public static void main(String[] args) { double a, b, w, sum, x_i; int i, n; a = 1.0; b = 2.0; // 1 ∫ 2 (1/x) dx n = 1000; // Use larger value for better approximation /* The Rectangle Rule Algorithm */

The Rectangle Method in Java (cont.) w = (b-a)/n; // Compute width sum = 0.0; // Clear running sum for ( i = 1; i <= n; i++ ) { x_i = a + (i-1)*w; sum = sum + ( w * (1/x_i) ); // f(x_i) = 1/x_i } System.out.println("Approximate integral value = " + sum); }

The Rectangle Method in Java (cont.) Example Program: (Demo above code) –Prog file: RectangleMethod02.java How to run the program: Right click on link and save in a scratch directory To compile: javac RectangleMethod02.java To run: java RectangleMethod02 Output: Approximate integral value = Exact answer: ln(2) =

The effect of the number of rectangles used in the approximation Difference in the approximations when using different number of rectangles:

The effect of the number of rectangles used in the approximation (cont.) Clearly: Using more rectangles will give us a more accurate approximation of the area under the graph

The effect of the number of rectangles used in the approximation (cont.) However: Using more rectangles will make the algorithm add more numbers I.e., the algorithm must do more work --- it must adds more smaller values (because the rectangles are smaller and have smaller areas))

The effect of the number of rectangles used in the approximation (cont.) Trade off: Often, in computer algorithms, a more accurate result can be obtained by a longer running execution of the same algorithm We call this phenomenon: trade off You cannot gain something without give up on something else

The effect of the number of rectangles used in the approximation (cont.) You can experience the trade off phenomenon by using n = in the above algorithm. It will run slower, but give you very accurate results ! Outputs for n = : 1.Approximate integral value of 0 ∫ 1 x 3 dx = Approximate integral value of 1 ∫ 2 (1/x) dx =