Matlab Introduction and Basics Guide.pdf

JesseGrant9 16 views 38 slides Mar 12, 2025
Slide 1
Slide 1 of 38
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38

About This Presentation

Matlab Guide


Slide Content

MATH 1194
Introduction to Mathematical Software III
- A Primer on MATLAB
Lecturer: Mr. Jagdesh Ramnanan
Department of Mathematics and Statistics
UWI, St. Augustine
Semester II - 2024/2025




1 - Introduction & MATLAB
Basics
Lecturer: Mr. Jagdesh Ramnanan 1 / 38

Course Information
Lecturer:
Mr. Jagdesh Ramnanan
Email:
[email protected]
Office Hours:
Monday 12-1pm & Tuesday 1-2pm
Lecturer: Mr. Jagdesh Ramnanan 2 / 38

Course Overview
The purpose of this course is to introduce students to the
MATLAB software and programming language.
This course is worth 1 credit.
The assessment is 100% Coursework, comprising of:
FourMCQ Quizzes (20%)
TwoCoursework Examinations (80%)
Lecturer: Mr. Jagdesh Ramnanan 3 / 38

Course Calendar
Wk.Date Lecture Topic Assess.
1 20 Jan Intro & Basics -
2 27 Jan Arrays -
3 3 FebData classes; Input/OutputQuiz 1
410 Feb 2D Plots Quiz 2
517 Feb 3D Plots Quiz 3
624 Feb - CW 1
7 3 Mar - -
810 Mar Intro to Programming -
917 Mar Control Structures -
1024 Mar More on functionsQuiz 4
1131 Mar - -
127 Apr - CW 2
Lecturer: Mr. Jagdesh Ramnanan 4 / 38

Important Things to Remember
Send emails to the correct email address:
[email protected]
Keep on top of your work and make use of MHC and
office hours
Ask questions
Practise using MATLAB
Attempt all assessments fully
Avoid basic algebraic mistakes
Always use radians, unless told otherwise
Lecturer: Mr. Jagdesh Ramnanan 5 / 38

What is MATLAB?
MATLABstands forMatrixLaboratory
It is a high-performance programming language and
computing program developed by MathWorks
MATLAB is most often used for:
Computation
Plotting functions and data
Developing and implementing algorithms
Data analysis, exploration and visualisation
Modeling and simulation of scientific and engineering
systems
Application development and graphical user interface
creation
https://www.mathworks.com/discovery/what-is-matlab.html
Lecturer: Mr. Jagdesh Ramnanan 6 / 38

MATLAB Access
The latest version available isMATLAB R2024b
Follow the instructions given in the document
“MATLAB Installation Manual” to download and
install a free student copy of MATLAB
Install the following packages (only):
MATLAB
Symbolic Math Toolbox
Alternatively, you may use the online browser version of
the software:https://matlab.mathworks.com
Lecturer: Mr. Jagdesh Ramnanan 7 / 38

MATLAB Windows
Command Window: This is where you type commands.
Workspace: All variables currently stored are displayed
here.
Command History(accessed through the Layout tab in
the Home menu): Stores all commands entered during
the current session as well as previous sessions.
Current Folder: Lists files stored in the current directory.
Graphics Window: Displays plots/graphs.
Editor Window: Used to create/edit MATLAB codes.
Lecturer: Mr. Jagdesh Ramnanan 8 / 38

MATLAB Data Classes
https://www.mathworks.com/help/matlab/matlab_prog/fundamental-matlab-
classes.html
Lecturer: Mr. Jagdesh Ramnanan 9 / 38

MATLAB as a Calculator
Arithmetic Operations
Addition +
Subtration -
Multiplication*.*
Division /./
Exponentiation^.^
Unless otherwise specified, MATLAB assigns computed
values to a variable calledans
Variables can be assigned values using=
“.*”, “./” and “.^” are element-wise operations. These
will be addressed in Lecture 2.
Lecturer: Mr. Jagdesh Ramnanan 10 / 38

Variables
A variable name may contain only letters, digits, and
underscores, andmustbeginwithaletter.
Punctuation marks arenotallowed!
Note: MATLAB is case-sensitive!
A variable can be assigned:
a numerical value,
an array of numerical values,
a string of characters,
other types of data.
Lecturer: Mr. Jagdesh Ramnanan 11 / 38

