chapter 1an engineer’s guide to matlab copyright © edward b. magrab 20091 an engineer’s guide...
TRANSCRIPT
![Page 1: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/1.jpg)
Copyright © Edward B. Magrab 2009 1
Chapter 1An Engineer’s Guide to MATLAB
AN ENGINEER’S GUIDE TO MATLAB
3rd Edition
CHAPTER 1
INTRODUCTION
![Page 2: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/2.jpg)
Copyright © Edward B. Magrab 2009 2
Chapter 1An Engineer’s Guide to MATLAB
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.
![Page 3: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/3.jpg)
Copyright © Edward B. Magrab 2009 3
Chapter 1An Engineer’s Guide to MATLAB
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)
![Page 4: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/4.jpg)
Copyright © Edward B. Magrab 2009 4
Chapter 1An Engineer’s Guide to MATLAB
Chapter 1 – Objective
To introduce the fundamental characteristics of the MATLAB environment and the language’s basic syntax
![Page 5: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/5.jpg)
Copyright © Edward B. Magrab 2009 5
Chapter 1An Engineer’s Guide to MATLAB
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.
![Page 6: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/6.jpg)
Copyright © Edward B. Magrab 2009 6
Chapter 1An Engineer’s Guide to MATLAB
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.
![Page 7: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/7.jpg)
Copyright © Edward B. Magrab 2009 7
Chapter 1An Engineer’s Guide to MATLAB
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.
![Page 8: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/8.jpg)
Copyright © Edward B. Magrab 2009 8
Chapter 1An Engineer’s Guide to MATLAB
Some Suggestions on How to Use MATLAB
Most importantly, verify by independent means that the outputs from the scripts and functions are correct.
![Page 9: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/9.jpg)
Copyright © Edward B. Magrab 2009 9
Chapter 1An Engineer’s Guide to MATLAB
Notation Conventions
Variable/Function Name Font Example
User-created variable Times Roman ExitPressurea2, sig
MATLAB function Courier cosh(x), pi
User-created function Times Roman BeamRoots(a, x, k)Bold
Numerical Value Font Example
Provided in program Times Roman 5.672
Output to command window Helvetica 5.672or to a graphical display
![Page 10: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/10.jpg)
Copyright © Edward B. Magrab 2009 10
Chapter 1An Engineer’s Guide to MATLAB
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
![Page 11: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/11.jpg)
Copyright © Edward B. Magrab 2009 11
Chapter 1An Engineer’s Guide to MATLAB
Command Window, Command History, Workspace
![Page 12: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/12.jpg)
Copyright © Edward B. Magrab 2009 12
Chapter 1An Engineer’s Guide to MATLAB
Command Window, Command History, Workspace, and Editor
![Page 13: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/13.jpg)
Copyright © Edward B. Magrab 2009 13
Chapter 1An Engineer’s Guide to MATLAB
Command Window and Editor
MATLAB command window (left) and editor (right) after closing the command history and workspace windows
![Page 14: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/14.jpg)
Copyright © Edward B. Magrab 2009 14
Chapter 1An Engineer’s Guide to MATLAB
Clearing of Command Window and Workspace
MATLAB function Description
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
![Page 15: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/15.jpg)
Copyright © Edward B. Magrab 2009 15
Chapter 1An Engineer’s Guide to MATLAB
These operation can also be done with
![Page 16: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/16.jpg)
Copyright © Edward B. Magrab 2009 16
Chapter 1An Engineer’s Guide to MATLAB
and with
format compact
format long e
format short
![Page 17: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/17.jpg)
Copyright © Edward B. Magrab 2009 17
Chapter 1An Engineer’s Guide to MATLAB
Option Display (number > 0) Display (number < 0)
shortlongshort elong eshort glong gshort englong engrationalhexbank
444.44444.444444444444445e+0024.4444e+0024.444444444444445e+002444.44444.444444444444444.4444e+000444.444444444444e+0004000/9407bc71c71c71c72444.44
0.00440.0044444444444444.4444e-0034.444444444444444e-0030.00444440.004444444444444444.4444e-0034.44444444444444e-0031/2253f723456789abcdf0.00
Results from Different Format Selections
![Page 18: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/18.jpg)
Copyright © Edward B. Magrab 2009 18
Chapter 1An Engineer’s Guide to MATLAB
Preferences Menu Selections: Font Size
![Page 19: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/19.jpg)
Copyright © Edward B. Magrab 2009 19
Chapter 1An Engineer’s Guide to MATLAB
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
![Page 20: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/20.jpg)
Copyright © Edward B. Magrab 2009 20
Chapter 1An Engineer’s Guide to MATLAB
Keywords Reserved Explicitly for the MATLAB Programming Language
breakcasecatchcontinueelseelseifendforfunction
globalifotherwisepersistentreturnswitchtrywhile
![Page 21: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/21.jpg)
Copyright © Edward B. Magrab 2009 21
Chapter 1An Engineer’s Guide to MATLAB
» p = 7.1p = 7.1000» x = 4.92x = 4.9200» k = -1.7k = -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
![Page 22: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/22.jpg)
Copyright © Edward B. Magrab 2009 22
Chapter 1An Engineer’s Guide to MATLAB
» 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.1000x = 4.9200k = -1.7000»
Using semicolon instead of commas
suppresses this output
![Page 23: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/23.jpg)
Copyright © Edward B. Magrab 2009 23
Chapter 1An Engineer’s Guide to MATLAB
Arithmetic Operators
Hierarchy Level
( ) Parentheses 1
´ Prime (Apostrophe) 1
^ Exponentiation 2
* Multiplication 3
/ Division 3
+ Addition 4
Subtraction 4
![Page 24: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/24.jpg)
Copyright © Edward B. Magrab 2009 24
Chapter 1An Engineer’s Guide to MATLAB
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.
![Page 25: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/25.jpg)
Copyright © Edward B. Magrab 2009 25
Chapter 1An Engineer’s Guide to MATLAB
Some Examples
Mathematical expression
MATLAB expression
1 dcx+2 dcx + 2 (2/d)cx+2 (dcx + 2)/g2.7
2xdc
1-d*c^(x+2) d*c^x+2 or 2+d*c^x (2/d)*c^(x+2) or 2/d*c^(x+2) or 2*c^(x+2)/d (d*c^x+2)/g^2.7 sqrt(d*c^x+2) or (d*c^x+2)^0.5
![Page 26: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/26.jpg)
Copyright © Edward B. Magrab 2009 26
Chapter 1An Engineer’s Guide to MATLAB
Another Example
Consider
1
1+
k
tpx
The MATLAB script is
p = 7.1; x = 4.92; k = 1.7; % Numerical values % must be assigned
firstt = (1/(1+p*x))^k
which results in
t = 440.8779
![Page 27: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/27.jpg)
Copyright © Edward B. Magrab 2009 27
Chapter 1An Engineer’s Guide to MATLAB
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 (;)
![Page 28: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/28.jpg)
Copyright © Edward B. Magrab 2009 28
Chapter 1An Engineer’s Guide to MATLAB
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)
-1
Note:
Real and complex numbers can be mixed without any special concerns.
Example 1
z = 4 + sqrt(-4)
System displaysz = 4.0000 + 2.0000i
Example 2
z = 1i^1i
System displaysz = 0.2079
![Page 29: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/29.jpg)
Copyright © Edward B. Magrab 2009 29
Chapter 1An Engineer’s Guide to MATLAB
(2) Exponential Form: x = 4.56102
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.
![Page 30: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/30.jpg)
Copyright © Edward B. Magrab 2009 30
Chapter 1An Engineer’s Guide to MATLAB
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
![Page 31: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/31.jpg)
Copyright © Edward B. Magrab 2009 31
Chapter 1An Engineer’s Guide to MATLAB
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
![Page 32: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/32.jpg)
Copyright © Edward B. Magrab 2009 32
Chapter 1An Engineer’s Guide to MATLAB
Mathematical function MATLAB expression
ex ex 1 x << 1
x ln(x) or loge(x) log10(x) |x| signum(x) loge(1+x) x << 1 n! All prime numbers n
exp(x) expm1(x) sqrt(x)
log(x)
log10(x) abs(x) sign(x) log1p(x) factorial(n) primes(n)
Some Elementary MATLAB Functions
![Page 33: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/33.jpg)
Copyright © Edward B. Magrab 2009 33
Chapter 1An Engineer’s Guide to MATLAB
Some MATLAB Constants and Special Quantities
Mathematical quantity or operation
MATLAB expression
Comments
1
Floating point relative accuracy 0/0, 0×, / Largest floating-point number before overflow Smallest floating-point number before underflow
pi i or j eps inf NaN realmax realmin
3.141592653589793 Indicates complex quantity. 2.2210-16 Infinity Undefined mathematical operation 1.7977e+308
2.2251e-308
![Page 34: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/34.jpg)
Copyright © Edward B. Magrab 2009 34
Chapter 1An Engineer’s Guide to MATLAB
MATLAB Trigonometric and Hyperbolic Functions
Trigonometric Hyperbolic
Function (radians) (degrees) Inverse Inverse
sine cosine tangent secant cosecant cotangent
sin(x) cos(x) tan(x) sec(x) csc(x) cot(x)
sind(x) cosd(x) tand(x) secd(x) cscd(x) cotd(x)
asin(x) acos(x) atan(x)† asec(x) acsc(x) acot(x)
sinh(x) cosh(x) tanh(x) sech(x) csch(x) coth(x)
asinh(x) acosh(x) atanh(x) asech(x) acsch(x) acoth(x)
† atan2(y, x) is the four quadrant version.
![Page 35: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/35.jpg)
Copyright © Edward B. Magrab 2009 35
Chapter 1An Engineer’s Guide to MATLAB
Several Specialized Mathematical Functions
Mathematical function
MATLAB Expression
Description
Ai(x) Bi(x) I(x) J(x) K(x) Y(x) B(x,w) K(m), E(m) erf(x), erfc(x) E1(z) (a)
( )mnP x
airy(0,x) airy(2,x) besseli(nu, x) besselj(nu, x) besselk(nu, x) bessely(nu, x) beta(x, w) ellipke(m) erf(x), erfc(x) expint(x) gamma(a) legendre(n, x)
Airy function Airy function Modified Bessel function of first kind Bessel function of first kind Modified Bessel function of second kind Bessel function of second kind Beta function Complete elliptic integrals of 1st & 2nd kind Error and complementary error function Exponential integral Gamma function Associated Legendre function
![Page 36: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/36.jpg)
Copyright © Edward B. Magrab 2009 36
Chapter 1An Engineer’s Guide to MATLAB
Several Specialized Statistical Functions
Mathematical function MATLAB Expression
Description
maximum value of x median minimum value of x mode or s 2 or s2
max(x) mean(x) median(x) min(x) mode(x) std(x) var(x)
Largest element(s) in an array Average or mean value of an array Median value of an array Smallest element(s) in an array Most frequent values in an array Standard deviation of an array Variance of an array of values
![Page 37: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/37.jpg)
Copyright © Edward B. Magrab 2009 37
Chapter 1An Engineer’s Guide to MATLAB
MATLAB Relational Operators
Conditional Mathematical symbol
MATLAB symbol
equal not equal less than greater than less than or equal greater than or equal
= < >
== ~= < > <= >=
![Page 38: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/38.jpg)
Copyright © Edward B. Magrab 2009 38
Chapter 1An Engineer’s Guide to MATLAB
Example
For x = 0.1 and a = 0.5, determine the value of y when
-= - sin( )/cosh( ) - ln ( )πxey e x a x + a
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 returnsy = 1.0736
![Page 39: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/39.jpg)
Copyright © Edward B. Magrab 2009 39
Chapter 1An Engineer’s Guide to MATLAB
Decimal-to-Integer Conversion Functions
MATLAB function
x y Description
y = fix(x)
2.7 1.9 2.49-2.51j
2.0000 1.0000 2.0000 2.0000i
Round toward
zero
y = round(x)
2.7 1.9 2.49 2.51j
3.0000 2.0000 2.0000 3.0000i
Round to nearest
integer
y = ceil(x)
2.7 1.9 2.49 2.51j
3.0000 1.0000 3.0000 - 2.0000i
Round toward
infinity
y = floor(x)
2.7 1.9 2.49 2.51j
2.0000 2.0000 2.0000 3.0000i
Round toward
minus infinity
![Page 40: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/40.jpg)
Copyright © Edward B. Magrab 2009 40
Chapter 1An Engineer’s Guide to MATLAB
Complex Number Manipulation Functions
MATLAB function z y Description
z = complex(a, b) a + b*j - Form complex number; a and b real
y = abs(z) 3 + 4j 5 Absolute value: 2 2a b
y = conj(z) 3 + 4j 3 4j Complex conjugate
y = real(z) 3 + 4j 3 Real part
y = imag(z) 3 + 4j 4 Imaginary part
y = angle(z) a +b*j atan2(b, a) Phase angle in radians: y
![Page 41: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/41.jpg)
Copyright © Edward B. Magrab 2009 41
Chapter 1An Engineer’s Guide to MATLAB
Additional Special Characters and a Summary of Their Usage
![Page 42: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/42.jpg)
Copyright © Edward B. Magrab 2009 42
Chapter 1An Engineer’s Guide to MATLAB
Additional Special Characters and a Summary of Their Usage
![Page 43: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/43.jpg)
Copyright © Edward B. Magrab 2009 43
Chapter 1An Engineer’s Guide to MATLAB
Additional Special Characters and a Summary of Their Usage
![Page 44: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/44.jpg)
Copyright © Edward B. Magrab 2009 44
Chapter 1An Engineer’s Guide to MATLAB
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.
![Page 45: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/45.jpg)
Copyright © Edward B. Magrab 2009 45
Chapter 1An Engineer’s Guide to MATLAB
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
![Page 46: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/46.jpg)
Copyright © Edward B. Magrab 2009 46
Chapter 1An Engineer’s Guide to MATLAB
Additional Reasons -
• Click one icon to save and run a program (Must use Save As first time)
Save and Run icon
![Page 47: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/47.jpg)
Copyright © Edward B. Magrab 2009 47
Chapter 1An Engineer’s Guide to MATLAB
Additional Reasons -
Enabling M-Lint from the Preferences menu
![Page 48: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/48.jpg)
Copyright © Edward B. Magrab 2009 48
Chapter 1An Engineer’s Guide to MATLAB
Illustration of M-Lint
Orange bar
Red bar
Red square
With cursor placed on red bar, this error message is displayed.
![Page 49: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/49.jpg)
Copyright © Edward B. Magrab 2009 49
Chapter 1An Engineer’s Guide to MATLAB
Additional Reasons –
Enabling the cell feature of the Editor
![Page 50: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/50.jpg)
Copyright © Edward B. Magrab 2009 50
Chapter 1An Engineer’s Guide to MATLAB
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
![Page 51: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/51.jpg)
Copyright © Edward B. Magrab 2009 51
Chapter 1An Engineer’s Guide to MATLAB
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
![Page 52: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/52.jpg)
Copyright © Edward B. Magrab 2009 52
Chapter 1An Engineer’s Guide to MATLAB
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.
![Page 53: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/53.jpg)
Copyright © Edward B. Magrab 2009 53
Chapter 1An Engineer’s Guide to MATLAB
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
![Page 54: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/54.jpg)
Copyright © Edward B. Magrab 2009 54
Chapter 1An Engineer’s Guide to MATLAB
Saving/Executing (Running) M Files
Set Path window (File)
Change current path to file being executed.
![Page 55: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/55.jpg)
Copyright © Edward B. Magrab 2009 55
Chapter 1An Engineer’s Guide to MATLAB
Accessing the Browser Window to Change Current Path (Directory)
Clicking on this icon brings up the
Browser
![Page 56: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/56.jpg)
Copyright © Edward B. Magrab 2009 56
Chapter 1An Engineer’s Guide to MATLAB
Example - Flow in a circular channel
3/23/2 5/2c
5/2
2 - 0.5sin 2=
8 sin 1- cos
D g θ θQ
θ θ
12
cosc
dD
Let d = 2 m, g = 9.8 m/s2, and = 60 = /3. Then the script is
g = 9.8; d = 2; th = pi/3; % InputDc = 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
Dc
d/2 d/2 2
![Page 57: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/57.jpg)
Copyright © Edward B. Magrab 2009 57
Chapter 1An Engineer’s Guide to MATLAB
The various MATLAB windows after executing the
program
![Page 58: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/58.jpg)
Copyright © Edward B. Magrab 2009 58
Chapter 1An Engineer’s Guide to MATLAB
On-Line Help: Getting Started
![Page 59: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/59.jpg)
Copyright © Edward B. Magrab 2009 59
Chapter 1An Engineer’s Guide to MATLAB
On-Line Help: Desktop
![Page 60: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/60.jpg)
Copyright © Edward B. Magrab 2009 60
Chapter 1An Engineer’s Guide to MATLAB
On-Line Help: Command Window
![Page 61: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/61.jpg)
Copyright © Edward B. Magrab 2009 61
Chapter 1An Engineer’s Guide to MATLAB
On-Line Help: When Function Name is Known
Type in function name
![Page 62: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/62.jpg)
Copyright © Edward B. Magrab 2009 62
Chapter 1An Engineer’s Guide to MATLAB
On-Line Help: When Function Name Is Not Known
Type search entry and press Enter
![Page 63: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/63.jpg)
Copyright © Edward B. Magrab 2009 63
Chapter 1An Engineer’s Guide to MATLAB
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.
![Page 64: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/64.jpg)
Copyright © Edward B. Magrab 2009 64
Chapter 1An Engineer’s Guide to MATLAB
We will illustrate by example –
• Syntax
• Variable precision arithmetic
• Taylor series expansions
• Differentiation and integration
• Limits
• Substitution
• Inverse Laplace transform
![Page 65: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/65.jpg)
Copyright © Edward B. Magrab 2009 65
Chapter 1An Engineer’s Guide to MATLAB
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.
![Page 66: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/66.jpg)
Copyright © Edward B. Magrab 2009 66
Chapter 1An Engineer’s Guide to MATLAB
Consider the expression 2-= 11.92 +af e b/d
Assuming that d = 4.2 (1/d = 0.238095), the script to represent this expression symbolically is
syms a bd = 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
![Page 67: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/67.jpg)
Copyright © Edward B. Magrab 2009 67
Chapter 1An Engineer’s Guide to MATLAB
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 bd = 4.2;f = vpa(11.92*exp(-a^2)+b/d, 5)
2-= 11.92 +af e b/d
![Page 68: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/68.jpg)
Copyright © Edward B. Magrab 2009 68
Chapter 1An Engineer’s Guide to MATLAB
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.
![Page 69: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/69.jpg)
Copyright © Edward B. Magrab 2009 69
Chapter 1An Engineer’s Guide to MATLAB
Consider the evaluation of the following expression 10032!y e
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×1043
![Page 70: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/70.jpg)
Copyright © Edward B. Magrab 2009 70
Chapter 1An Engineer’s Guide to MATLAB
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
![Page 71: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/71.jpg)
Copyright © Edward B. Magrab 2009 71
Chapter 1An Engineer’s Guide to MATLAB
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 tdt = 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
![Page 72: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/72.jpg)
Copyright © Edward B. Magrab 2009 72
Chapter 1An Engineer’s Guide to MATLAB
Integration is performed with the function
int(f, x, c, d)
where
f = f(x) is a symbolic expressionx = variable of integrationc = 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)
![Page 73: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/73.jpg)
Copyright © Edward B. Magrab 2009 73
Chapter 1An Engineer’s Guide to MATLAB
Example
We shall integrate the results of the differentiation performed previously. Thus,
syms b tf = 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))
![Page 74: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/74.jpg)
Copyright © Edward B. Magrab 2009 74
Chapter 1An Engineer’s Guide to MATLAB
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 of2
Lim3 4a
a b
a
![Page 75: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/75.jpg)
Copyright © Edward B. Magrab 2009 75
Chapter 1An Engineer’s Guide to MATLAB
The script is
syms a bLim = limit((2*a+b)/(3*a-4), a, inf)
where inf =
Example
Determine the limit of
2Lim
3 4a
a b
a
xLim 1
xy
x
![Page 76: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/76.jpg)
Copyright © Edward B. Magrab 2009 76
Chapter 1An Engineer’s Guide to MATLAB
The script is
syms y xLim = limit((1+y/x)^x, x, inf)
Upon execution, we obtain
Lim =exp(y)
In other words, the limit is ey.
xLim 1
xy
x
![Page 77: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/77.jpg)
Copyright © Edward B. Magrab 2009 77
Chapter 1An Engineer’s Guide to MATLAB
Taylor Series Expansion
An n-term Taylor series expansion of a function f(x) about the point a is given by
( )1
0
( )( )
!
knk
k
f ax a
k
The function that performs this operation is
taylor(f, n, a, x)
![Page 78: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/78.jpg)
Copyright © Edward B. Magrab 2009 78
Chapter 1An Engineer’s Guide to MATLAB
Example
Obtain a four-term expansion of cos about o. The script is
syms x thoTay = 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,
2 31 1cos sin cos sin
2 6o o o o o o ox x x
![Page 79: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/79.jpg)
Copyright © Edward B. Magrab 2009 79
Chapter 1An Engineer’s Guide to MATLAB
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)
![Page 80: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/80.jpg)
Copyright © Edward B. Magrab 2009 80
Chapter 1An Engineer’s Guide to MATLAB
2
1( )
2 1F s
s s
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)
![Page 81: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/81.jpg)
Copyright © Edward B. Magrab 2009 81
Chapter 1An Engineer’s Guide to MATLAB
To simplify this expression, we make use of the following change of variables
2 2 2z^2-1 1 1 r
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 2
2sin 1
1
tet
![Page 82: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/82.jpg)
Copyright © Edward B. Magrab 2009 82
Chapter 1An Engineer’s Guide to MATLAB
Example
Determine the curvature of the parametric curves2 cos cos 2
2 sin sin 2
x b t b t
y b t b t
The curvature is determined from
3/ 22 2
x y y x
x y
where the prime denoted the derivative with respect to t. The script is
![Page 83: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/83.jpg)
Copyright © Edward B. Magrab 2009 83
Chapter 1An Engineer’s Guide to MATLAB
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
1
4 2 2cos3b t
![Page 84: Chapter 1An Engineer’s Guide to MATLAB Copyright © Edward B. Magrab 20091 AN ENGINEER’S GUIDE TO MATLAB 3rd Edition CHAPTER 1 INTRODUCTION](https://reader035.vdocuments.us/reader035/viewer/2022062801/56649e685503460f94b63ab4/html5/thumbnails/84.jpg)
Copyright © Edward B. Magrab 2009 84
Chapter 1An Engineer’s Guide to MATLAB
This result can be simplified with the following trigonometric identity
21 cos 2sin ( / 2)a a
Then
22-2*cos(3*t) 2 1 cos3 4sin 3 / 2t t
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 obtaink =-1/8/sin(3/2*t)/b
1
8 sin(3 / 2)b t