Numerical Methods in MATLAB



Similar documents
Numerical Methods in MATLAB

Curve Fitting, Loglog Plots, and Semilog Plots 1

Simple Programming in MATLAB. Plotting a graph using MATLAB involves three steps:

1 Review of Newton Polynomials

Integration by substitution

INTERPOLATION. Interpolation is a process of finding a formula (often a polynomial) whose graph will pass through a given set of points (x, y).

November 16, Interpolation, Extrapolation & Polynomial Approximation

Linear Regression. use waist

Spline Toolbox Release Notes

Regression III: Advanced Methods

Taylor and Maclaurin Series

CD-ROM Appendix E: Matlab

Tutorial Program. 1. Basics

The Method of Least Squares. Lectures INF2320 p. 1/80

Curve Fitting with Maple

Computational Geometry Lab: FEM BASIS FUNCTIONS FOR A TETRAHEDRON

Math 4310 Handout - Quotient Vector Spaces

Nonlinear Regression Functions. SW Ch 8 1/54/

1 Review of Least Squares Solutions to Overdetermined Systems

I. Pointwise convergence

Beginner s Matlab Tutorial


4.3 Lagrange Approximation

Techniques of Integration

Review of Matlab for Differential Equations. Lia Vas

Increasing for all. Convex for all. ( ) Increasing for all (remember that the log function is only defined for ). ( ) Concave for all.

Matlab Practical: Solving Differential Equations

MATLAB Functions. function [Out_1,Out_2,,Out_N] = function_name(in_1,in_2,,in_m)

Section 4.4. Using the Fundamental Theorem. Difference Equations to Differential Equations

Fixed Point Theorems

CS 221. Tuesday 8 November 2011

The Method of Partial Fractions Math 121 Calculus II Spring 2015

Descriptive Statistics

Inner Product Spaces

G.A. Pavliotis. Department of Mathematics. Imperial College London

Creating, Solving, and Graphing Systems of Linear Equations and Linear Inequalities

Math Numerical Analysis Homework #2 Solutions

PROGRAMMING FOR CIVIL AND BUILDING ENGINEERS USING MATLAB

AC : MATHEMATICAL MODELING AND SIMULATION US- ING LABVIEW AND LABVIEW MATHSCRIPT

Irrational Numbers. A. Rational Numbers 1. Before we discuss irrational numbers, it would probably be a good idea to define rational numbers.

Data Analysis with MATLAB The MathWorks, Inc. 1

The Steepest Descent Algorithm for Unconstrained Optimization and a Bisection Line-search Method

MatLab - Systems of Differential Equations

1 Cubic Hermite Spline Interpolation

PRACTICAL GUIDE TO DATA SMOOTHING AND FILTERING

Lesson 10: Polynomials

Curve Fitting and Parameter Estimation

POLYNOMIAL AND MULTIPLE REGRESSION. Polynomial regression used to fit nonlinear (e.g. curvilinear) data into a least squares linear regression model.

Summary: Transformations. Lecture 14 Parameter Estimation Readings T&V Sec Parameter Estimation: Fitting Geometric Models

5.1 Derivatives and Graphs

Rolle s Theorem. q( x) = 1

Piecewise Cubic Splines

Least Squares Fitting of Data to a Curve. Gerald Recktenwald Portland State University Department of Mechanical Engineering gerry@me.pdx.

MATHEMATICS FOR ENGINEERING INTEGRATION TUTORIAL 3 - NUMERICAL INTEGRATION METHODS

Module 2 Introduction to SIMULINK

Objectives. Materials

Nonlinear Algebraic Equations. Lectures INF2320 p. 1/88

Lecture Notes on Polynomials

5 Numerical Differentiation