More on Variable Names
A variable cannot be given the same name as a MATLAB
keyword.
The command> >iskeywordgives a list of all the
MATLAB keywords.
To avoid potential conflicts, a variable should not be
given any name that already exists in MATLAB.
Theexistcommand can be used to check this.
Lecturer: Mr. Jagdesh Ramnanan 12 / 38

Built-In Constants and Functions
Name MATLAB syntax Remarks
?????? pi
∞ Inf, inf Infinity
NaN Not a number
??????or

−1 i, j Imaginary number
10
??????
1ex Powers of 10

?????? sqrt(x) Square root function
??????
??????
exp(x) Exponential function
log(??????) log10(x) log base 10
ln(??????) log(x) log base??????
sin(??????)
sin(x) Measured in radians
sind(x) Measured in degrees
arcsin(??????),sin
−1
(??????)asin(x) Inverse trig functions
sinh(??????) sinh(x) Hyperbolic functions
Lecturer: Mr. Jagdesh Ramnanan 13 / 38

MATLAB as a Calculator - Examples
Enter each of the following commands in the Command
Window:
> >1 + 2
> >pi^2-sqrt(ans)+cos(45)/cosh(45)
> >x=exp(3)*log10(3)+atan(3)/log(3)
> >y=20+log(csch(x-1)^2)
> >z=1+y/x
> >z*3e-2
Lecturer: Mr. Jagdesh Ramnanan 14 / 38

Exercise 1
Use MATLAB to perform the following calculations,
saving the answers in the specified variables (if any):
2 −

3
4 + 5sin(6)
?????? =
4 − 3cos(10
9
)
1 +

??????
2
− 4
+
3
2??????
?????? =
csc(sech(1))
csch(sec(1))

1 +√8 + 3tan
4
(2?????? − 1)
??????

3
−tan
−1
(1 +ln(??????))
Lecturer: Mr. Jagdesh Ramnanan 15 / 38

Formatting the Command Window Display
MATLAB uses double-precision floating point arithmetic -
values are stored accurate to 15 decimal places.
However, MATLAB displays only 4 decimal places by
default.
To display 15 decimal places, use> >formatlong
To display only 4 decimal places, use> >formatshort
(default)
To display numbers as fractions, use> >formatrat
By default, MATLAB displays a blank line between each
line of text in the Command Window.
To disable this feature, use> >formatcompact
To enable it, use> >formatloose
Lecturer: Mr. Jagdesh Ramnanan 16 / 38

Useful Commands and Tools
> >clearx- deletes only the variablex
> >clearx y- deletes only variablesxandy
> >clear- deletes all current variables in the workspace
> >clc- clears the Command Window
> >quit- ends the session and closes MATLAB
Hold downControl+Con the keyboard - this interrupts
the current command execution, which is often useful for
aborting commands that are taking too long to complete.
While the Command Window is active, use theUpand
Downarrow keys on the keyboard to access commands
stored in the Command History.
Lecturer: Mr. Jagdesh Ramnanan 17 / 38

Useful Commands and tools
A semicolon (;) is used to suppress the output - the
output is still stored in the Workspace, but it is not
displayed in the Command Window.
An ellipsis (...) is used to enter input on an additional line
- this is useful when typing long lines of MATLAB code.
%is used for commenting. MATLAB does not run any
commands in a line following a%- this is useful for
documenting and explaining what your code does.
> >helpxyz- provides help on topicxyz
Note:Online support for MATLAB can be found at:
www.mathworks.com/help/matlab/
Lecturer: Mr. Jagdesh Ramnanan 18 / 38

Using the diary Command to Record a Session
> >diarymath1194.txt- initialises a diary that stores
all subsequent Command Window input and the
computed output in the text file “math1194.txt”
> >diaryoff- ends recording
> >diaryon- begins recording
Diary files can be opened/edited using any standard text
editor
Alternatively, one may copy and paste the relevant parts
of the Command Window to a text file
Lecturer: Mr. Jagdesh Ramnanan 19 / 38

