CS 221 Lecture 11 Numerical Integration Tuesday, 22 November 2011 - - PowerPoint PPT Presentation

cs 221 lecture 11 numerical integration
SMART_READER_LITE
LIVE PREVIEW

CS 221 Lecture 11 Numerical Integration Tuesday, 22 November 2011 - - PowerPoint PPT Presentation

CS 221 Lecture 11 Numerical Integration Tuesday, 22 November 2011 Todays Agenda 1. Announcements 2. Numerical Integration 3. Class Quiz 3 Announcements Last In-Class Quiz is today. No Labs Thursday Happy


slide-1
SLIDE 1

CS 221 Lecture 11 Numerical Integration

Tuesday, 22 November 2011

slide-2
SLIDE 2

Today’s Agenda

  • 1. Announcements
  • 2. Numerical Integration
  • 3. Class Quiz 3
slide-3
SLIDE 3

Announcements

  • Last In-Class Quiz is today.
  • No Labs Thursday – Happy Thanksgiving!

– Remember to be thankful.

  • Lab Quiz 3 next Thursday, 1 December

– Coverage: simultaneous equation-solving, programming – More details in next Lecture

slide-4
SLIDE 4

– –

slide-5
SLIDE 5
slide-6
SLIDE 6
slide-7
SLIDE 7
slide-8
SLIDE 8
slide-9
SLIDE 9
slide-10
SLIDE 10
slide-11
SLIDE 11
slide-12
SLIDE 12
slide-13
SLIDE 13
slide-14
SLIDE 14
slide-15
SLIDE 15

Numerical Integration

  • Sometimes it’s difficult or impossible to integrate

a function symbolically

– Example:

The antiderivative of f(x) = exp(-x2) can’t be expressed in elementary form.

– Example:

The function f(x) may have been obtained by measurements (sampling), so there’s no symbolic definition to work with.

  • Numerical integration provides a way to

determine the definite integral (area under a given region of the curve of f(x))

slide-16
SLIDE 16
slide-17
SLIDE 17
slide-18
SLIDE 18
slide-19
SLIDE 19
slide-20
SLIDE 20
slide-21
SLIDE 21
slide-22
SLIDE 22
slide-23
SLIDE 23
slide-24
SLIDE 24

Built-in Integration Functions

  • Quadrature: numerical integration of a function of
  • ne variable
  • quad(fun,a,b) computes the integral of fun(x)

from a to b

  • Uses adaptive Simpson quadrature

– To within error tolerance of 10-6 – fun(x) must be vectorizable (uses only vector’able ops)

  • Some functions are not vectorizable
  • quad() silently gives the wrong answer!
  • Other variations: quadl(), quadgk()
slide-25
SLIDE 25

Example

function y = fooey(x) if x < 1 y = -x + 2; else y = (x-1)^2 + 1; end end Want to compute the definite integral of fooey(x) from 0 to 3. This function is not vectorizable (because of the “if-else”.

– Correct answer: 6.1667

slide-26
SLIDE 26

Implementing Trapezoidal Integration

  • Compute trapezoid width:

(upper – lower)/(number of slices)

  • Compute vertices of all the trapezoids

– linspace(lower,upper,n):

Creates a vector of n equally-spaced elements with first element = lower, last element = upper

  • Step through the vector of vertices, computing

the area of each trapezoid and adding it to the total area

– for loop