NUMERICAL INTEGRATION AND FUNCTION FUNCTIONS

Similar documents
ARRAY VARIABLES (ROW VECTORS)

Part 1: Numerical integration reloaded

Numerical Integration

SYDE 312 UNIT 4: EXTRA QUADRATURE PROBLEMS

University of Alberta

LINEARIZATION, NEWTON S METHOD BACKGROUND PREPARATIONS

4.7 Approximate Integration

Math 226A Homework 4 Due Monday, December 11th

AMATH 352: MATLAB Tutorial written by Peter Blossey Department of Applied Mathematics University of Washington Seattle, WA

Math 250A (Fall 2009) - Lab I: Estimate Integrals Numerically with Matlab. Due Date: Monday, September 21, INSTRUCTIONS

ELEMENTARY MATLAB PROGRAMMING

Euler s Method with Python

Introduction to Engineering gii

Math F302: Octave Miscellany September 28, e 1 x3 dx. Here s how to find a numerical approximation with Octave

Matlab Tutorial. Get familiar with MATLAB by using tutorials and demos found in MATLAB. You can click Start MATLAB Demos to start the help screen.

B. Examples Set up the integral(s) needed to find the area of the region bounded by

Peter Hertel. University of Osnabrück, Germany. Lecture presented at APS, Nankai University, China.

Solutions to Midterm Exam

Numerical Methods for Differential Equations Contents Review of numerical integration methods Rectangular Rule Trapezoidal Rule Simpson s Rule How to

Mathematical Operations with Arrays and Matrices

Consider this m file that creates a file that you can load data into called rain.txt

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

Section 6.1 Estimating With Finite Sums

EE 301 Lab 1 Introduction to MATLAB

Math 1206 Calculus Sec. 5.6: Substitution and Area Between Curves (Part 2) Overview of Area Between Two Curves

Introduction to Matlab

LECTURE 0: Introduction and Background

D01ARF NAG Fortran Library Routine Document

Queens College, CUNY, Department of Computer Science Numerical Methods CSCI 361 / 761 Fall 2018 Instructor: Dr. Sateesh Mane

Homework Project #6. Math 365, Spring Due Wednesday April 27th

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

MACM 204 Makeup Assignment - Practice Final.

Numerical Methods with Matlab: Implementations and Applications. Gerald W. Recktenwald. Chapter 11 Numerical Integration

Algorithms and Data Structures

Mathematical Methods and Modeling Laboratory class. Numerical Integration of Ordinary Differential Equations

MATLAB/Octave Tutorial

FUNCTIONS AND MODELS

Numerical Methods in Engineering Sciences

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

Math 113 Exam 1 Practice

7.8. Approximate Integration

17 USING THE EDITOR AND CREATING PROGRAMS AND FUNCTIONS

Numerical Methods Lecture 7 - Optimization

Objectives. Materials

A very brief Matlab introduction

Math 32B Discussion Session Week 2 Notes January 17 and 24, 2017

Introduction to MATLAB

Introduction to Computer Programming with MATLAB Calculation and Programming Errors. Selis Önel, PhD

Sharp EL-9900 Graphing Calculator

MTH 122 Calculus II Essex County College Division of Mathematics and Physics 1 Lecture Notes #11 Sakai Web Project Material

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method.

NAG Fortran Library Routine Document D01AJF.1

1 Introduction to Matlab

The Bisection Method versus Newton s Method in Maple (Classic Version for Windows)

Matlab notes Matlab is a matrix-based, high-performance language for technical computing It integrates computation, visualisation and programming usin

MATLAB Part 1 Oct

How to learn MATLAB? Some predefined variables

Part V Appendices c Copyright, Todd Young and Martin Mohlenkamp, Department of Mathematics, Ohio University, 2017

STAT/MATH 395 A - PROBABILITY II UW Winter Quarter Matlab Tutorial

Calculators ARE NOT Permitted On This Portion Of The Exam 28 Questions - 55 Minutes

Dr Richard Greenaway

Using Arithmetic of Real Numbers to Explore Limits and Continuity

A very short introduction to Matlab and Octave

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

SF1901 Probability Theory and Statistics: Autumn 2016 Lab 0 for TCOMK

A Brief Introduction to Mathematica

PART 1 PROGRAMMING WITH MATHLAB

ELEC6021 Research Methods Matlab Session 2

Introduction to Matlab

INTRODUCTION TO DERIVE - by M. Yahdi

Precalculus, Quarter 2, Unit 2.1. Trigonometry Graphs. Overview

nag 1d quad gauss 1 (d01tac)

SPM Add Math Form 5 Chapter 3 Integration

Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

A Low Level Introduction to High Dimensional Sparse Grids

1.00 Lecture 32. Integration. Reading for next time: Numerical Recipes Packaging Functions in Objects

Friday, 11 January 13. Interpolation

Surname. Other Names. Centre Number. Candidate Number. Candidate Signature. General Certificate of Education Advanced Level Examination June 2014

