release numbers matlab is updated regularly the mathworks packages their software in groups, called...
TRANSCRIPT
![Page 1: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/1.jpg)
Release Numbers
• MATLAB is updated regularly• The Mathworks packages their software in
groups, called releases, New releases are issued twice a year in the spring and in the fallRelease 2013a includes– MATLAB 8.1– A number of specialized “toolboxes”
![Page 2: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/2.jpg)
MATLAB has two different methods for executing commands: interactive mode and batch mode.
In interactive mode, commands are typed (or cut and pasted) into the command window.
In batch mode, a series of commands is saved in a text file with a .m extension. The batch commands in a file are then executed by typing the name of the file at the MATLAB command prompt.
What Is MATLAB?
![Page 3: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/3.jpg)
MATLAB is an interpreted language. Commands are executed line by line.
It supports two basic types: characters like 'a' (16 bits) and doubles like 8.25 (64 bits).
Most of the time MATLAB works with arrays or matrices of real numbers or characters.
Other types are also supported: complex, symbolic (if installed), 16-bit and 8-bit integers, etc...
The MATLAB Language
![Page 4: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/4.jpg)
Problem Solving• A standardized approach to problem solving commonly
found in all science disciplines.• 1.first you state the problem, • 2.then describe the input and output.• 3. Develop a plan for solving the problem, called an
algorithm. • 4.Then you actually solve the problem, in our case using
MATLAB, and • 5. finally you test your solution for reasonableness.
If you use a consistent problem solving strategy you increase the chance that your result is correct
![Page 5: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/5.jpg)
MATLAB
![Page 6: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/6.jpg)
• MATLAB uses a standard windows menu bar
• To exit MATLAB use the close icon
![Page 7: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/7.jpg)
Command Window
Enter commands at the prompt
MATLAB WindowsCommand History Window Records all commands issued in the command window – including mistakes
Current Folder WindowLists files stored in the current directory
Workspace Window
![Page 8: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/8.jpg)
The MATLAB DesktopCu
rren
t Fol
der W
indo
w
Command Window
Workspace Window
Command History Window
![Page 9: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/9.jpg)
Let’s look at the windows one at a time
![Page 10: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/10.jpg)
At the prompt (>>), type in a MATLAB command. Press the ENTER key.
MATLAB displays the result in the command window, followed by a prompt.
Repeat the process.
The Command Window
![Page 11: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/11.jpg)
To start a command, make sure cursor is next to the prompt.
MATLAB won’t respond until you press ENTER. It then executes only the last command.
Commands before the last one may still be visible, but MATLAB doesn’t execute them.
Notes on the Command Window
![Page 12: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/12.jpg)
You can type several commands in the same line by putting a comma between commands. (makes the program harder to read though, so it is not recommended).
If a command is too long to fit on one line, you can continue to the next line by typing ellipsis (3 periods, i.e., … ) and then pressing ENTER.
Notes on the Command Window
![Page 13: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/13.jpg)
When cursor is in the bottom (current) command line:
key moves the cursor one character to the left.
key moves the cursor one character to the right.
key recalls the preceding command
key recalls a previous command if it follows.
Navigating the Command Window
![Page 14: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/14.jpg)
Command Window
• Similar to a scratch pad• Once you hit enter, you can’t edit any
commands• You can retype them or use the arrow keys
to retrieve commands and edit them before hitting enter again
Command Window
![Page 15: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/15.jpg)
The PAGE-UP key moves up to previous commands in a window-size at a time.
The PAGE-DOWN key moves down to previous commands in a window-size at a time.
The BACKSPACE (Delete on Mac) key deletes one character to the left of the cursor.
The DELETE (fn-Delete on Mac) key deletes one character to the right of the cursor.
Navigating the Command Window
![Page 16: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/16.jpg)
To quickly execute a previous command but with small changes:
Recall the command with the up and down arrow keys.
Use the left and right arrow keys to move to characters to be altered.
Use BACKSPACE (Mac: Delete) or DELETE (Mac: fn-Delete) to remove old characters, then type new characters.
Press ENTER to execute the modified command.
T I P
![Page 17: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/17.jpg)
Semicolon (;)
When typed at end of a command, it suppresses the output. (only the prompt is displayed at the next line)
Useful for preventing display of large outputs
Used much more in scripts (see Section 1.8 of text book)
Percent sign (%)
When typed at beginning of a line, MATLAB treats the line as a comment and doesn’t execute the line.
Also used much more in scripts.
Command Syntax
![Page 18: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/18.jpg)
Clears the command window display.
The up and down arrows still bring back the previous commands.
The clc Command
![Page 19: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/19.jpg)
Shows the previous commands, including the ones from previous MATLAB sessions.
Double-clicking on a command puts it in the command window and executes it.
You can drag a command to the command window, make changes in it, then execute it.
The Command History Window
![Page 20: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/20.jpg)
Command History
• Records the commands you issue in the command window
• When you exit the command window, or when you issue the clc command, the command window is cleared
• But the command history remains
Command History
![Page 21: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/21.jpg)
Command History
• You can transfer commands from the command history to the command window– Double click on a command
• It executes immediately– Click and drag into the command window
• You can edit the command before executing
![Page 22: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/22.jpg)
Workspace Window
Workspace Window
![Page 23: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/23.jpg)
NOTE!• When you define variables in the command window, they are
listed in the workspace window• Document Window:If you double click on any variable in the workspace window
MATLAB launches a document window containing the array editor
You can edit variables in the array editor• Figure Window:When Figures are created a new window opensIt’s extremely easy to create graphs in MATLAB
![Page 24: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/24.jpg)
The Figure Window
The Figure Window opens automatically after any command that draws a graph.
Figure Window
![Page 25: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/25.jpg)
Scalar
Vector
2-D Matrix
![Page 26: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/26.jpg)
Current Directory/Folder
• The current folder window is a list of files• When you try to load information from a file
or try to save information – MATLAB uses the current folder
![Page 27: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/27.jpg)
The Document Window displays the variable editor
New Variable Icon
![Page 28: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/28.jpg)
Note: The semicolon suppresses the output from each command
First create a vector of x values – then a corresponding vector of y values
![Page 29: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/29.jpg)
Matlab makes it easy to modify graphs by adding
•Titles
•Axis labels
•Legends
•Other types of annotations
![Page 30: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/30.jpg)
![Page 31: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/31.jpg)
MATLAB
![Page 32: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/32.jpg)
Semicolon (;)
When typed at end of a command, it suppresses the output. (only the prompt is displayed at the next line)
Useful for preventing display of large outputs
Used much more in scripts (see Section 1.8 of text book)
Percent sign (%)
When typed at beginning of a line, MATLAB treats the line as a comment and doesn’t execute the line.
Also used much more in scripts.
Command Syntax
![Page 33: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/33.jpg)
Clears the command window display.
The up and down arrows still bring back the previous commands.
The clc Command
![Page 34: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/34.jpg)
Note: The semicolon suppresses the output from each command
First create a vector of x values – then a corresponding vector of y values
![Page 35: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/35.jpg)
Matlab makes it easy to modify graphs by adding
•Titles
•Axis labels
•Legends
•Other types of annotations
![Page 36: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/36.jpg)
Editing Window
• This window allows you to type and save a series of commands without executing them
• There are several ways to open an editing window– From the file menu– With the new file icon
![Page 37: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/37.jpg)
Open an editing window from the file menu or with the new file icon
![Page 38: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/38.jpg)
New file icon
![Page 39: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/39.jpg)
Use the editor/debugger window to write and debug MATLAB scripts. Open with the Edit command.
Editor/Debugger Window
The Editor Window
![Page 40: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/40.jpg)
Get the help window from the Help menu in any MATLAB window.
(may vary according to version)
The Help Window
![Page 41: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/41.jpg)
In this lesson we will only discuss arithmetic with scalars (single numbers).
We can do arithmetic directly on numbers (like a calculator).
Or, we can store numbers in variables (containers).
Arithmetic Operations
![Page 42: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/42.jpg)
Symbols for arithmetic operations are:
Note: Left division is rarely used with scalars.
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 5 to the power of 3 = 125)
Arithmetic Operators
![Page 43: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/43.jpg)
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
Arithmetic Operations Precedence Order
![Page 44: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/44.jpg)
The MATLAB precedence order is
The same as most calculators.
The same as doing arithmetic by hand.
For multiple operations of same precedence, MATLAB goes left to right.
You can change order by using parentheses.
Arithmetic Operations Precedence Rules
![Page 45: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/45.jpg)
Type in a mathematical expression.
Press the Enter key.
MATLAB displays the answer in the command window as ans = followed by the result. Example:
>> 2 * x - 2 ans = 6
Your display may appear on more than one line and have blank lines between the text.
Using MATLAB as a Calculator
![Page 46: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/46.jpg)
You can control the display of numbers with the format command.
Once a command is entered, the format stays the same until another format command is issued.
The default format is fixed point with four digits to the right of the decimal point. fixed-point means decimal point always between one’s-digit and one-tenth’s digit.
The format command only affects the display of numbers. MATLAB always computes and saves numbers in full precision.
Formatting the Display
![Page 47: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/47.jpg)
Style Result Exampleshort (default) Short fixed decimal
format, with 4 digits after the decimal point.
3.1416
long Long fixed decimal format, with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.
3.141592653589793
shortG The more compact of short fixed decimal or scientific notation. If you are displaying a matrix with a wide range of values, consider using shortG.
3.1416
bank Currency format, with 2 digits after the decimal point.
3.14
![Page 48: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/48.jpg)
Command Description Example
format short
Fixed-point with 4 decimal digits for:0.001 ≤ number ≤ 1000Otherwise display format short e
>> 290/7ans =41.4286
format long
Fixed-point with 15 decimal digits for:0.001 ≤ number ≤ 1000Otherwise display format long e
>> 290/7ans =41.428571428571431
format short e
Scientific notation with 4 decimal digits
>> 290/7ans =4.1429e+001
format long e
Scientific notation with 15 decimal digits
>> 290/7ans =4.142857142857143e+001
format compact
Eliminates empty lines to allow more lines with information displayed on the screen
format loose
Adds empty lines (opposite of compact)
![Page 49: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/49.jpg)
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
Inputs Outputs
functiony
tan(y/x)x
MATLAB Built-In Functions
![Page 50: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/50.jpg)
A function
Has a name.
Can have zero or more arguments (inputs).
Can produce zero or more outputs.
y = sqrt(x)
name argumentoutput
MATLAB Built-In Functions
![Page 51: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/51.jpg)
A function’s arguments can be numbers,
or variables (explained in the next section),
or expressions involving numbers, variables, or functions.
sqrt(64) argument is a number
sqrt(a) argument is the variable a
atan( y/sqrt(3^2+y^2) )argument is an expression that has a number (3), a variable (y), and a function (sqrt)
MATLAB Built-In Functions
![Page 52: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/52.jpg)
sqrt(x) – square root
nthroot(x,n) – nth real root
exp(x) – e to the power of x
abs(x) – absolute value
log(x) – natural log (base e)
log10(x) – log base 10
factorial(x) – x!
See Table 1-3 of text book for details.
Elementary Math Functions
![Page 53: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/53.jpg)
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 for of text book for details.
Elementary Trig Functions
![Page 54: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/54.jpg)
round(x) – round to nearest integer
fix(x) – round toward zero (transforms a double into an integer)
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 for of text book for details.
Rounding Functions
![Page 55: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/55.jpg)
round(A) – Positive elements with a fractional part of 0.5 round up to the nearest positive integer. Negative elements with a fractional part of -0.5 round down to the nearest negative integer. a = -1.9 round(a) a= -2.0000 0 , a=-0.2 round(a) a=0, a=3.4 round(a) a= 3.0000, a=5.6 round(a) a=6.0000
fix(A) – rounds the elements of A toward zero, resulting in an array of integers a = -1.9 fix(a) a= -1.0000 , a=-0.2 fix(a) a=0, a=3.4 fix(a) a= 3.0000, a=5.6 fix(a) a=5.0000
ceil(A) – rounds the elements of A to the nearest integers greater than or equal to A.
a = -1.9 ceil(a) ans = -1.0000 , a = -0.2000 ceil(a) ans =0 , a = 3.4000 ceil(a) ans = 4.0000
floor(A) – rounds the elements of A to the nearest integers less than or equal to A
A = -1.9 floor(A) ans = -2.0000, A = -0.2 floor(A) ans = -1.0000, A = 3.4 floor(A) ans = 3.0000
rem(X,Y) – Remainder after division of X by Y. X = 23; Y = 5; R = rem(X,Y) R=3,
X = 1:5; Y = 3; R = rem(X,Y) R= 1 2 0 1 2
X = [1 2 3;4 5 6;7 8 9]; Y = [9 8 7;6 5 4;3 2 1]; R = rem(X,Y) R = 1 2 3
4 0 2
1 0 0
Functions Examples
![Page 56: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/56.jpg)
Variables
• MATLAB allows you to assign a value to a variable
A=5• Should be read as A is assigned a value of 5• Use the variables in subsequent calculations
![Page 57: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/57.jpg)
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).
Defining Scalar Variables
![Page 58: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/58.jpg)
= (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 sideCreate the variable called a and store the value 3 in it
>> a = 3MATLAB acknowledges that it has created a and set it to 3
a =
3
Assigning Values to Variables
![Page 59: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/59.jpg)
Example:
>> a = 3
a =
3
>> b = 10 * a + 5
b =
35
Create a variable and store a number in it
Create a variable and store the value of an expression made up of a variable, numbers, and addition and multiplication
Assignment Operator
![Page 60: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/60.jpg)
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 (the x on the right side), add 6 to that and store the result back into x (the x on the left side which really is the same container (variable) named x)”.
How Assignment Works
![Page 61: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/61.jpg)
Example:
>> x = 3;
>> x = x + 6
x =
9
>> x = 2 * x
x =
18
; at end prevents MATLAB from displaying value of x.
takes what’s in x (3), adds 6 to it to get 9, then stores 9 back into x.
now x’s value is 9
takes what’s in x (9), multiplies it by 2 to get 18, then stores 18 back into x
now x’s value is 18
Assignment Operator Examples
![Page 62: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/62.jpg)
A variable must have a value before you use it in an expression.
>> x = 3;
>> x + 2
ans =
5
>> x + y % assume y is undefined
??? Undefined function or variable 'y'
comment
Assignment Operator Warning
![Page 63: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/63.jpg)
>> x = 3;
>> y = 10 * x;
>> z = y ^ 2;
>> y
y =
30
>> z
z =
900
The Value of a Variable
To find out the value of a variable, just type it and press ENTER.
![Page 64: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/64.jpg)
You can do multiple assignments on one line by separating them 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
Multiple Assignments
![Page 65: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/65.jpg)
To change the value of a variable, just assign it the new value.
>> ABB=72;
>> ABB=9;
>> ABB
ABB =
9
Changing the Value of a Variable
Only the last value is kept!One value at a time in a scalar variable. The original value of 72 is gone!
![Page 66: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/66.jpg)
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
Variables and Functions
![Page 67: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/67.jpg)
Naming Variables
• All names must start with a letter• They may contain letters, numbers and the
underscore ( _ )• Names are case sensitive• There are certain keywords you can’t use
![Page 68: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/68.jpg)
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.
Variable Name Rules
![Page 69: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/69.jpg)
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,
MTV, MTv, mTV, and mtv are four different variable names!
Variable Name Rules
![Page 70: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/70.jpg)
A variable name cannot contain a space. Two common alternatives:
Use an underscore in place of a space, ex: speed_of_light.
Capitalize the first letter of every word, ex: SpeedOfLight.
Variable Name Rules
![Page 71: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/71.jpg)
A keyword or reserved word 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
They appear in blue when typed in the editor/debugger window (type iskeyword to see the list).
They can’t be used as variable names.
Variable Name RulesMATLAB Keywords
![Page 72: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/72.jpg)
Use the iskeyword function for a list of keywords
iskeyword
ans = 'break' 'case' 'catch' 'classdef' 'continue' 'else' 'elseif' 'end‘ 'for‘ 'function'
'global' 'if' 'otherwise' 'parfor' 'persistent' 'return‘ ‘spmd’ 'switch' 'try' 'while'
Keywords are not acceptable variable names
![Page 73: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/73.jpg)
You can reassign function names
• MATLAB will let you use built-in function names as variables – but it’s a really bad idea
• sin = 3 changes sin from a function to a variable name
• clear sin resets sin back to a function
![Page 74: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/74.jpg)
Which of these names are allowed in MATLAB?
• test• Test• if• my-book• my_book• Thisisoneverylongnamebutisitstillallowed?• 1stgroup• group_one• zzaAbc• z34wAwy?12#• sin• log
![Page 75: Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the](https://reader030.vdocument.in/reader030/viewer/2022032612/56649eb45503460f94bbc7b3/html5/thumbnails/75.jpg)
Which of these names are allowed in MATLAB?
• test• Test• if• my-book• my_book• Thisisoneverylongnamebutisitstillallowed?• 1stgroup• group_one• zzaAbc• z34wAwy?12#• sin• log
x
x
x
x
x x
bad idea