PLOTTING COMMANDS (!' ) "' # "*# "!(!' +,

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

6 Further differentiation and integration techniques

Equations, Inequalities & Partial Fractions

Quick Tour of Mathcad and Examples

SAGE, the open source CAS to end all CASs?

MATLAB Tutorial. Chapter 6. Writing and calling functions

Lecture 21 Integration: Left, Right and Trapezoid Rules

Bildverarbeitung und Mustererkennung Image Processing and Pattern Recognition

Optimal Resource Allocation for the Quality Control Process

Numerical Methods for Solving Systems of Nonlinear Equations

Investigating Parametric Curves with MATLAB

7.7 Solving Rational Equations

Maximum Likelihood Estimation by R

7.6 Approximation Errors and Simpson's Rule

Inner product. Definition of inner product

Solving Equations and Inequalities

Linear and quadratic Taylor polynomials for functions of several variables.

Numerical Methods for Differential Equations

GINI-Coefficient and GOZINTO-Graph (Workshop) (Two economic applications of secondary school mathematics)

0 Introduction to Data Analysis Using an Excel Spreadsheet

2 Integrating Both Sides

4.5 Linear Dependence and Linear Independence

Linear Models and Conjoint Analysis with Nonlinear Spline Transformations

Math Placement Test Practice Problems

f(x) = g(x), if x A h(x), if x B.

Numerical Methods with Excel/VBA:

Integration. Topic: Trapezoidal Rule. Major: General Engineering. Author: Autar Kaw, Charlie Barker.

INTRODUCTION (Syllabus, Numerical Methods & Computational Tools)

Numerical Analysis. Professor Donna Calhoun. Fall 2013 Math 465/565. Office : MG241A Office Hours : Wednesday 10:00-12:00 and 1:00-3:00

Basic Data Analysis Tutorial

Numerical Methods for Engineers

The KaleidaGraph Guide to Curve Fitting

Simple linear regression

A Hybridized Self-Organizing Response Surface Methodology

Transcription:

Numerical Methods in MATLAB Center for Interdisciplinary Research and Consulting Department of Mathematics and Statistics University of Maryland, Baltimore County www.umbc.edu/circ Winter 2008

Mission and Goals: The Center for Interdisciplinary Research and Consulting (CIRC) is a consulting service on mathematics and statistics provided by the Department of Mathematics and Statistics at UMBC. Established in 2003, CIRC is dedicated to support interdisciplinary research for the UMBC campus community and the public at large. We provide a full range of consulting services from free initial consulting to 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 mathematics side, particular strengths include techniques of parallel computing and assistance with software packages such as MATLAB and COMSOL Multiphysics (formerly known as FEMLAB). On the statistics side, areas of particular strength include Toxicology, Industrial Hygiene, Bioequivalence, Biomechanical Engineering, Environmental Science, Finance, Information Theory, and packages such as SAS, SPSS, and S-Plus. Copyright c 2003 2008 by the Center for Interdisciplinary Research and Consulting, Department of Mathematics and Statistics, University of Maryland, Baltimore County. All Rights Reserved. This tutorial is provided as a service of CIRC to the community for personal uses only. Any use beyond this is only acceptable with prior permission from CIRC. This document is under constant development and will periodically be updated. Standard disclaimers apply. Acknowledgements: We gratefully acknowledge the efforts of the CIRC research assistants and students in Math/Stat 750 Introduction to Interdisciplinary Consulting in developing this tutorial series. MATLAB is a registered trademark of The MathWorks, Inc., www.mathworks.com.

1 Introduction 3 In 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 that Matlab provides. We will look at some optimization routines, where we mainly focus on unconstrained optimization.next, we discuss curve fitting and approximation of functions using Matlab. Our final topic will be numerical ODEs in Matlab. Matlab provides a number of specialized toolboxes, which extend the capabilities of the software. We will have a brief overview of the various toolboxes in Matlab and will provide a list of some available toolboxes. Numerical Optimization Data Fitting / Approximation Numerical ODEs Matlab Toolboxes 2 Unconstrained Optimization The commands we discuss in this section are two of the several optimization routines available in Matlab. First we discuss fminbnd, which is used to minimize functions of 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 local minimizer 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, [ π, 4π]. The plot of 2 the function is depicted in Figure 1. We can first define the function f(x) in Matlab using f = @(x)(cos(x) - 2*log(x)) Then we proceed by the following, >> [x fval] = fminbnd(f, 2, 4) x = 3.7108

4 2 UNCONSTRAINED OPTIMIZATION Figure 1: Plot of f(x) = cos(x) 2 ln(x). fval = -3.4648 We 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 which can 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 complete discussion of fminsearch readers can refer to the Matlab s documentations. Here we illustrate the use of fminsearch in an example. For a simple example, we minimize the function f(x, y) = x 2 + y 2 which clearly has its minimizer at (0, 0). Let s choose an initial guess of x 0 = (0, 0). The following Matlab 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.2102

5 0.2548 fval = 1.0915e-09 Of course, reader can try fminsearch on more complicated problems and see the results. For more information on optimization routines in Matlab, reader can investigate Matlab s Optimization Toolbox which includes several powerful optimization tools which can be used to solve both unconstrained and constrained linear and non-linear optimization problems. 3 Curve-Fitting Here we consider the problem of fitting a polynomial of degree (at most) k into the data points (x i, y i ); 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 argument p is a vector which contains the coefficients of the interpolating polynomial. The method used is least squares, in which we choose a polynomial P of degree k which minimizes the following: m [P (x i ) y i ] 2. (3.1) i=1 For example, say we are given the data points xi yi -------- 1 1 2 0.5 3 1 4 2.5 5 3 6 4 7 5 Suppose 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-FITTING Once we have the vector p which contains the coefficients of the interpolating polynomial we can use the Matlab function ployval to evaluate the approximating polynomial 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 Matlab Another useful idea is using polyfit to find an approximating polynomial for a given 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 be so easy to do the same for a function which is not so well behaved. As an example, we consider the problem of approximating the function sin( (x)) on the interval [0, 2π]. The following Matlab commands show how one selects a numerical grid to get data points which can be used to approximate the function using a polynomial of degree k (in a least-squares sense). >> f = @(x)(sin(sqrt(x))); >> xi = [0 : 0.1 : 2*pi]; >> yi = f(xi); >> p = polyfit(xi,yi,4); To see how the function and its approximation compare, we can use the following commands, >> x = [0 : 0.01 : 2*pi]; >> y = polyval(p, x); >> plot(x, f(x), x, y, -- ); The result can be seen in Figure 3.

Figure 3: Approximation of a function using polynomial data fitting 7