Unformatted text preview:

Numerical Methods in MATLABCenter for Interdisciplinary Research and ConsultingDepartment of Mathematics and StatisticsUniversity of Maryland, Baltimore Countywww.umbc.edu/circWinter 2008Mission and Goals: The Center for Interdisciplinary Research and Consulting(CIRC) is a consulting service on mathematics and statistics provided by the Depart-ment of Mathematics and Statistics at UMBC. Established in 2003, CIRC is dedicatedto support interdisciplinary research for the UMBC campus community and the pub-lic at large. We provide a full range of consulting services from free initial consultingto long term support for research programs.CIRC offers mathematical and statistical expertise in broad areas of applications,including biological sciences, engineering, and the social sciences. On the mathematicsside, particular strengths include techniques of parallel computing and assistance withsoftware packages such as MAT LAB and COMSOL Multiphysics (formerly known asFEMLAB). On the statistics side, areas of particular strength include Toxicology,Industrial Hygiene, Bioequivalence, Biomechanical Engineering, Environmental Sci-ence, Finance, Information Theory, and packages such as SAS, SPSS, and S-Plus.Copyrightc 2003–2008 by the Center for Interdisciplinary Research and Consult-ing, Department of Mathematics and Statistics, University of Maryland, BaltimoreCounty. All Rights Reserved.This tutorial is provided as a service of CIRC to the community for personal usesonly. Any use beyond this is only acceptable with prior permission from CIRC.This document is under constant development and will periodically be updated. Stan-dard disclaimers apply.Acknowledgements: We gratefully acknowledge the efforts of the CIRC researchassistants and students in Math/Stat 750 Introduction to Interdisciplinary Consultingin developing this tutorial series.MATLAB is a registered trademark of The MathWorks, Inc., www.mathworks.com.31 IntroductionIn this tutorial, we will introduce some of the numerical methods available in Matlab.Our goal is to provide some snap-shots of the wide variety of computational tools thatMatlab provides. We will look at some optimization routines, where we mainly focuson unconstrained optimization.Next, we discuss curve fitting and approximation offunctions using Matlab. Our final topic will be numerical ODEs in Matlab.Matlab provides a number of specialized toolboxes, which extend the capabilitiesof the software. We will have a brief overview of the various toolboxes in Matlab andwill provide a list of some available toolboxes.• Numerical Optimization• Data Fitting / Approximation• Numerical ODEs• Matlab Toolboxes2 Unconstrained OptimizationThe commands we discuss in this section are two of the several optimization routinesavailable in Matlab. First we discuss fminbnd, which is used to minimize functionsof one variable. The command,[x fval] = fminbnd(f, a, b)finds a local minimizer of the function f in the interval [a, b]. Here x is the localminimizer found by the command and fval is the value of the function f at that point.For complete discussion of fminbnd readers can refer to the Matlab’s documentations.Here we illustrate the use of fminbnd in an example.Consider the function f (x) = cos(x) − 2 ln(x) on the interval, [π2, 4π]. The plot ofthe function is depicted in Figure 1. We can first define the function f (x) in Matlabusingf = @(x)(cos(x) - 2*log(x))Then we proceed by the following,>> [x fval] = fminbnd(f, 2, 4)x =3.71084 2 UNCONSTRAINED OPTIMIZATIONFigure 1: Plot of f (x) = cos(x) − 2 ln(x).fval =-3.4648We see that fminbnd returns the (approximate) minimizer of f(x) in the interval [2, 4]and also computes the value of f(x) at the minimizer. The readers can try to find the(global) minimizer of f (x) which is seen to be somewhere in [8, 10] using fminbnd.The next (unconstrained) optimization command we discuss is fminsearch whichcan be used to find a local minimizer of a function of several variables. The command,[x fval] = fminsearch(f,x0)finds a local minimizer of the function f given the initial guess x0. For completediscussion of fminsearch readers can refer to the Matlab’s documentations. Here weillustrate the use of fminsearch in an example.For a simple example, we minimize the function f(x, y ) = x2+ y2which clearlyhas its minimizer at (0, 0). Let’s choose an initial guess of x0= (0, 0). The followingMatlab commands illustrate the usage of fminsearch>> f = @(x)(x(1)^2+x(2)^2);>> x0 = [1;1];>> [x fval] = fminsearch(f, x0)x =1.0e-04 *-0.210250.2548fval =1.0915e-09Of course, reader can try fminsearch on more complicated problems and see theresults.For more information on optimization routines in Matlab, reader can investigateMatlab’s Optimization To olbox which includes several powerful optimization toolswhich can be used to solve both unconstrained and constrained linear and non-linearoptimization problems.3 Curve-FittingHere we consider the problem of fitting a polynomial of degree (at most) k into thedata points (xi, yi); to do this, we use the command,p = polyfit(x, y, k)which fits a polynomial of degree k into the given data points. The output argumentp is a vector which contains the coefficients of the interpolating polynomial. Themethod used is least squares, in which we choose a polynomial P of degree k whichminimizes the following:mXi=1[P (xi) − yi]2. (3.1)For example, say we are given the data pointsxi yi--------1 12 0.53 14 2.55 36 47 5Suppose we would like to fit a polynomial of degree three into the given data points.We can use the following Matlab commands to get the interpolating polynomial.>> xi = 1 : 7;>> yi = [1 0.5 1 2.5 3 4 5];>> p = polyfit(xi,yi,3);6 3 CURVE-FITTINGOnce we have the vector p which contains the coefficients of the interpolating poly-nomial we can use the Matlab function ployval to evaluate the approximating poly-nomial over a given range of x values. We can proceed as follows:>> x = 1 :0.1: 7;>> y = polyval(p, x);>> plot(xi, yi, ’*’, x, y)Which produces the Figure 2.Figure 2: Data fitting in MatlabAnother useful idea is using polyfit to find an approximating polynomial for agiven function. The idea is useful because polynomials are much simpler to work with.For example one can easily integrate or differential polynomials while it may not beso easy to do the same for a function which is not so well behaved. As an example, weconsider the problem of approximating the function sin(p(x)) on the interval [0,


View Full Document

UMBC MATH 426 - Numerical Methods in MATLAB

Download Numerical Methods 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 Numerical Methods 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 Numerical Methods 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?