Unformatted text preview:

MATH2071: LAB 1(b): Selected topics in MatlabIntroduction Exercise 1Adaptive Quadrature Exercise 2Roundoff errors Exercise 3Exercise 4Exercise 5Exercise 6Exercise 7Exercise 8Exercise 91 Introd uctionThere are two versions of the first lab. This version discusses some special topics and is intended for studentswho took Math 2070. If you have not already taken Math 2070, please see Lab 1(a). That version of the firstlab introduces the Matlab environment and programming language, and presents the general format of thework you need to hand in. Students who take Lab 1(a) will not be in no way dis advantaged in this coursebecause they “missed” Lab 1(b). If you prefer, you will find a version of this lab in Adobe pdf fo rmat here .This version of the first lab is intended only for students who havealready taken Math 2070.This lab is concerned with several different topics. It covers material that is supplemental for studentsin Math 2071, but new students will not be shortchanged when they miss this material in favor of theintroductory material presented in Lab 1(a).The first topic discussed in this lab is a simple approach in a two-dimensional adaptive integration routine.The difference between the work in this lab and in last semester’s homework assignment is that this lab willbe using square mesh elements instead of triangular ones and an elementary technique fo r determining whichmesh elements need to be refined in order to meet the error requir ements.The sec ond topic is a demonstra tion of how roundoff erro r arises in a matrix calculation.The third topic is a brief introduction to ordinary differential equations. This topic is also covered inLab 1(a). The methods presented here will be use d to motivate some of the linear algebra algorithms in thefollowing labs.2 Adaptive QuadratureIn this section you will construct a Matlab function to compute the integral of a given mathematical functionover a square region in the plane. One way to do such a task would be to regar d the square to be the Cartesianproduct of two one-dimensional lines and integrate using a one-dimensional adaptive quadrature r outine suchas adaptquad from last semester. Instead, in this lab you will be looking at the square as a region in theplane and you will be dividing it up into many (square) s ubregions, computing the integral of the givenfunction over each subregion, and adding them up to get the integral over the given square.The basic outline of the method used in this lab is the following:1. Start with a list containing a sing le item: the square region of integration.12. Use a Gaußian integration rule to integrate the function over each subregion in the list and estimatethe resulting error of integration. The integral over the whole region is the sum of the integrals overthe subregions, as is the estimated error.3. If the total estimated error of the integral is too large, find the subregion with larges t error, divide itinto four subregions, and replace the single subregion with four smaller subregions, and r e tur n to theprevious s tep.The way the notion of a “list” is implemented will introduce a data s tructure (discussed in deta il below)that is more versatile than arrays or matrices.2.1 Two-dimensional Gauß quadratureOne simple way of deriving a two-dimensional integration formula over a square is to use iterated integration.Recall that a one-dimensional Gauß integration rule can be written asZx+hxf(x)dx ≈NXn=1wnf(xn). (1)Here, N is the index of the rule. For the case N = 2, the points xnare x + h/2 ± h/(2√3) and the weightsare w1= w2= h/2. The degree of precision is 3, and the error is proportional to h5max|f′′′′|. (If you lookup the error in a reference somewhere, you will notice that the error is usually given as proportional to h4,not h5. The extra power o f h appe aring in (1) comes from the fact that the region of integration is [x, x+h].)Applying (1) twice in the iterated integralZx+hxZy+hyf(x, y)dxdy ≈NXn=1MXm=1wnwmf(xn, ym). (2)For the case N = M = 2, (2) becomesZx+hxZy+hyf(x, y)dxdy ≈4Xn=1(h2/4)f(xn, yn), (3)where the points (xn, yn) = (x + h/2 ± h/(2√3), y + h/2 ± h/(2√3)). The error is O(h6), and (3) is exactfor monomia ls xnymwith n ≤ 3 and m ≤ 3, and for sums of such monomials. In the following exercise, youwill implement this method in Matlab.Exercise 1:(a) Write a Matlab function to compute the integral of a function over a s ingle square element using(3) with (xn, yn) = (x + h/2 ± h/(2√3), y + h/2 ± h/(2√3)). Name the function m-file q elt.mand have it beginfunction q=q_elt(f,x,y,h)% q=q_elt(f,x,y,h)% INPUT% f=???% x=???% y=???% h=???% OUTPUT% q=???% your name and the date2(b) Test qelt on the functions 1, xy, x2y, x2y2, and x3y3over the squa re [0, 1] ×[0, 1] and show thatthe result is exact, up to roundoff.(c) Test qelt on the function x4y4to see that it is not exact, thus showing the degree of precisio n is3.2.2 Error estimationIn order to do any sort of adaptive quadrature, you need to be able to estimate the error in one element.Remember, this is only an estimate because without the true value of the quadrature, you cannot get thetrue error.Suppose you have a square e le ment with side of length h. If you divide it into four sub-squares with sidesof length h/2, then you can compute the quadrature twice: once on the single square with side of length hand once by adding up the four quadratures over the four squares with sides of length h/2. Consider thefollowing figure.hh1 234Denote the true integral over this square as q and its approximation over the square with side of length has qh. Denote the four approximate integrals over the four squares with sides of length h/2 as q1h/2, q2h/2,q3h/2, and q4h/2. Assuming that the fourth derivative of f is roughly constant over the squares, the followingexpressions can be written.qh= q + Ch6q1h/2+ q2h/2+ q3h/2+ q4h/2= q + 4C(h/2)6= q + (4/64)Ch6.This system of eq uations can be solved for the error in qhaserror = Ch6=6460qh− (q1h/2+ q2h/2+ q3h/2+ q4h/2)(4)In the following exercise you will wr ite a Matlab function to estimate both the integral and the error ove r asingle element.Exercise 2:(a) Write an m- file named qerrelt.m to estimate the error according to (4). Use q elt.m to evaluatethe integrals. qerr elt.m should b egin3function [q,errest]=qerr_elt(f,x,y,h)% q=qerr_elt(f,x,y,h)% more comments% your name and the date(b) Use qerr elt to estimate the integral and e rror for the


View Full Document

Pitt MATH 2071 - Selected topics in Matlab

Download Selected topics in Matlab
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Selected topics in Matlab and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Selected topics in Matlab 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?