Synthesis Lectures on Engineering, Science, and Technology Farzin Asadi Applied Numerical Analysis with MATLAB®/Simulink® For Engineers and Scientists Synthesis Lectures on Engineering, Science, and Technology The focus of this series is general topics, and applications about, and for, engineers and scientists on a wide array of applications, methods and advances. Most titles cover subjects such as professional development, education, and study skills, as well as basic introductory undergraduate material and other topics appropriate for a broader and less technical audience. Farzin Asadi Applied Numerical Analysis ® ® with MATLAB /Simulink For Engineers and Scientists Farzin Asadi Department of Electrical and Electronics Engineering Maltepe University Istanbul, Turkey ISSN 2690-0300 ISSN 2690-0327 (electronic) Synthesis Lectures on Engineering, Science, and Technology ISBN 978-3-031-19365-1 ISBN 978-3-031-19366-8 (eBook) https://doi.org/10.1007/978-3-031-19366-8 © The Editor(s) (if applicable) and The Author(s), under exclusive license to Springer Nature Switzerland AG 2023 This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use. The publisher, the authors, and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations. This Springer imprint is published by the registered company Springer Nature Switzerland AG The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland Preface Numerical analysis is the branch of mathematics that is used to find approximations to difficult problems such as finding the roots of nonlinear equations, integration involving complex expressions, and solving differential equations for which analytical solutions do not exist. It is applied to a wide variety of disciplines such as business, all fields of engineering, computer science, education, geology, meteorology, and others. Many good books are written about the numerical analysis theories. In this book, we don’t focus on the theories of numerical analysis. Instead we focus on solving numerical analysis problems with the aid of tools that MATLAB/Simulink provides for us. This book can accompany and complete any standard theoretical textbook on numerical analysis. The prerequisite for this book is a first course on numerical analysis, and no prior knowledge of MATLAB/Simulink is assumed. This book is written primarily for students of engineering and science. However, it can be useful for anyone who wants to solve a numeric problem with MATLAB/Simulink. For instance, master or Ph.D. students can use the material presented here to solve the numerical problems of their thesis or papers. All of the material presented here can be covered in one semester (14 weeks) with 3 h per week. This book is composed of 11 chapters. Here is a brief summary of each chapters: Chapter 1 is an introduction to MATLAB. In this chapter, you will learn how to do basic calculations with MATLAB. Basic matrix operations, trigonometric, hyper- bolic, logarithmic, exponential, and many other important functions are studied in this chapter. Chapter 2 shows how MATLAB can be used for symbolic calculations. In this chapter, you will learn to calculate limits, derivatives, integrals (both definite and indefinite), partial fraction expansion, Laplace transform, Fourier transform, and Taylor series with MATLAB. Chapter 3 shows how MATLAB can be used to calculate a definite integral (single, double or triple) or derivative of a function at a given point. Trapezoidal and Simpson rules for calculation of definite integrals are studied in this chapter. v vi Preface Chapter 4 shows how MATLAB can be used for statistical calculations. In this chapter, you will learn how to calculate summation, average, variance, and standard devia- tion of a given data, how to generate random numbers, how to calculate factorial and combinatorial, and how to obtain cumulative distribution function for a normal distribution. Chapter 5 shows how MATLAB can be used to obtain impulse and step responses of a linear differential equation. In this book, we use the linear system (or linear dynamical system) and linear differential equation terms interchangeably. By impulse and step responses, we mean the response that observed in the output when the input is Heaviside step function and Dirac impulse function, respectively. Chapter 6 shows how differential equations can be solved with Simulink. Simulink is a software package which accompanies MATLAB and permits you to simulate dynam- ical systems (i.e., systems which can described by a linear or nonlinear differential equations) with the aid of many ready to use graphical blocks. Chapter 7 shows how difference equations can be solved with Simulink. A difference equation is a relation between the differences of unknown function at one or more general values of the independent variable. Generally, a difference equation is obtained in an attempt to solve an ordinary differential equation by finite difference method. Chapter 8 shows how curve-fitting problems can be solved with MATLAB’s Curve Fitting Toolbox™. Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points. Chapter 9 shows how MATLAB can be used for drawing the graph of a given data. Different kind of graphs are studied in this chapter. Chapter 10 shows how some of well-known numerical analysis algorithms can be implemented with MATLAB programming. Chapter 11 shows how optimization problems can be solved with the aid of ready to use functions that Optimization Toolbox™ provides. I hope that this book will be useful to the readers, and I welcome comments on the book. Istanbul, Turkey Farzin Asadi [email protected] Contents 1 Essential of MATLAB® .............................................. 1 1.1 Introduction .................................................. 1 1.2 MATLAB Environment ........................................ 2 1.3 Basic Operation with MATLAB ................................ 3 1.4 Clearing the Screen and Variables ............................... 11 1.5 Basic Matrix Operations ....................................... 15 1.6 Trigonometric Functions ....................................... 25 1.7 Hyperbolic Functions .......................................... 28 1.8 Logarithmic and Exponential Function ........................... 29 1.9 Rounding Functions ........................................... 30 1.10 Colon Operator ............................................... 30 1.11 Linspace and Logspace Commands .............................. 33 1.12 Ones, Zeros and Eye Commands ................................ 36 1.13 Format Command ............................................. 37 1.14 Polynomial Functions ......................................... 38 1.15 Solution of Nonlinear Systems .................................. 40 1.16 Eigen Values and Eigen Vectors ................................ 44 1.17 Reduced Echelon Form of a Matrix ............................. 45 1.18 Norm of Vectors and Matrices .................................. 46 1.19 Condition Number of a Matrix .................................. 48 1.20 Tic and Toc Commands ........................................ 49 1.21 Getting Help in MATLAB ..................................... 49 References for Further Study .......................................... 53 2 Symbolic Calculations in MATLAB® .................................. 55 2.1 Introduction .................................................. 55 2.2 Calculation of Limit, Derivative and Integral ..................... 55 2.3 Solving the Ordinary Differential Equations ...................... 57 2.4 Partial Fraction Expansion and Laplace Transform ................ 61 2.5 Fourier Transform ............................................. 65 vii viii Contents 2.6 Taylor Series ................................................. 67 2.7 Expansion of an Algebraic Expression ........................... 69 Reference for Further Study ........................................... 71 3 Numerical Integration and Derivation ................................. 73 3.1 Introduction .................................................. 73 3.2 Trapezoidal Rule .............................................. 73 3.3 Simpson’s 1/3 Rule ........................................... 74 3.4 Simpson 3/8 Rule ............................................. 77 3.5 Double Integrals .............................................. 79 3.6 Triple Integrals ............................................... 79 3.7 Derivative .................................................... 81 References for Further Study .......................................... 84 4 Statistics with MATLAB® ............................................ 85 4.1 Introduction .................................................. 85 4.2 Sum of Elements ............................................. 85 4.3 Average ..................................................... 86 4.4 Variance and Standard Deviation ................................ 87 4.5 Factorial ..................................................... 89 4.6 Combination ................................................. 89 4.7 Random Numbers ............................................. 90 4.8 Normal Probability Density Function ............................ 92 4.9 Cumulative Distribution Function ............................... 94 References for Further Study .......................................... 96 5 Impulse and Step Response of Linear Systems ......................... 97 5.1 Introduction .................................................. 97 5.2 Impulse Response of Dynamical Systems ........................ 98 5.3 Step Response of Dynamical Systems ........................... 102 Reference for Further Study ........................................... 103 6 Solving Differential Equations in Simulink® ........................... 105 6.1 Introduction .................................................. 105 6.2 Solving a Linear Differential Equation in Simulink ................ 105 6.3 Multiplexer Block ............................................. 124 6.4 Giving Name to Scope Blocks .................................. 127 6.5 Selection of Solver ............................................ 131 6.6 Transferring the Results from Simulink Environment to MATLAB® ................................................ 135 6.7 Transfer Function Block ....................................... 141 6.8 State Space Block ............................................. 148 6.9 Nonlinear Models ............................................. 161 Contents ix 6.10 Van Der Pol Equation ......................................... 166 References for Further Study .......................................... 168 7 Solving Difference Equations in Simulink® ............................ 169 7.1 Introduction .................................................. 169 7.2 Solving the Difference Equations: Example 1 ..................... 169 7.3 Solving the Difference Equations: Example 2 ..................... 176 7.4 Solving the Difference Equations: Example 3 ..................... 183 References for Further Study .......................................... 189 8 Curve Fitting with MATLAB® ....................................... 191 8.1 Introduction .................................................. 191 8.2 Example 1: Linear Curve Fitting ................................ 191 8.3 Graphical Comparison of Estimation with Measured Data .......... 200 8.4 Quantitative Comparison of Estimation with Data ................. 203 8.5 Example 2: Nonlinear Curve Fitting ............................. 205 8.6 Example 3: Export the Obtained Equation to the MATLAB Workspace ................................................... 212 References for Further Study .......................................... 217 9 Drawing Graphs with MATLAB® .................................... 219 9.1 Introduction .................................................. 219 9.2 fplot Command ............................................... 219 9.3 Plotting the Graph of a Numeric Data ........................... 222 9.4 Addition of Labels and Title to the Drawn Graph ................. 228 9.5 Exporting the Drawn Graph as a Graphical File ................... 229 9.6 Drawing Two or More Graphs on the Same Axis ................. 231 9.7 Logarithmic Axis ............................................. 236 9.8 Drawing 2D and 3D Parametric Graphs .......................... 239 9.9 Polar Plot .................................................... 241 9.10 3D Surfaces .................................................. 243 9.11 Pie Chart .................................................... 245 9.12 Exploded Pie Chart ........................................... 248 9.13 Export the Drawn Pie Chart as a Graphical File ................... 250 9.14 Bar Graphs ................................................... 252 References for Further Study .......................................... 256 10 MATLAB® Programming ............................................ 257 10.1 Introduction .................................................. 257 10.2 MATLAB Editor .............................................. 257 10.3 Simple Game ................................................. 258 10.4 Switch-Case Control Statement ................................. 262 10.5 fprintf and disp Command ..................................... 265