0% found this document useful (0 votes)
20 views47 pages

ch1 - Starting With MATLAB - Tagged

Chapter 1 introduces MATLAB, detailing its various windows and the command window's functionality for arithmetic operations and variable management. It covers the use of script files for executing multiple commands efficiently and provides guidelines for defining variables and using built-in functions. Additionally, it explains the importance of proper variable naming and the management of variables within MATLAB.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views47 pages

ch1 - Starting With MATLAB - Tagged

Chapter 1 introduces MATLAB, detailing its various windows and the command window's functionality for arithmetic operations and variable management. It covers the use of script files for executing multiple commands efficiently and provides guidelines for defining variables and using built-in functions. Additionally, it explains the importance of proper variable naming and the management of variables within MATLAB.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Chapter 1

Starting with MATLAB


Conten
ts
This chapter describes:
 MATLAB windows
 Command Window in detail
 How to do basic arithmetic
 Create basic variables
 Introductory script files
1.1 STARTING MATLAB, MATLAB
WINDOWS
Default layout of desktop window
Current Folder
Window

Toolstrip
Details Window

Workspace Window Command Window


1.1 STARTING MATLAB, MATLAB
WINDOWS

Window Purpose
Command Window Main window, enters variables,
runs programs.
Figure Window Contains output from graphic
commands.
Editor Window Creates and debugs script and
function files.
Help Window Provides help information.
Command History Window Logs commands entered in the
Command Window.
Workspace Window Provides information about the
variables that are stored.
Current Folder Window Shows the files in the current
folder.
1.2 WORKING IN THE COMMAND WINDOW

Semicolon (;)
 When typed at end of command, suppresses output.
(Only prompt displayed at next line)
 Useful for preventing display of large outputs
 Used much more in scripts (see Section 1.8)

Percent sign(%)
 When typed at beginning of line, MATLAB treats line as
a comment and doesn’t execute line
 Used much more in scripts (see Section 1.8)
1.2 WORKING IN THE COMMAND WINDOW

clc command
 Clears Command Window display
 Up and down arrows still bring back previous
commands
1.3 ARITHMETIC OPERATIONS WITH SCALARS

Symbols for arithmetic are:


Operation Symbol Example
Addition + 5+3
Subtraction – 5–3
Multiplication * 5*3
Right division / 5/3
Left division \ 5\3=3/5
Exponentiation ^ 5 ^ 3 (means 53 =
125)

Left division rarely used with scalars


1.3.1 Order of Precedence

Order in which MATLAB does arithmetic

Precedence Mathematical Operation

First Parentheses. For nested parentheses, the


innermost are executed first.
Second Exponentiation.
Third Multiplication, division (equal precedence).
Fourth Addition and subtraction.
1.3.1 Order of Precedence

Precedence order
 Same as most calculators
 Same as doing arithmetic by hand
 For multiple operations of same precedence, MATLAB
goes left to right
 Can change order by using parentheses
1.3.2 Using MATLAB as a Calculator

Can use MATLAB as a (very expensive!) calculator


1. Type in mathematical expression
2. Press Enter key
3. MATLAB displays answer in Command Window as ans
= followed by the result
Your display may appear on more than one line and have blank
lines between text
1.4 DISPLAY
FORMATS
Can control display of numbers with format command
 Once enter command, format stays the same until
another format command
 Default format is fixed point with four digits to right of
decimal point
 fixed-point means decimal point always between
one’s-digit and one-tenth’s digit
 Format only affects display of numbers. MATLAB always
computes and saves numbers in full precision
1.4 DISPLAY FORMATS

Some types of
formatting
1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS

MATLAB expressions can include functions. You can think


of a function as a black box that, in general, takes inputs,
does some computations with them, and produces outputs.

function x
function tan(y/x)
Inputs Outputs y
1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS

A function
 Has a name
 Can have zero or more arguments (inputs)
 Can produce zero or more outputs

y = sqrt( x )

output name argument


1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS

