Numerical Methods in MATLAB

Total Page:16

File Type:pdf, Size:1020Kb

Numerical Methods in MATLAB Numerical methods in MATLAB Mariusz Janiak p. 331 C-3, 71 320 26 44 c 2015 Mariusz Janiak All Rights Reserved Contents 1 Introduction 2 Ordinary Differential Equations 3 Optimization Introduction Using numeric approximations to solve continuous problems Numerical analysis is a branch of mathematics that solves continuous problems using numeric approximation. It involves designing methods that give approximate but accurate numeric solutions, which is useful in cases where the exact solution is impossible or prohibitively expensive to calculate. Numerical analysis also involves characterizing the convergence, accuracy, stability, and computational complexity of these methods.a aThe MathWorks, Inc. Introduction Numerical methods in Matlab Interpolation, extrapolation, and regression Differentiation and integration Linear systems of equations Eigenvalues and singular values Ordinary differential equations (ODEs) Partial differential equations (PDEs) Optimization ... Introduction Numerical Integration and Differential Equations Ordinary Differential Equations – ordinary differential equation initial value problem solvers Boundary Value Problems – boundary value problem solvers for ordinary differential equations Delay Differential Equations – delay differential equation initial value problem solvers Partial Differential Equations – 1-D Parabolic-elliptic PDEs, initial-boundary value problem solver Numerical Integration and Differentiation – quadratures, double and triple integrals, and multidimensional derivatives Ordinary Differential Equations An ordinary differential equation (ODE) contains one or more derivatives of a dependent variable y with respect to a single independent variable t, usually referred to as time. The notation used here for representing derivatives of y with respect to t is y 0 for a first derivative, y 00 for a second derivative, and so on. The order of the ODE is equal to the highest-order derivative of y that appears in the equation. Example y 00 = 2y Ordinary Differential Equations In an Initial Value Problem, the ODE is solved by starting from an initial state. Using the initial condition y0 as well as a period of time over which the answer is to be obtained (t0, tf ), the solution is obtained iteratively. At each step the solver applies a particular algorithm to the results of previous steps. At the first such step, the initial condition provides the necessary information that allows the integration to proceed. The final result is that the ODE solver returns a vector of time steps t=[t0,t1,t2,...,tf] as well as the corresponding solution at each step y=[y0,y1,y2,...,yf]. Ordinary Differential Equations Solvers in Matlab solve these types of first-order ODEs (1) Explicit ODEs of the form y 0 = f (t, y) Linearly implicit ODEs of the form M(t, y)y 0 = f (t, y), where M(t, y) is a nonsingular mass matrix. The mass matrix can be time- or state-dependent, or it can be a constant matrix. Linearly implicit ODEs can always be transformed to an explicit form y 0 = M−1(t, y)f (t, y) Ordinary Differential Equations Solvers in Matlab solve these types of first-order ODEs (2) If some components of y 0 are missing, then the equations are called Differential Algebraic Equations (DAE) y 0 = f (t, y, z), 0 = g(t, y, z), and the system of DAEs contains some algebraic variables. Algebraic variables are dependent variables whose derivatives do not appear in the equations. The number of derivatives needed to rewrite a DAE as an ODE is called the differential index Ordinary Differential Equations Solvers in Matlab solve these types of first-order ODEs (3) Fully implicit ODEs of the form f (t, y, y 0) = 0 Fully implicit ODEs cannot be rewritten in an explicit form, and might also contain some algebraic variables Systems of ODEs 0 y1 f1(t, y1, y2,..., yn) 0 y2 f2(t, y1, y2,..., yn) . = . . . . . 0 yn fn(t, y1, y2,..., yn) Ordinary Differential Equations Higher-Order ODEs (1) The Matlab ODE solvers only solve first-order equations The higher-order ODEs have to be rewritten as an equivalent system of first-order equations using the generic substitutions 0 y1 = y y1 = y2 y = y 0 y 0 = y 2 2 3 00 0 y3 = y =⇒ y3 = y4 . (n−1) 0 yn = y yn = f (t, y1, y2, y3,..., yn) Ordinary Differential Equations Higher-Order ODEs (2) Example: y 000 − y 00y + 1 = 0 0 y1 = y y1 = y2 0 0 y2 = y =⇒ y2 = y3 00 0 y3 = y y3 = y1y3 − 1 Ordinary Differential Equations Stiffness Lack of a precise definition In general, stiffness occurs when there is a difference in scaling somewhere in the problem, eg. two solution components that vary on drastically different time scales The step size taken by the solver is forced down to an unreasonably small level in comparison to the interval of integration, even in a region where the solution curve is smooth. Nonstiff solvers are unable to solve the problem or are extremely slow Stiff solvers reliability and efficiency can be improved by supplying the Jacobian matrix or its sparsity pattern Ordinary Differential Equations Basic Solver Selection Solver Problem Type Accuracy When to use ode45 Nonstiff Medium Should be the first solver you try ode23 Nonstiff Low Can be more efficient than ode45 at problems with crude tolerances, or in the presence of moderate stiffness ode113 Nonstiff Low to High Can be more efficient than ode45 at problems with stringent error tolerances, or when the ODE function is expensive to evaluate ode15s Stiff Low to Medium Use when ode45 fails or is inefficient and you suspect that the problem is stiff. Also use ode15s when solving differential algebraic equations (DAEs). ode23s Stiff Low Can be more efficient than ode15s at problems with crude error tolerances. It can solve some stiff problems for which ode15s is not effective. ode23t Stiff Low Use if the problem is only moderately stiff and you need a solution without numerical damping. Can solve differential algebraic equations (DAEs) ode23tb Stiff Low Solver might be more efficient than ode15s at problems with crude error tolerances ode15i Fully implicit Low Use for fully implicit problems f (t, y, y 0) = 0 and for diffe- rential algebraic equations (DAEs) of index 1. Ordinary Differential Equations Solver ode45 (1) [t,y] = ode45(odefun ,tspan ,y0) [t,y] = ode45(odefun ,tspan ,y0,options) [t,y,te,ye,ie] = ode45(odefun,tspan ,y0,options) s o l = ode45 ( ) Integrates the system of differential equations y 0 = f (t, y) on time horizon tspan with initial conditions y0 odefun — Functions to solve, specified as a function handle which defines the functions to be integrated. The function dydt = odefun(t,y), for a scalar t and a column vector y, must return a column vector dydt of data type single or double that corresponds to f (t, y). odefun must accept both input arguments, t and y, even if one of the arguments is not used in the function Ordinary Differential Equations Solver ode45 (2) tspan – Interval of integration, specified as a vector. At minimum, tspan must be a two element vector [t0 tf] specifying the initial and final times. To obtain solutions at specific times between t0 and tf, use a longer vector of the form [t0,t1,t2,...,tf]. The elements in tspan must be all increasing or all decreasing. The solver imposes the initial conditions, y0, at tspan(1), then integrates from tspan(1) to tspan(end) y0 – Initial conditions, specified as a vector. y0 must be the same length as the vector output of odefun, so that y0 contains an initial condition for each equation defined in odefun Ordinary Differential Equations Solver ode45 (3) options – Option structure, specified as a structure array. Use the odeset function to create or modify the options structure t – Evaluation points, returned as a column vector y – Solutions, returned as an array. Each row in y corresponds to the solution at the value returned in the corresponding row of t. te – Time of events, returned as a column vector ye – Solution at time of events (te), returned as an array ie – Index of vanishing event function, returned as a column vector sol – Structure for evaluation, returned as a structure array. Use this structure with the deval function to evaluate the solution at any point in the interval [t0 tf] Ordinary Differential Equations ODE Event Location (1) Use event functions to detect when certain events occur during the solution of an ODE Event functions take an expression that user specify, and detect an event when that expression is equal to zero They can also signal the ODE solver to halt integration when they detect an event Use the ’Events’ option of the odeset function to specify an event function Ordinary Differential Equations ODE Event Location (2) The event function must have the general form [value ,isterminal ,direction] = myEventsFcn(t,y) The output arguments are vectors whose ith element corresponds to the ith event value(i) is a mathematical expression describing the ith event. An event occurs when value(i) is equal to zero isterminal(i) = 1 if the integration is to terminate when the ith event occurs. Otherwise, it is 0. direction(i) = 0 if all zeros are to be located (the default). A value of +1 locates only zeros where the event function is increasing, and -1 locates only zeros where the event function is decreasing. Specify direction = [] to use the default value of 0 for all events. Ordinary Differential Equations Unicycle example (1) Z (q 1 ,q 2 ) Y q3 X q_ 1 = u1 cos q3, q_ 2 = u1 sin q3, t = [0, 2π], q(0) = (0, 0, 0). q_ 3 = u2, Assuming control u1 = 1, u2 = 0.1 sin t, we are looking for solution q(t) = ϕq0,t (u). Ordinary Differential Equations Unicycle example (2) File unicycle.m f u n c t i o n Dq = unicycle(t, q, u) % unicycle(t,q,u) −− system definition % %t −− time %q −− s t a t e vector %u −− c o n t r o l Dq = zeros(3, 1); Dq( 1 ) = u ( 1 ) * cos ( q ( 3 ) ) ; Dq( 2 ) = u ( 1 ) * s i n ( q ( 3 ) ) ; Dq(3) = u(2); Ordinary Differential Equations Unicycle example (3) File sym1.m % Set symulaiton parameters q0 =[0; 0; 0]; t s p a n = [ 0 2* p i ] ; u = [ 1 ; 0 .
Recommended publications
  • Optimization Toolbox User's Guide
    Optimization Toolbox For Use with MATLAB® User’s Guide Version 2 How to Contact The MathWorks: www.mathworks.com Web comp.soft-sys.matlab Newsgroup [email protected] Technical support [email protected] Product enhancement suggestions [email protected] Bug reports [email protected] Documentation error reports [email protected] Order status, license renewals, passcodes [email protected] Sales, pricing, and general information 508-647-7000 Phone 508-647-7001 Fax The MathWorks, Inc. Mail 3 Apple Hill Drive Natick, MA 01760-2098 For contact information about worldwide offices, see the MathWorks Web site. Optimization Toolbox User’s Guide COPYRIGHT 1990 - 2003 by The MathWorks, Inc. The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or repro- duced in any form without prior written consent from The MathWorks, Inc. FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by or for the federal government of the United States. By accepting delivery of the Program, the government hereby agrees that this software qualifies as "commercial" computer software within the meaning of FAR Part 12.212, DFARS Part 227.7202-1, DFARS Part 227.7202-3, DFARS Part 252.227-7013, and DFARS Part 252.227-7014. The terms and conditions of The MathWorks, Inc. Software License Agreement shall pertain to the government’s use and disclosure of the Program and Documentation, and shall supersede any conflicting contractual terms or conditions. If this license fails to meet the government’s minimum needs or is inconsistent in any respect with federal procurement law, the government agrees to return the Program and Documentation, unused, to MathWorks.
    [Show full text]
  • Optimization Toolbox
    Optimization Toolbox™ 4 User’s Guide How to Contact The MathWorks www.mathworks.com Web comp.soft-sys.matlab Newsgroup www.mathworks.com/contact_TS.html Technical Support [email protected] Product enhancement suggestions [email protected] Bug reports [email protected] Documentation error reports [email protected] Order status, license renewals, passcodes [email protected] Sales, pricing, and general information 508-647-7000 (Phone) 508-647-7001 (Fax) The MathWorks, Inc. 3 Apple Hill Drive Natick, MA 01760-2098 For contact information about worldwide offices, see the MathWorks Web site. Optimization Toolbox™ User’s Guide © COPYRIGHT 1990–2008 by The MathWorks, Inc. The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc. FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through the federal government of the United States. By accepting delivery of the Program or Documentation, the government hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and Documentation by the federal government (or other entity acquiring for or through the federal government) and shall supersede any conflicting contractual terms or conditions.
    [Show full text]
  • Overview of Optimization Software
    Stanford University, Management Science & Engineering (and ICME) MS&E 318 (CME 338) Large-Scale Numerical Optimization Instructor: Michael Saunders Spring 2018 Notes 2: Overview of Optimization Software 1 Optimization problems We study optimization problems involving linear and nonlinear constraints: NP minimize φ(x) n x∈R 0 x 1 subject to ` ≤ @ Ax A ≤ u, c(x) where φ(x) is a linear or nonlinear objective function, A is a sparse matrix, c(x) is a vector of nonlinear constraint functions ci(x), and ` and u are vectors of lower and upper bounds. We assume the functions φ(x) and ci(x) are smooth: they are continuous and have continuous first derivatives (gradients). Sometimes gradients are not available (or too expensive) and we use finite difference approximations. Sometimes we need second derivatives. We study algorithms that find a local optimum for problem NP. Some examples follow. If there are many local optima, the starting point is important. „ x « LP Linear Programming min cTx subject to ` ≤ ≤ u Ax MINOS, SNOPT, SQOPT LSSOL, QPOPT, NPSOL (dense) CPLEX, Gurobi, LOQO, HOPDM, MOSEK, XPRESS CLP, lp solve, SoPlex (open source solvers [7, 34, 54]) „ x « QP Quadratic Programming min cTx + 1 xTHx subject to ` ≤ ≤ u 2 Ax MINOS, SQOPT, SNOPT, QPBLUR LSSOL (H = BTB, least squares), QPOPT (H indefinite) CLP, CPLEX, Gurobi, LANCELOT, LOQO, MOSEK BC Bound Constraints min φ(x) subject to ` ≤ x ≤ u MINOS, SNOPT LANCELOT, L-BFGS-B „ x « LC Linear Constraints min φ(x) subject to ` ≤ ≤ u Ax MINOS, SNOPT, NPSOL 0 x 1 NC Nonlinear Constraints min φ(x) subject to ` ≤ @ Ax A ≤ u MINOS, SNOPT, NPSOL c(x) CONOPT, LANCELOT Filter, KNITRO, LOQO (second derivatives) IPOPT (open source solver [30]) Algorithms for finding local optima are used to construct algorithms for more complex optimization problems: stochastic, nonsmooth, global, mixed integer.
    [Show full text]
  • Optimization Toolbox for Use with MATLAB®
    Optimization Toolbox For Use with MATLAB® User’s Guide Version 3 How to Contact The MathWorks: www.mathworks.com Web comp.soft-sys.matlab Newsgroup [email protected] Technical support [email protected] Product enhancement suggestions [email protected] Bug reports [email protected] Documentation error reports [email protected] Order status, license renewals, passcodes [email protected] Sales, pricing, and general information 508-647-7000 Phone 508-647-7001 Fax The MathWorks, Inc. Mail 3 Apple Hill Drive Natick, MA 01760-2098 For contact information about worldwide offices, see the MathWorks Web site. Optimization Toolbox User’s Guide © COPYRIGHT 1990 - 2004 by The MathWorks, Inc. The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or repro- duced in any form without prior written consent from The MathWorks, Inc. FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by, for, or through the federal government of the United States. By accepting delivery of the Program or Documentation, the government hereby agrees that this software or documentation qualifies as commercial computer software or commercial computer software documentation as such terms are used or defined in FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of this Agreement and only those rights specified in this Agreement, shall pertain to and govern the use, modification, reproduction, release, performance, display, and disclosure of the Program and Documentation by the federal government (or other entity acquiring for or through the federal government) and shall supersede any conflicting contractual terms or conditions.
    [Show full text]
  • Optimization Toolbox User's Guide
    Optimization Toolbox For Use with MATLAB® Thomas Coleman Mary Ann Branch Andrew Grace Computation Visualization Programming User’s Guide Version 2 How to Contact The MathWorks: 508-647-7000 Phone 508-647-7001 Fax The MathWorks, Inc. Mail 24 Prime Park Way Natick, MA 01760-1500 http://www.mathworks.com Web ftp.mathworks.com Anonymous FTP server comp.soft-sys.matlab Newsgroup [email protected] Technical support [email protected] Product enhancement suggestions [email protected] Bug reports [email protected] Documentation error reports [email protected] Subscribing user registration [email protected] Order status, license renewals, passcodes [email protected] Sales, pricing, and general information Optimization Toolbox User’s Guide COPYRIGHT 1990 - 1999 by The MathWorks, Inc. The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. No part of this manual may be photocopied or repro- duced in any form without prior written consent from The MathWorks, Inc. U.S. GOVERNMENT: If Licensee is acquiring the Programs on behalf of any unit or agency of the U.S. Government, the following shall apply: (a) For units of the Department of Defense: the Government shall have only the rights specified in the license under which the commercial computer software or commercial software documentation was obtained, as set forth in subparagraph (a) of the Rights in Commercial Computer Software or Commercial Software Documentation Clause at DFARS 227.7202-3, therefore the rights set forth herein shall apply; and (b) For any other unit or agency: NOTICE: Notwithstanding any other lease or license agreement that may pertain to, or accompany the delivery of, the computer software and accompanying documentation, the rights of the Government regarding its use, reproduction, and disclo- sure are as set forth in Clause 52.227-19 (c)(2) of the FAR.
    [Show full text]
  • Nonlinear Constrained Optimization: Methods and Software
    ARGONNE NATIONAL LABORATORY 9700 South Cass Avenue Argonne, Illinois 60439 Nonlinear Constrained Optimization: Methods and Software Sven Leyffer and Ashutosh Mahajan Mathematics and Computer Science Division Preprint ANL/MCS-P1729-0310 March 17, 2010 This work was supported by the Office of Advanced Scientific Computing Research, Office of Science, U.S. Department of Energy, under Contract DE-AC02-06CH11357. Contents 1 Background and Introduction1 2 Convergence Test and Termination Conditions2 3 Local Model: Improving a Solution Estimate3 3.1 Sequential Linear and Quadratic Programming......................3 3.2 Interior-Point Methods....................................5 4 Globalization Strategy: Convergence from Remote Starting Points7 4.1 Augmented Lagrangian Methods..............................7 4.2 Penalty and Merit Function Methods............................8 4.3 Filter and Funnel Methods..................................9 4.4 Maratos Effect and Loss of Fast Convergence....................... 10 5 Globalization Mechanisms 11 5.1 Line-Search Methods..................................... 11 5.2 Trust-Region Methods.................................... 11 6 Nonlinear Optimization Software: Summary 12 7 Interior-Point Solvers 13 7.1 CVXOPT............................................ 13 7.2 IPOPT.............................................. 13 7.3 KNITRO............................................ 15 7.4 LOQO............................................. 15 8 Sequential Linear/Quadratic Solvers 16 8.1 CONOPT...........................................
    [Show full text]
  • MATLAB's Optimization Toolbox Algorithms
    Appendix A MATLAB’s Optimization Toolbox Algorithms Abstract MATLAB’s Optimization Toolbox (version 7:2) includes a family of algorithms for solving optimization problems. The toolbox provides functions for solving linear programming, mixed-integer linear programming, quadratic program- ming, nonlinear programming, and nonlinear least squares problems. This chapter presents the available functions of this toolbox for solving LPs. A computational study is performed. The aim of the computational study is to compare the efficiency of MATLAB’s Optimization Toolbox solvers. A.1 Chapter Objectives • Present the linear programming algorithms of MATLAB’s Optimization Tool- box. • Understand how to use the linear programming solver of MATLAB’s Optimiza- tion Toolbox. • Present all different ways to solve linear programming problems using MAT- LAB. • Compare the computational performance of the linear programming algorithms of MATLAB’s Optimization Toolbox. A.2 Introduction MATLAB is a matrix language intended primarily for numerical computing. MATLAB is especially designed for matrix computations like solving systems of linear equations or factoring matrices. Users that are not familiar with MATLAB are referred to [3, 7]. Experienced MATLAB users that want to further optimize their codes are referred to [1]. MATLAB’s Optimization Toolbox includes a family of algorithms for solving optimization problems. The toolbox provides functions for solving linear pro- gramming, mixed-integer linear programming, quadratic programming, nonlinear programming, and nonlinear least squares problems. MATLAB’s Optimization Toolbox includes four categories of solvers [9]: © Springer International Publishing AG 2017 565 N. Ploskas, N. Samaras, Linear Programming Using MATLAB®, Springer Optimization and Its Applications 127, DOI 10.1007/978-3-319-65919-0 566 Appendix A MATLAB’s Optimization Toolbox Algorithms • Minimizers: solvers that attempt to find a local minimum of the objective function.
    [Show full text]