A = [1, 6; 78, 9] Note: everything is case-sensitive, so a and A are different. One enters the above matrix as

QUICK INTRODUCTION TO MATLAB PART I

MAT128A: Numerical Analysis Lecture Two: Finite Precision Arithmetic

Lab#1: INTRODUCTION TO DERIVE

AMTH142 Lecture 10. Scilab Graphs Floating Point Arithmetic

CSE/Math 485 Matlab Tutorial and Demo

Monte Carlo Method for Definite Integration

MATLAB Modul 3. Introduction

Introduction to MATLAB for Engineers, Third Edition

Chapter 1 Introduction to MATLAB

Direction Fields; Euler s Method

AMS 27L LAB #2 Winter 2009

0.1 Numerical Integration Rules Using Maple

TEAMS National Competition High School Version Photometry Solution Manual 25 Questions

nag 1d quad gauss (d01bac)

Problem #3 Daily Lessons and Assessments for AP* Calculus AB, A Complete Course Page Mark Sparks 2012

COMPUTING AND DATA ANALYSIS WITH EXCEL. Numerical integration techniques

East Penn School District Secondary Curriculum

MATH2070: LAB 3: Roots of Equations

1 extrema notebook. November 25, 2012

4.0 Programming with MATLAB

Transcription:

NUMERICAL INTEGRATION AND FUNCTION FUNCTIONS The arguments of functions previously considered had constants, or vector or matrix variables as arguments. In this section we will see how to set up a function to process another function as one of its arguments, as well as, explore the integration process with a computer. % You should recall basic numerical integration concepts including simple rectangular approximations, as well as, the trapezoidal rule. The next function will use this rule to calculate the definite integration of any well behaved function. NOTE: Any well behaved function means you do not have to memorize the indefinite integral of a function like the integral of cos() is sin() etc or look in ancient texts for worked out integrals of exotic function which was done a great deal in the 19 th century. Let s review the Trapezoidal rule, study the figures below to the left. Remember integral = area under the curve. We want the area from x = a to x = b in this case. We have divided the wanted area into 3 parts. So the interval between the x values is simple x=(b-a)/3. Each area is a rectangle plus triangle we call the combination a trapezoid. A magnified view of two sections is below. Area = area rectangle +area of triangle which becomes for the magnified view Area = area rect + area of triabgle + next rect etc Area= X*f(x1)+1/2 X*(f(xo) - f(x1))+ X*f(x2)+ 1/2 X*(f(x1) - f(x2)) X*f(x3)+ 1/2 X*(f(x2) - f(x3)) = X*{ f(xo)/2+ f(x1)- f(x1)/2+ 1/2f(x1) + f(x2)- f(x2)/2+ 1/2f(x2)+ f(x3)- f(x3)/2} = X*{ f(xo)/2+ f(x1)+ f(x2)+ f(x3)/2} = formula under the curve! = f(x0) (f(xo) - f(x1)) f(x1) (f(x1) - f(x2)) f(x2) (f(x2) - f(x3)) f(x3) xo x1 x2 x3 <------- x----- a Formally, we get for n divisions which is best. The more divisions,n, the better the integral value compared to the definite integral of known functions, except too many could create problems. b

TRAPEZOIDAL INTEGRATION PROGRAM (example of a function of a function) We now construct an m-file to do trapezoidal integration.» type area1 % This program estimates area under curve using trapezoids % USING A SPECIFIED n number of trapezoids, over the limits a to b the function func! >> type area1 function y=area1(func,a,b,n) total=0; base=(b-a)/n; for k=2:n xk= a+base*(k-1); total =total +feval(func,xk); end y = 0.5.* base.* (feval(func,a) + 2.0.* total + feval(func,b)); func is the name of the function we will take the integral of % The special function feval as defined below permits us to use a function name when we have a function that needs another function as an argument.» help feval FEVAL Function evaluation. If F is a string containing the name of a function (usually defined by an M-file), then FEVAL(F,x1,...,xn) evaluates that function at the given arguments. For example, F = 'foo', FEVAL(F,9.64) is the same as foo(9.64). FEVAL is usually used inside functions which have the names of other functions as arguments. So the function we are going to integrate must be passed the FEVAL as a String. % lets start with the built in function sin() Using area1 to evaluate the integral of sin() between 0 and pi DO THIS NOW IN CLASS! The answer is? Remember the trapezoidal rule is a basic but primitive calculation we see this with The answers. Taking 20 divisions first then a 100 improves the results. >> area1('sin',0,pi,20) ans = 1.9959 >> area1('sin',0,pi,100) ans = 1.9998 %Since we are passing a function or our function area1 to be evaluated We say area1 is said to be a function function.