Example
Enter the following commands in the Command Window:
> >diaryday1.txt
> >x=1+1/3
> >diaryoff
> >formatrat
> >y=x-1
> >diaryon
> >z=x/4
> >diaryoff
Lecturer: Mr. Jagdesh Ramnanan 20 / 38

Exercise 2
Use MATLAB to perform the following calculations (in
the order given):
?????? =ln(1 +
1
2
√2??????
??????−2
− 3sin(??????
2
))
?????? =cos
−1
(4 − 3csc(??????))
?????? = 3tanh(?????? − ??????) + 10
9
tan(10
−10
)
?????? =csc
−1
(coth(10
−3
)) +
4 −√9 + 2sin
4
(
23
37
??????)
??????
tan(1)
+cosh(1)
Lecturer: Mr. Jagdesh Ramnanan 21 / 38

Exercise 3
Create a diary file calledexercise3.txtand use it to
document the following:
Use thehelpcommand to obtain information on the
formatcommand.
Enter a command in the Command Window to remove
extra blank lines in the display.
Enter a command in the Command Window to show
numbers as fractions.
Given that
?????? =
1 + 3sinh(2)ln(2)
?????? − 1
and?????? =√4cos
2
(??????) + 6??????
??????−3
,
use appropriate MATLAB commands to find the value
of??????, with the answer displayed to 15 decimal places.
Suppress the value of??????in the Command Window
display.
Lecturer: Mr. Jagdesh Ramnanan 22 / 38

'
&
$
%
List of Some Basic
MATLAB Commands
Lecturer: Mr. Jagdesh Ramnanan 23 / 38

Some Basic MATLAB Commands
BASICS
clear- Clears variables stored in the Workspace
clc- Clears the Command Window display
diary- Used to start/end a diary file
format- Used to format the Command Window display
help- Used to obtain documentation on MATLAB
commands
quit- Ends the session and closes MATLAB
Lecturer: Mr. Jagdesh Ramnanan 24 / 38

Some Basic MATLAB Commands
TRIGONOMETRICFUNCTIONS(RADIANS)
acsc- Inverse cosecant function
acos- Inverse cosine function
acot- Inverse cotangent function
asec- Inverse secant function
asin- Inverse sine function
atan- Inverse tangent function
csc- Cosecant function
cos- Cosine function
cot- Cotangent function
sec- Secant function
sin- Sine function
tan- Tangent function
Lecturer: Mr. Jagdesh Ramnanan 25 / 38

Some Basic MATLAB Commands
TRIGONOMETRICFUNCTIONS(DEGREES)
acscd- Inverse cosecant function (in degrees)
acosd- Inverse cosine function (in degrees)
acotd- Inverse cotangent function (in degrees)
asecd- Inverse secant function (in degrees)
asind- Inverse sine function (in degrees)
atand- Inverse tangent function (in degrees)
cscd- Cosecant function (in degrees)
cosd- Cosine function (in degrees)
cotd- Cotangent function (in degrees)
secd- Secant function (in degrees)
sind- Sine function (in degrees)
tand- Tangent function (in degrees)
Lecturer: Mr. Jagdesh Ramnanan 26 / 38

Some Basic MATLAB Commands
HYPERBOLICFUNCTIONS
csch- Hyperbolic cosecant function
cosh- Hyperbolic cosine function
coth- Hyperbolic cotangent function
sech- Hyperbolic secant function
sinh- Hyperbolic sine function
tanh- Hyperbolic tangent function
Lecturer: Mr. Jagdesh Ramnanan 27 / 38

Some Basic MATLAB Commands
EXPONENTIALANDLOGARITHMICFUNCTIONS
exp- Exponential function (??????
??????
)
log- Natural log (base??????)
log10- Log base10
log2- Log base2
Lecturer: Mr. Jagdesh Ramnanan 28 / 38

Some Basic MATLAB Commands
REALANDCOMPLEXNUMBERS
abs- Absolute value or complex magnitude
conj- Complex conjugate
imag- Imaginary part of a complex number
nthroot-??????th root
real- Real part of a complex number
round- Rounding off
sqrt- Square root
Lecturer: Mr. Jagdesh Ramnanan 29 / 38

