Download presentation
Presentation is loading. Please wait.
Published bySharlene Haynes Modified over 9 years ago
1
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION
2
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20092 Goal of Course For you to be able to generate readable, compact, and verifiably correct MATLAB programs that obtain numerical solutions to a wide range of physical and empirical models, and to display the results with fully annotated graphics.
3
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20093 MATLAB founded in 1984 by Jack Little, Steve Bangert, and Clive Moler 1985 - MIT bought 10 copies 1986 - MATLAB 2 1987 - MATLAB 3 1990 - Simulink 1 1994 - MATLAB 4 1996 - MATLAB 5 2000 - MATLAB 6 2004 - MATLAB 7 2009 – MATLAB 7.8 (2009a)
4
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20094 Chapter 1 – Objective To introduce the fundamental characteristics of the MATLAB environment and the language’s basic syntax
5
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20095 MATLAB A computing language devoted to processing data in the form of arrays of numbers (called matrices). Integrates computation and visualization into a flexible computer environment, and provides a diverse family of built-in functions that can be used to obtain numerical solutions to a wide range of engineering problems. Derives its name from MATrix LABoratory.
6
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20096 Some Suggestions on How to Use MATLAB Use the Help files extensively. This will minimize errors caused by incorrect syntax and by incorrect or inappropriate application of a MATLAB function. Write scripts and functions in a text editor and save them as M files. This will save time, save the code, and greatly facilitate the debugging process, especially if the MATLAB editor/debugger is used.
7
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20097 Some Suggestions on How to Use MATLAB Attempt to minimize the number of expressions comprising scripts and functions. This usually leads to a tradeoff between readability and compactness, but it can encourage the search for MATLAB functions and procedures that can perform some of the steps faster and more directly. When practical, use graphical output as the script or function is being developed. This usually shortens the code development process by identifying potential coding errors and can facilitate the understanding of the physical process being modeled or analyzed.
8
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20098 Some Suggestions on How to Use MATLAB Most importantly, verify by independent means that the outputs from the scripts and functions are correct.
9
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20099 Notation Conventions Variable/Function NameFontExample User-created variableTimes Roman ExitPressure a2, sig MATLAB functionCourier cosh (x), pi User-created functionTimes Roman BeamRoots (a, x, k) Bold Numerical ValueFontExample Provided in programTimes Roman 5.672 Output to command windowHelvetica5.672 or to a graphical display
10
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200910 The MATLAB Environment Preliminaries: Command Window Management Executing Expressions from the MATLAB Command Window: Basic MATLAB Syntax Clarification and Exceptions to MATLAB Syntax MATLAB Functions Creating Scripts and Executing Them from the MATLAB Editor Online Help Symbolic Toolbox What We Will Cover in Chapter 1
11
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200911 Command Window, Command History, Workspace
12
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200912 Command Window, Command History, Workspace, and Editor
13
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200913 Command Window and Editor MATLAB command window (left) and editor (right) after closing the command history and workspace windows
14
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200914 Clearing of Command Window and Workspace MATLAB functionDescription clc Clear the command window clear Removes variables from the workspace (computer memory) close all Closes (deletes) all graphic windows format Formats the display of numerical output to the command window format compact Removes empty (blank) lines
15
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200915 These operation can also be done with
16
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200916 and with format compact format long e format short
17
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200917 OptionDisplay (number > 0)Display (number < 0) short long short e long e short g long g short eng long eng rational hex bank 444.4444 4.444444444444445e+002 4.4444e+002 4.444444444444445e+002 444.44 444.444444444444 444.4444e+000 444.444444444444e+000 4000/9 407bc71c71c71c72 444.44 0.0044 0.004444444444444 4.4444e-003 4.444444444444444e-003 0.0044444 0.00444444444444444 4.4444e-003 4.44444444444444e-003 1/225 3f723456789abcdf 0.00 Results from Different Format Selections
18
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200918 Preferences Menu Selections: Font Size
19
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200919 MATLAB Variable Names 63 alphanumeric characters Start with uppercase or lowercase letter Followed by any combination of uppercase and lowercase letters, numbers, and the underscore character (_) Case sensitive - junk different from junK Example – exit_pressure or ExitPressure Length of variable names - Tradeoff between easily recognizable identifiers and readability of the resulting expressions
20
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200920 Keywords Reserved Explicitly for the MATLAB Programming Language break case catch continue else elseif end for function global if otherwise persistent return switch try while
21
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200921 » p = 7.1 p = 7.1000 » x = 4.92 x = 4.9200 » k = -1.7 k = -1.7000 User types and hits Enter System response User types and hits Enter System response User types and hits Enter System response Command Window Interaction
22
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200922 » p = 7.1; » x = 4.92; » k = -1.7; » Suppression of System Response - Semicolon Several Expressions Placed on One Line p = 7.1, x = 4.92, k = 1.7 System response – p = 7.1000 x = 4.9200 k = -1.7000 » Using semicolon instead of commas suppresses this output
23
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200923 Arithmetic Operators Hierarchy Level ( ) Parentheses1 ´ Prime (Apostrophe)1 ^ Exponentiation2 * Multiplication3 / Division3 + Addition4 Subtraction4
24
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200924 Parentheses Needed so that the mathematical operations are performed on the proper collections of quantities and in their proper order. Within each set of parentheses the mathematical operation are performed from left to right on quantities at the same hierarchical level. These rules can help minimize the number of parentheses.
25
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200925 Some Examples
26
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200926 Another Example Consider The MATLAB script is p = 7.1; x = 4.92; k = 1.7; % Numerical values % must be assigned first t = (1/(1+p*x))^k which results in t = 440.8779
27
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200927 Syntax Clarification and Exceptions Blanks In an arithmetic expression, blanks can be employed with no consequence – except when expression appears in an array specification; that is, between two brackets [ ] Excluding blanks in assignment statements, variable names on the right hand side of the equal sign must be separated by one of the arithmetic operators a comma (,) a semicolon (;)
28
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200928 Two Exceptions (1) Complex Numbers: z = a +1jb or z = a +1ib (i = j = ) Example a = 2; b = 3; z = (a+1j*b)*(4-7j) Note: Real and complex numbers can be mixed without any special concerns. Example 1 z = 4 + sqrt(-4) System displays z = 4.0000 + 2.0000i Example 2 z = 1i^1i System displays z = 0.2079
29
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200929 (2) Exponential Form: x = 4.56 10 2 x = 4.56e-2 or x = 0.0456 or x = 4.56*10^-2 Note: Maximum number of digits that can follow the ‘e’ is 3.
30
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200930 System Assignment of Variable Names Type in the command window cos ( pi /3) The system responds with ans = 0.5000 The variable ans can now be used as one would any other variable. Then, typing in the command window ans+2 the system responds with ans = 2.5000
31
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200931 Scalars versus Arrays MATLAB considers all variables as arrays of numbers – When using the five arithmetic operators, +, , *, /, and ^, these operations have to obey the rules of linear (matrix) algebra When the variables are scalar quantities [arrays of one element (one row and one column)] the usual rules of algebra apply One can operate on arrays of numbers and suspend the rules of linear algebra by using dot operations
32
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200932 Some Elementary MATLAB Functions
33
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200933 Some MATLAB Constants and Special Quantities
34
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200934 MATLAB Trigonometric and Hyperbolic Functions
35
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200935 Several Specialized Mathematical Functions
36
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200936 Several Specialized Statistical Functions
37
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200937 MATLAB Relational Operators
38
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200938 Example For x = 0.1 and a = 0.5, determine the value of y when The script is x = 0.1; a = 0.5; y = sqrt ( abs ( exp (- pi *x)- sin (x)/ cosh (a)- log (x+a))) When executed, the system returns y = 1.0736
39
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200939 Decimal-to-Integer Conversion Functions
40
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200940 Complex Number Manipulation Functions
41
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200941 Additional Special Characters and a Summary of Their Usage
42
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200942 Additional Special Characters and a Summary of Their Usage
43
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200943 Additional Special Characters and a Summary of Their Usage
44
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200944 Creating Programs and Executing Them from the MATLAB Editor When to use the editor - 1. The program will contain more than a few lines of code. 2. The program will be used again. 3. A permanent record is desired. 4. It is expected that occasional upgrading will be required. 5. Substantial debugging is required. 6. One wants to transfer the listing to another person or organization.
45
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200945 Additional Reasons - Required when creating functions Editor has many features Commenting/Un-commenting lines Smart Indenting Parentheses checking Keywords in blue Comments in green Strings in violet Smart indent
46
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200946 Additional Reasons - Click one icon to save and run a program (Must use Save As first time) Save and Run icon
47
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200947 Additional Reasons - Enabling M-Lint from the Preferences menu
48
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200948 Illustration of M-Lint Orange bar Red bar Red square With cursor placed on red bar, this error message is displayed.
49
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200949 Additional Reasons – Enabling the cell feature of the Editor
50
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200950 Illustration of Editing Cells Run the highlighted cell Run the highlighted cell and advance to the next cell Selected cell is highlighted Second cell Third cell
51
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200951 A Script or a Function Typically Has the Following Attributes - 1. Documentation: Purpose and operations performed Programmer's Name Date originated Date(s) revised Description of the input(s): number, meaning, and type Description of the output(s): number, meaning, and type
52
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200952 2. Input - which includes numerous checks to ensure that all input values have the qualities required for the script/function to work properly. 3. Initialization - where the appropriate variables are assigned their numerical values. 4. Computation - where the numerical evaluations are performed. 5. Output - where the results are presented as graphical and/or annotated numerical quantities.
53
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200953 Naming of Programs and Functions File name follows that for variable names. Must start with an upper or lower case letter followed by up to 62 contiguous alphanumeric characters and the underscore character. No blank spaces are allowed in file names. [This is different from what is allowed by the Windows operating system] A ‘.m’ suffix must be affixed to the file name – Consequently, these files are called ‘M’ files
54
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200954 Saving/Executing (Running) M Files Set Path window (File) Change current path to file being executed.
55
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200955 Accessing the Browser Window to Change Current Path (Directory) Clicking on this icon brings up the Browser
56
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200956 Example - Flow in a circular channel Let d = 2 m, g = 9.8 m/s 2, and = 60 = /3. Then the script is g = 9.8; d = 2; th = pi /3; % Input Dc = d/2*(1- cos (th)); Qnum = 2^(3/2)*Dc^(5/2)* sqrt (g)*(th-0.5* sin (2*th))^(3/2); Qden = 8* sqrt ( sin (th))*(1- cos( th))^(5/2); Q = Qnum/Qden % m^3/s
57
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200957 The various MATLAB windows after executing the program
58
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200958 On-Line Help: Getting Started
59
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200959 On-Line Help: Desktop
60
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200960 On-Line Help: Command Window
61
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200961 On-Line Help: When Function Name is Known Type in function name
62
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200962 On-Line Help: When Function Name Is Not Known Type search entry and press Enter
63
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200963 Symbolic Toolbox The Symbolic Math Toolbox provides the capability of manipulating symbols to perform algebraic, matrix, and calculus operations symbolically When one couples the results obtained from symbolic operations with MATLAB’s ability to create functions, one has a very effective means of numerically evaluating symbolically obtained expressions. This is introduced in Chapter 5.
64
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200964 We will illustrate by example – Syntax Variable precision arithmetic Taylor series expansions Differentiation and integration Limits Substitution Inverse Laplace transform
65
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200965 The shorthand way to create symbolic variables is with syms a b c … where a, b, and c are now symbolic variables. The blanks between each variable are required If the variables are restricted to being real variables, then we modify this statement as syms a b c real These symbols can be intermixed with non-symbolic variable names, numbers, and MATLAB functions, with the result being a symbolic expression.
66
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200966 Consider the expression Assuming that d = 4.2 (1/d = 0.238095), the script to represent this expression symbolically is syms a b d = 4.2; f = 11.92* exp (-a^2)+b/d which, upon execution, displays f = 298/25* exp (-a^2)+5/21*b where f is a symbolic object. Note that: 21/5 = 4.2 298/25 = 11.92
67
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200967 Numbers in a symbolic expression are converted to the ratio of two integers, when possible. If the decimal representation of numbers is desired, then one uses vpa (f, n) where f is the symbolic expression and n is the number of digits. Thus, to revert to the decimal notation with five decimal digits, the script becomes syms a b d = 4.2; f = vpa (11.92* exp (-a^2)+b/d, 5)
68
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200968 which yields f = 11.920* exp (-1.*a^2)+.23810*b Note: 1/d = 1/4.2 = 0.2381 Variable Precision Arithmetic One can also use vpa to calculate quantities with more than 15 digits of accuracy as follows vpa ('Expression', n) where Expression is a valid MATLAB symbolic relation and n is the desired number of digits of precision.
69
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200969 Consider the evaluation of the following expression The script to evaluate this relation with 50 digits of precision is y = vpa ('factorial(32)-exp(100)', 50) Its execution gives y = -26881171155030517550432725348582123713611118.773742 If variable precision arithmetic had not been used, then y = 2.688117115503052×10 43
70
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200970 Symbolic Differentiation and Integration Differentiation is performed with the function diff (f, x, n) where f = f(x) is a symbolic expression x = variable with which differentiation is performed n = number of differentiations to be performed For example, when n = 2 the second derivative is taken
71
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200971 Example We shall take the derivative of bcos(bt), first with respect to t and then with respect to b. The script is syms b t dt = diff (b* cos (b*t), t, 1) db = diff (b* cos (b*t), b, 1) Upon execution, we obtain dt = -b^2*sin(b*t) db = cos(b*t)-b*sin(b*t)*t
72
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200972 Integration is performed with the function int (f, x, c, d) where f = f(x) is a symbolic expression x = variable of integration c = lower limit of integration d = upper limit of integration When c and d are omitted, the application of int results in the indefinite integral of f(x)
73
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200973 Example We shall integrate the results of the differentiation performed previously. Thus, syms b t f = b* cos (b*t); dt = diff (f, t, 1); db = diff (f, b, 1); it = int (dt, t) ib = int (db, b) The execution results in it = b*cos(b*t) ib = b*t*cos(b*t))
74
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200974 Limits One can take the limit of a symbolic expression using limit (f, x, z) where f = f(x) is the symbolic function x = symbolic variable that is to assume the limiting value z Example Determine the limit of
75
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200975 The script is syms a b Lim = limit ((2*a+b)/(3*a-4), a, inf) where inf = Example Determine the limit of
76
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200976 The script is syms y x Lim = limit ((1+y/x)^x, x, inf ) Upon execution, we obtain Lim = exp(y) In other words, the limit is e y.
77
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200977 Taylor Series Expansion An n-term Taylor series expansion of a function f(x) about the point a is given by The function that performs this operation is taylor (f, n, a, x)
78
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200978 Example Obtain a four-term expansion of cos about o. The script is syms x tho Tay = taylor ( cos (x), 4, tho, x) Upon execution, we obtain Tay = cos(tho)-sin(tho)*(x-tho)-1/2*cos(tho)*(x-tho)^2 +1/6*sin(tho)*(x-tho)^3 That is,
79
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200979 Substitution To substitute one expression b for another expression a, the following function is used subs (f, a, b) where f = f(a) Inverse Laplace Transform If the Laplace transform of a function f(t) is F(s), where s is the Laplace transform parameter, then the inverse Laplace transform is obtained from ilaplace (F, s, t)
80
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200980 Example Consider the expression where 0 < < 1. The inverse Laplace transform is obtained from syms s t z f = ilaplace (1/(s^2+2*z*s+1), s, t) The execution of this script gives f = exp(-t*z)*sinh(t*(z^2-1)^(1/2))/(z^2-1)^(1/2)
81
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200981 To simplify this expression, we make use of the following change of variables Then, a simplified expression can be obtained by modifying the original script as follows syms s t z r f = ilaplace (1/(s^2+2*z*s+1), s, t) f = simple ( subs (f,(z^2-1), -r^2)) Upon execution, we obtain f = exp(-t*z)*sin(t*r)/r
82
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200982 Example Determine the curvature of the parametric curves The curvature is determined from where the prime denoted the derivative with respect to t. The script is
83
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200983 syms t a b x = 2*b*cos(t) +b*cos(2*t); y = 2*b*sin(t)-b*sin(2*t); xp = diff (x, t, 1); xpp = diff (x, t, 2); yp = diff (y, t, 1); ypp = diff (y, t, 2); kn = xp*ypp-yp*xpp; kd = xp^2+yp^2; kn = factor ( simple (kn)); kd = factor ( simple (kd)); k = simple (kn/kd^(3/2)) The execution of this script gives k = -1/4/(2-2*cos(3*t))^(1/2)/b
84
Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 200984 This result can be simplified with the following trigonometric identity Then To make this final change, we employ subs as follows k = simple ( subs (k, 2-2*cos(3*t), 4*sin(3*t/2)^2)) to obtain k = -1/8/sin(3/2*t)/b
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.