Another example is to use area1 to evaluate a function we define. Suppose we define an exponential function called fexp as» type fexp function y = fexp(x) y=4* exp(-x); % We now can get the integral of this function using our area1 function which needs the name of the function to integrate as well as the limits on the x-axis as well as how many trapezoids to use. So for examples» area1('fexp',0,1,5) ans = 2.5369» area1('fexp',0,1,10) ans = 2.5306» area1('fexp',0,1,50) ans = 2.5286 ans = 2.5285 %Here we have been getting answers in 4 decimal accuracy. As you know MATLAB actually does calculations to a high precision (double precision) in memory. We can increase the number of decimals, we output, by using the MATLAB format command. As in:» format long % Then when we run a program we get, for example ans = 2.52850330596441 % We note an, we can consider an exact value to the integral can be calculated and to electronic calculator accuracy, my Iphone! 2.52848223514231 our answer with 100 subdivisions of the area only agrees to 3 decimals in long form. This is because our integral function uses the trapezoidal approximation. We can do a lot Better. Matlab has more advance built in functions. Let us focus on the intrinsic routine quad() help quad quad Numerically evaluate integral, adaptive Simpson quadrature. Q = quad(fun,a,b) tries to approximate the integral of scalar-valued function FUN from A to B to within an error of 1.e-6 using recursive adaptive Simpson quadrature. FUN is a function handle. The function Y=FUN(X) should accept a vector argument X and return a vector result Y, the integrand evaluated at each element of X. Q = quad(fun,a,b,tol) uses an absolute error tolerance of TOL instead of the default, which is 1.e-6. Larger values of TOL result in fewer function evaluations and faster computation, but less accurate results. The quad function in MATLAB 5.3 used a less reliable algorithm and a default tolerance of 1.e-3. Etc. NOTE. Some advanced functions change when new versions of Matlab become available. Use help quad to see other integration schemes MATLAB has.

% redoing the sin() function example with quad gives us a much better ansew to the exact value of 2 for the integration from 0 to pi. >> quad('sin',0,pi) ans = 2.0000 >> format long >> quad('sin',0,pi) ans = 1.999999996398431 % Note again above the value from my ipone to the integral of fexp() 2.52848223514231 Compare the answers below with this last value. RETURNING FOR COMPARISON TO LESS DECIMALS» format short» q=quad('fexp',0,1) q = 2.5285» format long» q=quad('fexp',0,1) q = 2.528482235687844 % agreeing to 9 decimals to the iphone calculator value Even picking a large tolerance gives a good value since the routine is very robust in its convergence. >>quad('fexp',0,1,10) ans 2.528482240821508 % How good is this one? % we see the last is best but the others are not bad Noting again the value for comparison the trapezoidal rule. ans = 2.52850330596441 a 3 decimal match We can push the trapezoidal area1 like having 10,000 trapezoids get a better matching value. After a while one has to wonder how good is our calculator value? >> area1('fexp',0,1,10000) ans = 2.528482237421307 % In class let s look over the quad.m files with the 'type quad' and note how a professional mathematical package is done! % Be sure to check out in your careers the many numerical integration and differential functions Matlab provides. Run the demo to see the wealth of mathematical power you have with MATLAB >> demo

LABORATORY TASKS Show all work, especially any files you construct as well as the executions. 41. (2 pts) Try to get an exact value of the area with your calculator of the function above fexp between 0 and 1. What is this value and how many decimal places does the quad evaluation agree? 42. (10 pts) Recall function humps» type humps function y = humps(x) % Humps(x) is a function used by QUADDEMO and ZERODEMO. y = 1./ ((x-.3).^2 +.01) + 1./ ((x-.9).^2 +.04) - 6; a. Write a short m-file that will call upon the function And integrate it from 0 to 1 both by first constructing the function of function area1.m as above, using 100 trapezoids and then b. calling on the built in quad function to get the area. c. The program also plots humps from 0 to 1 very finely with 100 values of x. Looking at the graph d. WHICH OF THE two methods gave the best answer? Why or why not? Show all work! 43(10 pts). Integration fun. a. -Set up a function file for y(x)=34 exp(-0.2x) sin(3x+pi/8) call it whatever you want. In an one m-file do the following b. -Integrate this function from 0 to 30 using the quad() functions. c. - next use area1() to integrate this function. d. - Plot the function over the interval in steps of 0.01: Do a rough estimate of the area from the plot. e. -Compare the integrations over the same interval. Which is probably the best answer. Why? Or why not 44. (8 pts) Write a function function that sums all values of the function in 43 y(x)= for each value in the interval -8 to +8 with steps of.1. Note: This function called SUMFUN calls the function as an argument and uses feval() to get its answers (SEE THE area1 for feval use). Then run an m-file containing this function that gets the sum as specified. 45. (8 pts) EXTRA CREDIT Construct a function function that calculates the area from a to b when it divides any curve into n rectangular boxes which it sums up to get the area. Run it for the sin() as above as well as fexp() as above. Comment on its accuracy! For the sin() keep increasing n to see what happens. (0 to pi range). Comment!