Programming Mathematics Using MATLAB 1st Edition Lisa A. Oberbroeckling - The ebook is now available, just one click to start reading
Programming Mathematics Using MATLAB 1st Edition Lisa A. Oberbroeckling - The ebook is now available, just one click to start reading
com
https://textbookfull.com/product/programming-mathematics-
using-matlab-1st-edition-lisa-a-oberbroeckling/
OR CLICK HERE
DOWLOAD EBOOK
https://textbookfull.com/product/programming-mathematics-using-
matlab-1st-edition-oberbroeckling-lisa-a/
textbookfull.com
https://textbookfull.com/product/programming-mathematics-using-
matlab-1st-edition-oberbroeckling-lisa-a-2/
textbookfull.com
https://textbookfull.com/product/linear-programming-using-matlab-
springer-optimization-and-its-applications-127-ploskas/
textbookfull.com
https://textbookfull.com/product/engineering-mathematics-with-
matlab-1st-edition-won-y-yang/
textbookfull.com
Fundamentals of Graphics Using MATLAB 1st Edition Ranjan
Parekh
https://textbookfull.com/product/fundamentals-of-graphics-using-
matlab-1st-edition-ranjan-parekh/
textbookfull.com
https://textbookfull.com/product/design-optimization-using-matlab-and-
solidworks-1st-edition-suresh/
textbookfull.com
https://textbookfull.com/product/matlab-a-practical-introduction-to-
programming-and-problem-solving-stormy-attaway/
textbookfull.com
https://textbookfull.com/product/matlab-a-practical-introduction-to-
programming-and-problem-solving-stormy-attaway-2/
textbookfull.com
https://textbookfull.com/product/nonlinear-control-systems-using-
matlab-first-edition-boufadene/
textbookfull.com
PROGRAMMING
MATHEMATICS
USING MATLAB®
PROGRAMMING
MATHEMATICS USING
MATLAB®
LISA A. OBERBROECKLING
Department of Mathematics and Statistics
Loyola University Maryland
Baltimore, MD, United States
Academic Press is an imprint of Elsevier
125 London Wall, London EC2Y 5AS, United Kingdom
525 B Street, Suite 1650, San Diego, CA 92101, United States
50 Hampshire Street, 5th Floor, Cambridge, MA 02139, United States
The Boulevard, Langford Lane, Kidlington, Oxford OX5 1GB, United Kingdom
Copyright © 2021 Elsevier Inc. All rights reserved.
ISBN: 978-0-12-817799-0
Preface xiii
Introduction xv
Part 1. MATLAB®
1. Introduction to MATLAB® 3
1.1. Basic MATLAB® information 3
1.1.1. Starting MATLAB 3
1.1.2. Good commands to know 3
1.2. Basic mathematics 4
1.2.1. Built-in mathematical functions 5
1.2.2. Precedence rules 6
1.2.3. Formats 8
1.3. Variables 9
1.4. Diaries and script files 10
1.5. Exercises 12
3. Plotting in MATLAB® 33
3.1. Basic 2D plots 33
3.2. Bad domain examples 34
3.3. Axis settings 35
3.4. Multiple plots 40
3.5. Color, line, and marker modifications 43
3.5.1. Clf/close all 46
3.5.2. Subplots 46
3.6. Other 2D plots 49
3.6.1. Parametric curves 49
3.6.2. Polar curves 50
3.7. Exercises 52
4. Three-Dimensional Plots 59
4.1. Vector functions or space curves 59
vii
viii Contents
5. Functions 83
5.1. The lookfor and help commands 83
5.2. File format 84
5.3. Function examples 86
5.3.1. Basic function examples 86
5.3.2. More function examples – multiple inputs 86
5.3.3. Multiple outputs 87
5.3.4. Bad examples 89
5.4. Exercises 90
6. Control Flow 93
6.1. Relational and logical operators 93
6.2. If statements 97
6.3. Switch/case 99
6.4. Use of characteristic functions 99
6.5. For loops 100
6.6. While loops 102
6.7. Useful commands break, continue, return, and error 103
6.8. Optional inputs and outputs of functions 104
6.9. Exercises 107
References 271
Index 273
Preface
This book started in 2004 when I started to use MATLAB® in my courses at Loyola
University Maryland. I started including a few MATLAB projects within the intro-
duction to linear algebra and multivariable calculus courses. I also taught a one-credit
MATLAB course that was required of every mathematics major. Later the course was
changed to a three-credit course. I expanded the previous assignments and added some
others. The class notes and assignments from over the years have expanded into this
book.
My philosophy has always been to use MATLAB to practice basic programming skills
with mathematics topics students had seen previously, such as numerical integration, and
topics they likely had not seen such as fractals. Visualizing mathematics has always been
important.
Supplements
Student companion site: Please visit https://www.elsevier.com/books-and-journals/
book-companion/9780128177990
Instructor-only site: Qualified instructors can register and access teaching materials
at https://textbooks.elsevier.com/web/Manuals.aspx?isbn=9780128177990
xiii
Introduction
The goal of the course and thus book is to introduce MATLAB® and to practice basic
programming techniques. There is a lot more to MATLAB than what is covered in this
book. Most students have already had some programming experience in another lan-
guage before taking the course this book has stemmed from, although it is not necessary.
On completion of the course/book, one should be familiar enough with MATLAB to
explore more complicated features and commands. Deepening your understanding of
mathematics and learning new topics are bonuses!
xv
CHAPTER 1
Introduction to MATLAB®
1.1. Basic MATLAB® information
1.1.1 Starting MATLAB
MATLAB has many different windows or panels, the first three of which are on the
main screen by default (see Table 1.1).
You can always modify the layout of the panels including “docking” or “undock-
ing” them, in the “Home” view, select “Layout” from the menu and the top item is
“Default.” In order to use MATLAB successfully, you should pay attention to the Cur-
rent Folder. Otherwise, MATLAB may not be able to save and run your programs/files
successfully.
clear/close named figures, etc. such as clf(2) and close(2) that will clear or close
Figure 2, respectively.
• format Sends the output display back to the default format. The command format
compact will not have as much white space (blank lines) between subsequent
commands and output.
• exit or quit Quit MATLAB. You can also quit/exit MATLAB by using the
MENU option under “File” or the usual close application icon within a Mac or
Windows environment.
ans =
>> 10/3
ans =
3.3333
>> 10\3
ans =
0.3000
Thus the forward slash is our usual division, “3 divided by 2” while the backslash is “3
divided into 2.” The need for both of these become more apparent when working with
matrices.
“Dot” operations or component-wise operations are useful and/or necessary for
use with vectors and matrices (discussed in Section 2.4). These are considered “array
arithmetic operations” and are carried out element or component-wise.
MATLAB does NUMERICAL, rather than algebraic computations, as seen below.
Think about what is expected versus what is given as the answer to the subraction
calculation.
>> asin(1/2)
ans =
0.5236
ans =
-1.1102e-16
>> mod(10,3)
ans =
1
6 Programming Mathematics Using MATLAB®
>> rem(10,3)
ans =
>> mod(10,-3)
ans =
-2
>> rem(10,-3)
ans =
>> mod(10,0)
ans =
10
>> rem(10,0)
ans =
NaN
If you have a long calculation/expression, you can continue on the next line in
the command window or within a MATLAB file with the ellipses or continuation
operator. Note when the ellipses work with/without spaces:
ans =
-89
ans =
-89
>> 1+2*3-12^2/3*...
2
8 Programming Mathematics Using MATLAB®
ans =
-89
>> 1+2*3-12^2/3...
1+2*3-12^2/3...
|
Error: Unexpected MATLAB operator.
1.2.3 Formats
As mentioned above, the format command returns the format back to the default
format, which is the same as format short. Generally speaking, this will display a
number up to four decimal places, while format long will display 15. In scientific
notation, this amounts to five and 16 significant digits, respectively. See Table 1.4.
>> pi
ans =
3.1416
ans =
3.141592653589793
There are other built-in formats, including how numbers in scientific notation are dis-
played. See help format for more examples.
Another useful command is format compact and format loose (default). This
will change how the output is displayed.
>> pi
ans =
3.1416
>> pi
ans =
3.1416
>>
The subsequent commands shown in this text will use format compact (see Table 1.4).
1.3. Variables
The format for a variable assignment is as follows:
Variable name = Numerical value or computable expression
Some conventions:
• The = is the assignment operator which assigns a value to a variable.
• Left-hand side can include only one variable name.
• Right-hand side can be a number or an expression made up of numbers, functions,
and/or variables previously assigned numerical values.
• Variables must begin with a letter.
• Press the Enter/Return key to make the assignment.
• The variable ans is the value of the last expression that is not assigned.
• Be careful with variable names. For example, do not name a variable help or sin.
• Variable names are case sensitive; thus A is not the same as a.
Remember:
• Use semicolon (;) to suppress screen output.
• Multiple commands can be typed on one line by typing a comma (,) between them
if they are not already ended with a semicolon (;).
10 Programming Mathematics Using MATLAB®
√
Example 1.3.1. Assign the number 3 to variable a and 4 to variable b. Print a2 + b2
and assign the solution to the variable c.
Notice in the above example, you do not need spaces around the “=” for variable
assignments but you may use them for aesthetic reasons.
>> x = pi/5;
>> LHS = (cos(x/2))^2, RHS = (tan(x)+sin(x))/(2*tan(x))
LHS =
0.9045
RHS =
0.9045
>> format long
>> LHS, RHS
LHS =
0.904508497187474
RHS =
0.904508497187474
>> LHS-RHS
ans =
-1.110223024625157e-16
more than once (within the same current folder), it will continue to APPEND to the
file.
For example, the commands above will generate the following text in the file “file-
name”:
1+1
ans =
2
diary off
3^2
ans =
9
diary
Script files, or m-files, are extremely useful for running and rerunning code. You
may be required to turn in script files for your assignments.
• Script files are ASCII files (plain text files) with extension .m; thus they are also
called m-files. These are basically batch files.
• When you run a SCRIPT file, MATLAB executes each line as if you typed each
line into the command window.
• Script files are very useful; you can edit them, save them, execute them many times
and “tweak” them to experiment with commands.
• The MATLAB editor window is the best way to create and edit script files.
• To avoid extraneous output to the command window, put “;” after variable assign-
ments or intermediate calculations.
• Comments within MATLAB files begin with the percent symbol (%).
Running script files:
There are many ways to run an m-file of name filename.m. First, you must MAKE
SURE CURRENT FOLDER IS CORRECT!
12 Programming Mathematics Using MATLAB®
1. >> filename
2. >> run filename
3. >> run('filename')
4. Within the Editor tab, chose run...
1.5. Exercises
1. Basic calculations Use MATLAB to do the following calculations. Be careful! The
following are displayed using regular mathematical notation; you need to figure out
what MATLAB functions are needed.
7 .753 292 644/3
(a) (2.4)(64 ) + 8 , (b) + + 20 · 9−3 ,
16 2 − 225 5 11
(c) cos(360), (d) cos(360◦ ),
(k) From the above calculations, do you see anything surprising with the answers?
1 √
(l) Calculate (−x) 4 and 4 −x. What are the differences?
(m) Calculate (−8)2/3 and 82/3 on paper using your exponent rules. Now do the
calculations within MATLAB, Excel, Google.com, WolframAlpha.com, and
a scientific or graphing calculator (specifying what model you have used).
What are the differences, if there are any?
7. Calculator precision √
(a) Within an Excel spreadsheet or Google Sheet, calculate 12 178212 + 184112
using exponential notation for the calculations. Write your answer clearly.
(b) Rewrite the above expression with your answer to part (a) into an equation
and algebraically simplify the equation so there are no radicals or rational
exponents. √
(c) Now calculate 12 178212 + 184112 using MATLAB.
(d) Compare the left-hand side and right-hand side of the equation you get in
part (b) by subtraction within MATLAB, using format long).
14 Programming Mathematics Using MATLAB®
(e) Calculate “3 quadrillion and 18 minus 3 quadrillion and 14” in your head
and write it down. Now translate this into mathematics so you can calculate
it within Google.com, Excel, and MATLAB. (You may need to look up how
many zeros you will need!) Compare your answers in a table.
(f) Do the same with 2.000000000000018 − 2.000000000000014.
8. Ambiguities with notation. Define variables with the assignments x = 10 and
y = π/4. Calculate the following within MATLAB. You may have to adjust from
mathematical notation to correct MATLAB notation. Make sure you are using the
default format!
(a) cos y, (b) cos y2 , (c) cos(y2 ), (d) cos2 y, (e) (cos y)2 ,
(j) Redo part (g) and then use the MATLAB variable ans to calculate
cos−1 (x/20)
.
4y
(k) Are any of the above calculations ambiguous in how they are written (which
ones and why)? What could be done to make the calculations clearer to the
person performing/entering the calculations?
9. Exploring rem and div. It is common to use either of the functions mod or rem
to tell whether positive integers are even or odd, among other uses. We will explore
the differences and similarities of these functions.
(a) Here is another simple use for these functions. You are given a list of 10-digit
numbers. You would like to only use the last seven digits of these numbers (for
example, for display purposes). Use both the mod or rem functions to easily
get the last seven digits of the number 4108675309. Do you see a difference
in their use for this?
(b) Use the same commands on the number -4108675309. Do you see a differ-
ence? Explain in your own words what you think is the difference between
the mod and rem functions. Is there a difference when using these functions
to tell whether any integer is even or odd?
(c) Can you come up with a way, using MATLAB functions such as mod, rem,
round, ceil, fix, etc. to capture the “area code” (first three digits) of
4108675309? Experiment with at least two phone numbers with different
area codes.
(d) What about capturing the “central office” part of the number (867)? Do it
for 4108675309 and 4106172000.
CHAPTER 2
>> y = [4;5;6]
y =
4
5
6
There are ways to define vectors without having to enter every element. The two most
common ways are to define constant spaced and equally spaced (linearly spaced) vectors.
variablename = [m: q : n]
1 3 5
z =
1 4 7 10 13 16 19 22 25
w =
1 4 7 10 13 16 19 22
Notice in the vectors y and w above that the last number (n) may not actually be a value
in the vector if the increment (q) is such that it will not occur based on the starting
value (m).
One can also increment backwards with a negative increment value.
Notice that both the values m and n are elements in the vector.
There is also a command logspace that is also useful for plotting (among other
things). The command logspace(a,b) will generate a row vector of 50 logarithmically
spaced points from 10a and 10b . If b is pi, then the points are between 10a and π .
variablename = logspace(a,b)
variablename = logspace(a,b,q)
Just as in linspace, you can specify the number of elements in the vector by stating
the value q.
Vectors and Matrices (Arrays) 17
>> logspace(0,5,6)
ans =
1 10 100 1000 10000 100000
>> logspace(5,1,5)
ans =
100000 10000 1000 100 10
>> linspace(1,pi,4)
ans =
1.0000 1.7139 2.4277 3.1416
>> logspace(0,pi,4)
ans =
1.0000 1.4646 2.1450 3.1416
>> logspace(2,pi,3)
ans =
100.0000 17.7245 3.1416
2x + 3y + z = 4,
x − 5y + 3z = 3,
4x − 2y + 3z = 2.
The format for defining matrices expands on the format for defining row and column
vectors; spaces or commas between elements within a row with semicolons between
rows.
variable = [1st row ; 2nd row ; ... ; last row]
You can also use vectors of the same size to be rows (or columns) of a matrix, as we do
to define the matrix B below.
Elements of vectors
• v(k) picks the kth element of v.
• v(m:n) picks the mth through the nth elements of v.
>> v = linspace(0,1,5)
v =
0 0.2500 0.5000 0.7500 1.0000
>> v(3)
ans =
0.5000
>> v(2:4)
ans =
0.2500 0.5000 0.7500
Elements of matrices
• A(m,n) picks the (m, n)th element (element in the mth row, nth column) of the
matrix A.
• A(m:n, p:q) gives the submatrix from the elements (m : n) × (p : q)
• A(m:n, :) gives rows m through n, and every column of A
Another Random Document on
Scribd Without Any Related Topics
The Project Gutenberg eBook of The Theory of
Environment
This ebook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this ebook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.
Language: English
Part I
PHILOSOPHY
BY
“.............................
He fixed thee ’mid this dance
Of plastic circumstance.”
Copyright, 1918
By Armin H. Koller
TO
MY PARENTS
CONTENTS
PAGE
Summary 93
Appendix 97
PREFACE
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com