MATLAB for Beginners: A Gentle Approach - Revised Edition
By Peter Kattan
()
About this ebook
This book is written for beginners and students who wish to learn MATLAB. One of the objectives of writing this book is to introduce MATLAB to students in high schools. The material presented is very easy and simple to understand - written in a gentle manner. The topics covered in the book include arithmetic operations, variables, mathematical functions, complex numbers, vectors, matrices, programming, graphs, solving equations, and an introduction to calculus. In addition, the MATLAB Symbolic Math Toolbox is emphasized in this book. There are also over 230 exercises at the ends of chapters for students to practice. Detailed solutions to all the exercises are provided in the second half of the book. The author has been using MATLAB for the past fifteen years and is the author of the best selling book "MATLAB Guide to Finite Elements". For the paperback edition, visit Amazon.com. This description applies to the Revised Edition.
Read more from Peter Kattan
Solving Equations with MATLAB (Taken from the Book "MATLAB for Beginners: A Gentle Approach") Rating: 3 out of 5 stars3/5Programming with MATLAB: Taken From the Book "MATLAB for Beginners: A Gentle Approach" Rating: 5 out of 5 stars5/5A Brief Introduction to MATLAB: Taken From the Book "MATLAB for Beginners: A Gentle Approach" Rating: 3 out of 5 stars3/5Matrices with MATLAB (Taken from "MATLAB for Beginners: A Gentle Approach") Rating: 3 out of 5 stars3/5Graphs with MATLAB (Taken from "MATLAB for Beginners: A Gentle Approach") Rating: 4 out of 5 stars4/5Kindergarten Mazes: Simple Mazes for Kids Rating: 0 out of 5 stars0 ratingsCorridors of Time: Poetic Reflections on the Tapestry of Time Rating: 0 out of 5 stars0 ratings
Related to MATLAB for Beginners
Related ebooks
Learning Open Office: Calc & Base Rating: 0 out of 5 stars0 ratingsExercises of Logarithms and Exponentials Rating: 0 out of 5 stars0 ratingsMastering Racket Programming: From Basics to Expert Proficiency Rating: 0 out of 5 stars0 ratingsMastering C: Advanced Techniques and Tricks Rating: 0 out of 5 stars0 ratingsMATLAB for Beginners: A Gentle Approach Rating: 0 out of 5 stars0 ratingsMATLAB for Beginners: A Gentle Approach - Revised Edition Rating: 3 out of 5 stars3/5A Friendly Introduction to MATLAB Programming Rating: 0 out of 5 stars0 ratingsTop Numerical Methods With Matlab For Beginners! Rating: 0 out of 5 stars0 ratingsStatics with MATLAB® Rating: 0 out of 5 stars0 ratingsIntroduction to Algorithms Rating: 0 out of 5 stars0 ratingsVector Calculus Using Mathematica Second Edition Rating: 0 out of 5 stars0 ratingsKronecker Products and Matrix Calculus with Applications Rating: 0 out of 5 stars0 ratingsEssential Algorithms: A Practical Approach to Computer Algorithms Using Python and C# Rating: 5 out of 5 stars5/5Advanced Numerical Methods with Matlab 1: Function Approximation and System Resolution Rating: 0 out of 5 stars0 ratingsBasic Math Notes Rating: 5 out of 5 stars5/5Matrix Operations for Engineers and Scientists: An Essential Guide in Linear Algebra Rating: 0 out of 5 stars0 ratingsMore on C# in Front Office Rating: 0 out of 5 stars0 ratingsDirect Linear Transformation: Practical Applications and Techniques in Computer Vision Rating: 0 out of 5 stars0 ratingsProfound Python Data Science Rating: 0 out of 5 stars0 ratingsIntroduction to Calculus Rating: 5 out of 5 stars5/5Graph Theory Rating: 0 out of 5 stars0 ratingsThe Practically Cheating Calculus Handbook Rating: 4 out of 5 stars4/5Fundamental Math Rating: 0 out of 5 stars0 ratingsEngineering and Scientific Computations Using MATLAB Rating: 0 out of 5 stars0 ratingsLearn Programming Using C# Rating: 0 out of 5 stars0 ratingsDesign And Analysis Of Algorithm Rating: 0 out of 5 stars0 ratingsElementary Theory and Application of Numerical Analysis: Revised Edition Rating: 0 out of 5 stars0 ratingsDeep Learning Fundamentals in Python Rating: 4 out of 5 stars4/5Calculus Fundamentals Explained Rating: 3 out of 5 stars3/5Surviving Introduction to Finance Rating: 0 out of 5 stars0 ratings
Science & Mathematics For You
The Source: The Secrets of the Universe, the Science of the Brain Rating: 4 out of 5 stars4/5Blitzed: Drugs in the Third Reich Rating: 4 out of 5 stars4/5Outsmart Your Brain: Why Learning is Hard and How You Can Make It Easy Rating: 4 out of 5 stars4/5What If?: Serious Scientific Answers to Absurd Hypothetical Questions Rating: 5 out of 5 stars5/5Sapiens: A Brief History of Humankind Rating: 4 out of 5 stars4/5Feeling Good: The New Mood Therapy Rating: 4 out of 5 stars4/5The Big Book of Hacks: 264 Amazing DIY Tech Projects Rating: 4 out of 5 stars4/5Our Kind of People: Inside America's Black Upper Class Rating: 3 out of 5 stars3/5Mothers Who Can't Love: A Healing Guide for Daughters Rating: 4 out of 5 stars4/5The Gulag Archipelago [Volume 1]: An Experiment in Literary Investigation Rating: 4 out of 5 stars4/5No-Drama Discipline: the bestselling parenting guide to nurturing your child's developing mind Rating: 4 out of 5 stars4/5The Gulag Archipelago: The Authorized Abridgement Rating: 4 out of 5 stars4/5Chaos: Making a New Science Rating: 4 out of 5 stars4/5First, We Make the Beast Beautiful: A New Journey Through Anxiety Rating: 4 out of 5 stars4/5Other Minds: The Octopus, the Sea, and the Deep Origins of Consciousness Rating: 4 out of 5 stars4/5The Invisible Rainbow: A History of Electricity and Life Rating: 5 out of 5 stars5/5A Crack In Creation: Gene Editing and the Unthinkable Power to Control Evolution Rating: 4 out of 5 stars4/5Memory Craft: Improve Your Memory with the Most Powerful Methods in History Rating: 3 out of 5 stars3/5American Carnage: On the Front Lines of the Republican Civil War and the Rise of President Trump Rating: 4 out of 5 stars4/5Ultralearning: Master Hard Skills, Outsmart the Competition, and Accelerate Your Career Rating: 4 out of 5 stars4/5Homo Deus: A Brief History of Tomorrow Rating: 4 out of 5 stars4/5Becoming Cliterate: Why Orgasm Equality Matters--And How to Get It Rating: 4 out of 5 stars4/5The Confidence Code: The Science and Art of Self-Assurance---What Women Should Know Rating: 4 out of 5 stars4/5Alchemy: The Dark Art and Curious Science of Creating Magic in Brands, Business, and Life Rating: 4 out of 5 stars4/5The Systems Thinker: Essential Thinking Skills For Solving Problems, Managing Chaos, Rating: 4 out of 5 stars4/5The Psychology of Totalitarianism Rating: 4 out of 5 stars4/5Suicidal: Why We Kill Ourselves Rating: 4 out of 5 stars4/5The Wisdom of Psychopaths: What Saints, Spies, and Serial Killers Can Teach Us About Success Rating: 4 out of 5 stars4/5On Food and Cooking: The Science and Lore of the Kitchen Rating: 5 out of 5 stars5/5
Reviews for MATLAB for Beginners
0 ratings0 reviews
Book preview
MATLAB for Beginners - Peter Kattan
Preface
This book is written for people who wish to learn MATLAB¹ for the first time. The book is really designed for beginners and students. In addition, the book is suitable for students and researchers in various disciplines ranging from engineers and scientists to biologists and environmental scientists. The book is intended to be used as a first course in MATLAB in these areas. Students at both the undergraduate level and graduate level may benefit from this book. The material presented has been simplified to such a degree such that the book may also be used for students and teachers in high schools – at least for performing simple arithmetic and algebraic manipulations. One of the objectives of writing this book is to introduce MATLAB and its powerful and simple computational abilities to students in high schools.
The material presented increases gradually in difficulty from the first few chapters on simple arithmetic operations to the last two chapters on solving equations and an introduction to calculus. In particular, the material presented in this book culminates in Chapters 6 and 7 on vectors and matrices, respectively. There is no discussion of strings, character variables or logical operators in this book. The emphasis is on computational and symbolic aspects. In addition, the MATLAB Symbolic Math Toolbox² is emphasized in this book. A section is added at the end of each chapter (except chapters 1 and 9) dealing with various aspects of algebraic and symbolic computations with the Symbolic Math Toolbox. In fact, the final chapter on calculus assumes entirely the use of this toolbox.
Chapter 1 provides an overview of MATLAB and may be skipped upon a first reading of the book. The actual material in sequence starts in Chapter 2 which deals with arithmetic operations. Variables are introduced in Chapter 3 followed by mathematical functions in Chapter 4. The important topic of complex numbers is covered in Chapter 5. This is followed by Chapters 6 and 7 on vectors and matrices, respectively, which provide the main core of the book. In fact, MATLAB stands for MATrixLABoratory – thus a long chapter is devoted to matrices and matrix computations. An introduction to programming using MATLAB is provided in Chapter 8. Plotting two-dimensional and three-dimensional graphs is covered in some detail in Chapter 9. Finally, the last two chapters (10 and 11) present solving equations and an introduction to calculus using MATLAB.
The material presented in this book has been tested with version 7 of MATLAB and should work with any prior or later versions. There are also over 230 exercises at the ends of chapters for students to practice. Detailed solutions to all the exercises are provided in the second half of the book. The material presented is very easy and simple to understand – written in a gentle manner. An extensive references list is also provided at the end of the book with references to books and numerous web links for more information. This book will definitely get you started in MATLAB. The references provided will guide you to other resources where you can get more information.
I would like to thank my family members for their help and continued support without which this book would not have been possible. In this edition, I am providing two email addresses for my readers to contact me-pkattan@alumni.lsu.edu and pkattan@tedata.net.jo.
January 2024 Peter I. Kattan
¹ MATLAB is a registered trademark of the MathWorks, Inc.
² The MATLAB Symbolic Math Toolbox is a registered trademark of the MathWorks, Inc.
]>
1. Introduction
In this introductory chapter a short MATLAB tutorial is provided. This tutorial describes the basic MATLAB commands needed. More details about these commands will follow in subsequent chapters. Note that there are numerous free MATLAB tutorials on the internet – check references [1-28]. Also, you may want to consult many of the excellent books on the subject – check references [29-47]. This chapter may be skipped on a first reading of the book.
In this tutorial it is assumed that you have started MATLAB on your computer system successfully and that you are now ready to type the commands at the MATLAB prompt (which is denoted by double arrows >>
). For installing MATLAB on your computer system, check the web links provided at the end of the book.
Entering scalars and simple operations is easy as is shown in the examples below:
>> 2*3+5
ans =
11
The order of the operations will be discussed in subsequent chapters.
>> cos(60*pi/180)
ans =
0.5000
The argument for the cos command and the value of pi will be discussed in subsequent chapters.
We assign the value of 4 to the variable x as follows:
>> x = 4
x =
4
>> 3/sqrt(2+x)
ans =
1.2247
To suppress the output in MATLAB use a semicolon to end the command line as in the following examples. If the semicolon is not used then the output will be shown by MATLAB:
>> y = 30;
>> z = 8;
>> x = 2; y-z;
>> w = 4*y + 3*z
w =
144
MATLAB is case-sensitive, i.e. variables with lowercase letters are different than variables with uppercase letters. Consider the following examples using the variables x and X:
>> x = 1
x =
1
>> X = 2
X =
2
>> x
x =
1
>> X
X =
2
Use the help command to obtain help on any particular MATLAB command. The following example demonstrates the use of help to obtain help on the det command which calculated the determinant of a matrix:
>> help det
DET Determinant.
DET(X) is the determinant of the square matrix X.
Use COND instead of DET to test for matrix singularity.
See also cond.
Overloaded functions or methods (ones with the same name in other directories) help sym/det.m
Reference page in Help browser doc det
The following examples show how to enter matrices and perform some simple matrix operations:
>> x = [1 2 3 ; 4 5 6 ; 7 8 9]
x =
1 2 3
4 5 6
7 8 9
>> y = [1 ; 0 ;-4]
y =
1
0
-4
>> w = x*y
w =
-11
-20
-29
Let us now see how to use MATLAB to solve a system of simultaneous algebraic equations. Let us solve the following system of simultaneous algebraic equations:
Eq_9781257136919_0013_001.gif(1.1)
We will use Gaussian elimination to solve the above system of equations. This is performed in MATLAB by using the backslash operator "\" as follows:
>> A= [3 5-1 ; 0 4 2 ;-2 1 5]
A =
3 5-1
0 4 2
-2 1 5
>> b = [2 ; 1 ;-4]
b =
2
1
-4
>> x = A\b
x =
-1.7692
1.1154
-1.7308
It is clear that the solution is x1 =-1.7692, x2 = 1.1154, and x3 =-1.7308. Alternatively, one can use the inverse matrix of A to obtain the same solution directly as follows:
>> x = inv(A)*b
x =
-1.7692
1.1154
-1.7308
It should be noted that using the inverse method usually takes longer than using Gaussian elimination especially for large systems of equations.
Consider now the following 4 x 4 matrix D:
>> D = [1 2 3 4 ; 2 4 6 8 ; 3 6 9 12 ;-5-3
-1 0]
D =
1 2 3 4
2 4 6 8
3 6 9 12
-5-3-1 0
We can extract the sub-matrix in rows 2 to 4 and columns 1 to 3 as follows:
>> E = D(2:4, 1:3)
E =
2 4 6
3 6 9
-5-3-1
We can extract the third column of D as follows:
>> F = D(1:4, 3)
F =
3
6
9
-1
We can extract the second row of D as follows:
>> G = D(2, 1:4)
G =
2 4 6 8
We can also extract the element in row 3 and column 2 as follows:
>> H = D(3,2)
H =
6
We will now show how to produce a two-dimensional plot using MATLAB. In order to plot a graph of the function y = f (x), we use the MATLAB command plot(x,y) after we have adequately defined both vectors x and y. The following is a simple example to plot the function y = x² + 3 for a certain range:
>> x = [1 2 3 4 5 6 7 8 9 10 11]
x =
Columns 1 through 10
1 2 3 4 5 6 7
8 9 10
Column 11
11
>> y = x.^2 + 3
y =
Columns 1 through 10
4 7 12 19 28 39 52
67 84 103
Column 11
124
>> plot(x,y)
Figure 1.1 shows the plot obtained by MATLAB. It is usually shown in a separate window. In this figure no titles are given to the x-and y-axes. These titles may be easily added to the figure using the xlabel and ylabel commands. More details about these commands will be presented in the chapter on graphs.
In the calculation of the values of the vector y, notice the dot .
before the exponentiation symbol ^
. This dot is used to denote that the operation following it be performed element by element. More details about this issue will be discussed in subsequent chapters.
Figure 1.1: Using the MATLAB plot
command
Finally, we will show how to make magic squares with MATLAB. A magic square is a square grid of numbers where the total of any row, column, or diagonal is the same. Magic squares are produced by MATLAB using the command magic. Here is a simple example to produce a 5 x 5 magic square:
>> magic(5)
ans =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
It is clear that the total of each row, column or diagonal in the above matrix is 65.
Exercises
Solve all the exercises using MATLAB. All the needed MATLAB commands for these exercises were presented in this chapter.
1. Perform the operation 3*4+6. The order of the operations will be discussed in subsequent chapters.
2. Perform the operation cos(5). The value of 5 is in radians.
3.