A function’s arguments can be


 Numbers
 Variables (explained in next section
 Expressions involving numbers, variables, or functions
sqrt(64) Argument is a number

sqrt(a) Argument is the variable “a”

Argument to arctan function is an expression


atan( y/sqrt(3^2+y^2) ) that has a number (3), a variable (y), and
a function (sqrt)
1.5 ELEMENTARY MATH BUILT-IN
FUNCTIONS
Elementary math functions
 sqrt(x) – square root
 nthroot(x,n) – nth real root
 exp(x) – ex
 abs(x) – absolute value
 log(x) – natural log (base e)
 log10(x) – log base 10
 factorial(x) – x!
See Table 1-3 in book for details
1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS
Trigonometric functions
 sin(x) – sine (x in radians)
 sind(x) – sine (x in degrees)
 cos(x) – cosine (x in radians)
 cosd(x) – cosine (x in degrees)
 tan(x) – tangent (x in radians)
 tand(x) – tangent (x in degrees)
 cot(x) – cotangent (x in radians)
 cotd(x)- cotangent (x in degrees)
See Table 1-4 in book for details
1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS
Inverse trigonometric functions
 asin(x), acos(x), atan(x), acot(x)
(x in radians)
 asind(x), acosd(x), atand(x), acotd(x) (x
in degrees)
Hyperbolic trigonometric functions
 cosh(x) –
 sinh(x) -
 tanh(x) -
 coth(x)-
1.5 ELEMENTARY MATH BUILT-IN FUNCTIONS
Rounding functions
 round(x) – round to nearest integer
 fix(x) – round toward zero
 ceil(x) – round toward infinity
 floor(x) – round toward minus infinity
 rem(x,y) – remainder after x is divided by y (also
called modulus)
 sign(x) – returns 1 if x is positive,
-1 if x is negative, zero if x is zero
See Table 1-5 in book for details
1.6 DEFINING SCALAR VARIABLES

A variable is a name that is assigned a numerical


value
 Once assigned, can use variable in expressions,
functions, and MATLAB statements and commands
 Can read the variable (get its value)
 Can write to the variable (set its value)
1.6.1 The Assignment Operator

= (equals sign) is MATLAB’s assignment operator. It


evaluates the expression on its right side and stores the
resulting value in the variable on its left side
>> a = 3 Create the variable called “a” and store the value 3 in it
a =
3 MATLAB acknowledges that it has created “a” and set it to
3
1.6.1 The Assignment
Operator
EXAMPLE
>> a = 3; Make a variable and store a number in it
>> b = 10*a + 5
b=
35 Make a variable and store the value of an
expression made up of a variable,
numbers, and addition and multiplication
1.6.1 The Assignment Operator

Think of = as meaning “assign to” or “store in” but


not meaning “equals”!
Why?
x = x + 6 has no meaning in math because it implies
that 0 = 6
x = x + 6 is perfectly fine in MATLAB because it
means “take whatever is in x, add 6 to that and store
the result back into x”
1.6.1 The Assignment Operator

EXAMPLE
>> x = 3; ; at end prevents MATLAB from displaying value of x

>> x = x + 6 takes what’s in x (3), adds 6 to it to get 9, then stores


9 back into x
x =
9 now x’s value is 9

>> x = 2 * x takes what’s in x (9), multiplies it by 2 to get 18, then


stores 18 back into x
x =
18 now x’s value is 18
1.6.1 The Assignment Operator

A variable must have a value before you use it in an


expression
>> x = 3;
>> x+2
ans =
5
>> x + y % assume y undefined
??? Undefined function or variable 'y'
1.6.1 The Assignment Operator
To find out the value of a variable, just type it and press ENTER
>> x = 3; x input
>> y = 10 * x; y output, x input
>> z = y ^ 2; y input, z output

>> y
y =
30
>> z
z =
900
1.6.1 The Assignment Operator
Can do multiple assignments on one line by separating
with a comma or semicolon. If semicolon, no display for
that assignment

>> a=12, B=4 , C=(a-B)/2+40-a/B*10


a =12:
B=4;

C =(a-B)/2+40-a/B*10
1.6.1 The Assignment Operator

To change the value of a variable, just assign it the


new value
>> ABB=72;
>> ABB=9;
>> ABB
ABB =
9
1.6.1 The Assignment Operator

You must define a variable (give it a value) before you can


use it in an argument of a function
>> sqrt( x ) % assume x undefined
??? Undefined function or variable 'x'
>> x = 144;
>> sqrt( x )
x =
12
1.6.2 Rules About Variable Names

A variable name
 Must begin with a letter
 Can be up to 63 characters long
 Can contain letters, digits, and underscores ( _ )
 Can’t contain punctuation, e.g., period, comma,
semicolon
Avoid using the name of a built-in function as the name
of a variable, e.g., don’t call a variable exp or sqrt
1.6.2 Rules About Variable Names

MATLAB is case-sensitive, and does not consider an


upper-case letter in a variable name to be the same
as its lower-case counterpart, e.g., MTV, MTv, mTV,
and mtv are four different variable names

A variable name cannot contain a space. Two common


alternatives:
1. Use an underscore in place of a space, e.g.,
speed_of_light
2. Capitalize the first letter of all but first word, e.g.,
speedOfLight
(This is known as camel case!)
1.6.3 Predefined Variables and Keywords

A keyword is a word that has special meaning to


MATLAB
 There are 20 keywords
 break case catch classdef
continue else elseif end for
function global if otherwise
parfor persistent return spmd
switch try while
 Appear in blue when typed in the Editor
Window
 Can’t be used as variable names
1.6.3 Predefined Variables and Keywords

MATLAB has pre-defined variables for some common


quantities
pi the number
eps the smallest difference between any two
numbers in MATLAB
inf or Inf infinity
i
j (same as i) but commonly used instead of i in
electrical engineering
1.6.3 Predefined Variables and Keywords

More pre-defined variables


ans the value of the last expression that was not
assigned to a variable
NaN or nan not-a-number. Used to express
mathematically undefined values, such as
0/0
You can redefine (change) the values of the predefined
variables, but don’t. You’ll cause confusion
 Exceptions are i and j, which are often used as
loop variables (see Section 6.4)
1.7 USEFUL COMMANDS FOR MANAGING
VARIABLES
Some commands for managing variables
Command Outcome
clear Removes all variables from memory
clear x y z Removes only variables x, y, and z from
memory
who Displays a list of the variables currently in
memory
whos Displays a list of the variables currently in
memory and their size, together with
information about their bytes and class
(see Section 4.1)
1.8 SCRIPT FILES

So far, have run MATLAB commands by typing in single


command, pressing ENTER, getting MATLAB’s result, and
then repeating this process for next command
 Not practical for calculations involving more than a few
commands. Can use up and down arrow keys to avoid
lots of typing, but still not practical
Better way
 Save all commands in a file
 With one command in Command Window,
tell MATLAB to run all commands in file
Will use script files to do this
1.8.1 Notes About Script Files
A script file is a sequence of MATLAB commands, also called
a program
 When a script file runs (is executed), MATLAB performs
the commands in the order they are written, just as if they
were typed in the Command Window
 When a script file has a command that generates an
output (e.g. assignment of a value to a variable without
semicolon at the end), the file displays the output in the
Command Window
 Using a script file is convenient because it can be edited
(corrected and/or changed) and executed many times
 Script files can be typed and edited in any text editor and
then pasted into the MATLAB editor
 Script files are also called M-files because the
extension .m is used when they are saved
1.8.2 Creating and Saving a Script File
Use the Editor Window to work with script files
Can open window and create file two ways
1. Click on New Script icon
2. Click on New icon, select Script
3. In the Command Window, type edit and then press
ENTER
1.8.2 Creating and Saving a Script File

Editor has tool strip on top with three tabs – EDITOR,


PUBLISH, VIEW
 MATLAB used most often with EDITOR tab selected
1.8.2 Creating and Saving a Script File

 Type in commands line by line, pressing ENTER after


each one
 MATLAB automatically numbers lines
1.8.2 Creating and Saving a Script File

Comment lines
 Lines that start with percent sign (%)
 Common for first few lines to be comments and to briefly
explain what commands in file do
 Editor Window shows comment lines in green
1.8.2 Creating and Saving a Script File
Before MATLAB can run commands in file, you must save
file
 If you haven’t named file yet, click on Save icon,
MATLAB brings up Save As dialog box
 If you’ve already named and saved file, just click on
Save icon
 If you don’t add an extension (.xxx) to the file name,
MATLAB adds “.m”
 Rules for file names are same as rules for function
names
 Don’t use names of your variables, predefined
variables, MATLAB commands, or MATLAB functions
1.8.3 Running (Executing) a Script File
To execute a script file means to run all of the commands
in it. You can execute a file by
 Pressing the Run icon (a green arrow)
 Typing the file name in the Command Window and
pressing ENTER
MATLAB will execute file if it is in MATLAB’s current
folder or if the file’s folder is in the search path (explained
next)
1.8.4 Current Folder

The current folder is the folder that MATLAB checks first


when looking for your script file
 Can see current folder in desktop toolbar
 Can also display current folder by issuing MATLAB
command pwd
1.8.4 Current Folder

If you try to run the script by clicking on the Run icon


and the current folder is not the folder where the script
file is saved, you’ll get the prompt shown.
 Either change the current folder or add the script
folder to the MATLAB path
1.8.4 Current Folder

Can change current folder in Current Folder Window


 To show Current Folder Window, click on Layout icon
in desktop, then select
Current Folder
1.8.4 Current Folder
Can change current folder from command line using
cd command, space, new folder name in single
quote marks, ENTER, i.e.,
>> cd 'new folder'
For example,

You might also like