Some Basic MATLAB Commands
INTEGERS
ceil- Ceiling function
factor- Factors of a number
factorial- Factorial of a number
floor- Floor function
isprime- Checks if number is prime
gcd- Greatest common divisor
lcm- Least common multiple
mod- Modulus after division
primes- Prime numbers
rem- Remainder after division
Lecturer: Mr. Jagdesh Ramnanan 30 / 38

Some Basic MATLAB Commands
RANDOMNUMBERS
rand- Generates random numbers in the interval(0, 1)
randi- Generates random integers
randperm- Generates a random permutation of integers
rng(’shuffle’)- Randomises the stream of numbers used
byrand,randiandrandperm
Lecturer: Mr. Jagdesh Ramnanan 31 / 38

Some Basic MATLAB Commands
ARRAYOPERATIONS
cross- Cross product
dot- Dot product
eye- Creates an array with ones in the leading diagonal
and zeros elsewhere
length- The larger dimension of an array
linspace- Creates a row vector of equidistant points
logspace- Creates a row vector of logarithmically-spaced
points
ones- Creates an array of ones
numel- The number of elements in an array
size- The two dimensions of an array
zeros- Creates an array of zeros
Lecturer: Mr. Jagdesh Ramnanan 32 / 38

Some Basic MATLAB Commands
ARRAYOPERATIONS(continued)
magic- Magic square
max- Maximum element
maxk-??????largest elements
mean- Average of the elements
median- Median of the elements
min- Minimum element
mink-??????smallest elements
mode- Most frequently occurring element
prod- Product of elements
sort- Sorts the elements of an array
std- Standard deviation of the elements
sum- Sum of the elements
Lecturer: Mr. Jagdesh Ramnanan 33 / 38

Some Basic MATLAB Commands
LINEARALGEBRA
adjoint- Adjoint of a square matrix
det- Determinant of a square matrix
eig- Eigenvalues and eigenvectors of a matrix
inv- Inverse of a square matrix
null- Null space of a matrix
rank- Rank of a matrix
rref- Reduced row echelon form of a matrix
trace- Sum of leading diagonal elements of a matrix
Lecturer: Mr. Jagdesh Ramnanan 34 / 38

Some Basic MATLAB Commands
PLOTS
axis- Used to formal the axes of a plot
bar- Creates a bar chart
barh- Creates a horizontal bar chart
bar3- Creates a 3D bar chart
colorbar- Adds a colorbar to a plot
contour- Creates a contour plot
figure- Opens a graphics window
grid- Used to turn grid lines on/off
histogram- Creates a histogram plot
hold- Used to freeze/unfreeze a plot
legend- Adds legend to a plot
mesh- Creates a mesh plot
Lecturer: Mr. Jagdesh Ramnanan 35 / 38

Some Basic MATLAB Commands
PLOTS(Continued)
meshgrid- Creates a grid of points
pie- Creates a pie chart
pie3- Creates a 3D pie chart
plot- Creates a 2D plot
plot3- Creates a 3D plot
subplot- Creates subplots within a graphics window
surf- Creates a surface plot
text- Used to add text to a plot
title- Adds a title to a plot
xlabel- Adds an??????-axis label to a plot
ylabel- Adds a??????-axis label to a plot
zlabel- Adds a??????-axis label to a plot
Lecturer: Mr. Jagdesh Ramnanan 36 / 38

Some Basic MATLAB Commands
OTHERCOMMANDS
break- Terminates a loop
char- Creates a character array
continue- Ends the current iteration of a loop and goes
to the next iteration
disp- Displays text in the Command Window
exist- Checks if a name exists in MATLAB
feval- Used to evaluate a function
find- Find indices and values of elements of an array
fprintf- Displays text in the Command Window
global- Defines global variables
Lecturer: Mr. Jagdesh Ramnanan 37 / 38

Some Basic MATLAB Commands
OTHERCOMMANDS(continued)
input- Displays a prompt and saves the value entered
isequal- Checks if values are equal
iskeyword- Gives a list of all MATLAB keywords
lookfor- Displays the first comment line of a MATLAB
function
return- Terminates a function or script
what- Lists MATLAB files in the current directory
who- Lists current variables
whos- Lists current variables (long display)
Lecturer: Mr. Jagdesh Ramnanan 38 / 38
Tags