Introduction to Computing using Matlab
sind(x
sine (xin degrees)
sin(x)
sine (xin radians)
sqrt(x)
square root
i
square root of (-1)
tand(x)
tangent (xin degrees)
tan(x)
tangent (xin radians)
eps
the smallest difference betweenany two numbers in MATLAB
ans
the value of the last expression that was not assigned to a variable
factorial(x)
x!
Comment lines
•Lines that start with percent sign (%) •Common for first few lines to be commentsand to briefly explain what commands in file do •Editor Window shows comment lines in green
floor(x)
round toward minus infinity
fix(x)
round toward zero
coth(x)
(e^x+e^-x)/(e^x-e^-x)
cosh(x)
(e^x+e^-x)/2
tanh(x)
(e^x-e^-x)/(e^x+e^-x)
sinh(x)
(e^x-e^-x)/2
Can read the variable
(get its value)
j
(same as i) but commonly used instead of i in electrical engineering
Can write to the variable
(set its value)
A function's arguments can be
-Numbers -Variables (explained in next section -Expressions involving numbers, variables, orfunctions
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
Right division
/
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.,speed_Of_Light (This is known as camel case!)
pi
3.14
To change the value of a variable, just assign it the new value
>> ABB=72; >> ABB=9; >> ABB ABB = 9
To change the value of a variable, just assign it the new value
>> ABB=72; >> ABB=9; >> ABBABB =9
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)+40-a/B*10 a = 12 C = 18
A variable must have a value before you use it in an expression
>> x = 3; >> x+2ans=5 >> x + y% assume y undefined??? Undefined function orvariable 'y'
To find out the value of a variable, just type it and press ENTER
>> x = 3; >> y = 10 * x; >> z = y ^ 2; >> yy =30 >> zz =900
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
Fourth
Addition and subtraction.
A keyword is a word that has special meaning to MATLAB
Appear in blue when typed in the Editor Window Can't be used as variable names
clc command
Clears Command Window display -Up and down arrows still bring back previous commands
Figure Window
Contains output from graphic commands.
Editor Window
Creates and debugs script and function files.
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
Second
Exponentiation.
Can type several commands in same line by putting a commabetween commands
Hard to read, so don't do this often
A function
Has a name -Can have zero or morearguments(inputs) -Can produce zero or more outputs
Command History Window
Logs commands entered in theCommand Window.
Command Window
Main window, enters variables,runs programs.
Third
Multiplication, division (equal precedence).
First
Parentheses. For nested parentheses, the innermost are executed first.
Help Window
Provides help information.
Workspace Window
Provides information about thevariables that are stored.
clear
Removes all variables from memory
Command History Window
Shows previous commands, including ones from previousMATLAB sessions -Double-clicking on command puts it in Command Window andexecutes it -Can drag command to Command Window, make changes incommand, then execute it -To clear one or more commands, select the lines to delete, rightclick, choose Delete Selection -To clear entire history, right click, select Clear Command History
Current Folder Window
Shows the files in the current folder.
Percent sign(%)
When typed at beginning of line, MATLAB treats line as acommentand doesn't execute line
Semicolon (;)
When typed at end of command, suppresses output. (Only prompt displayed at next line) -Useful for preventing display of large outputs
Left division
\
abs(x)
absolute value
(xin radians)
asin(x),acos(x),atan(x),acot(x)
(xin degrees)
asind(x),acosd(x),atand(x),acotd(x)
MATLAB is
case-sensitive
cosd(x)
cosine (xin degrees)
cos(x)
cosine (xin radians)
cotd(x)
cotangent (xin degrees)
cot(x)
cotangent (xin radians)
exp(x)
e^x
inf or Inf
infinity
log10(x)
log base 10
NaN
nan not-a-number. Used to express mathematically undefined values, such as 0/0
log(x)
natural log (base e)
nthroot(x,n)
nth real root
rem(x,y)
remainder after x is divided by y (also called modulus)
clear x y z
removes only variables x, y, and z from memory
sign(x)
returns the sign of input x as -1, 0, or 1 (negative, zero, or positive respectively)
round(x)
round to nearest integer
ceil(x)
round toward infinity
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-infunction as the name of a variable, e.g.,don't call a variableexporsqrt
NOTES ABOUT SCRIPT FILES
•Using a script file is convenient because it canbe edited (corrected and/or changed) andexecuted many times •Script files can be typed and edited in any texteditor and then pasted into the MATLAB editor •Script files are also calledM-filesbecause the extension.m is used when they are saved