1/100
Looks like no tags are added yet.
Computer
A programmable electronic device that can store, retrieve, and process data
MATLAB Stands for
Matrix Laboratory
Syntax error
Output when the computer does not understand the instruction
Logic error
Program input by the user is read properly by the computer but is not the intended output desired
CPU
Central processing unit
Arithmetic and Logic Unit (ALU)
Performs mathematical functions
Control Unit
Fetches the next instruction, decodes the instruction, and then executes the instruction as appropriate
Volatile memory
Temporary storage that is faster; examples include RAM and DRAM
Non-Volatile Memory
Permanent storage that is slower; examples include read-only memory (ROM), compact discs (CDs), and magnetic tape
Operating System (OS)
Software that allocates computer resources and allows communication between the user and the computer
Programming Language
A structured language that exists to communicate information to a computer in the form of a program
High-Level Languages
Easy for humans to understand, but too difficult for a computer to understand directly (e.g., C = A + B)
Low-Level Languages
Harder for humans to understand, but easier for a computer to convert to usable instructions (e.g., ADD A B C)
Machine Language
Binary digits (0s and 1s) containing instructions understood directly by the computer
Compiled Language
Code is reduced to machine-specific instructions prior to runtime and saved as an executable file; generally runs faster
Interpreted Language
Code written by the programmer is saved in the same format as it was originally written in, which is converted to machine-specific instructions at runtime. (e.g., MATLAB, Python)
Variable
Container for some information; must begin with a letter and can contain up to 63 characters
Assignment rule in MATLAB
Whatever happens on the right-hand side is stored into the variable on the left-hand side of the equals sign
Case sensitivity in MATLAB
MATLAB is case-sensitive, meaning uppercase and lowercase letters matter
clc
Clears the command window where outputs are displayed
clear
Clears the workspace and removes variables
Comments in MATLAB
Started with the % symbol and appear green; block comments use %{ and %}
Section break (Cell)
Created using %% to allow sections of code to be run independently
Order of operations
Parentheses, Exponents, Multiplication and Division, Addition and Subtraction
Element-by-element operation
Performed using a dot operator (e.g., .*) to multiply corresponding elements of matrices
Default values in MATLAB
pi, i, and j
Creating matrices/arrays
Rows are separated by semicolons (;) and columns are separated by spaces or commas (,)
Colon operator (1:5)
Stores values from a lower bound to an upper bound in steps of one (lower:step:upper)
abs()
Finds the absolute value of data or each item in an array
sqrt()
Calculates the square root for real and imaginary numbers
sign()
Finds if a number is positive, negative, or zero, displaying -1, 0, or 1
rem()
Finds the remainder of a division operation
log vs log10
log is the natural logarithm, while log10 is log base 10
Floor and Ceiling
floor rounds down and ceiling rounds up to the nearest integer
factor()
Lists the factors of a number
gcd() & lcm()
Finds the greatest common divisor and least common multiple of numbers
isprime()
Determines if a number is prime (returns 1 for true, 0 for false)
rats()
Converts the input to a fraction format
linspace(n,m,p)
MATLAB function that generates a row vector of linearly spaced points with a specified start, end, and number of points
subplot(m, n, p)
Splits the figure window into m rows and n columns, and selects the p-th slot counting row-by-row from top-left (this is ONLY for subplots other wise counting matrices is top to bottom, left to right)
Array storage order in MATLAB
MATLAB stores and reads matrices from top to bottom, then left to right (column dominant)
plot()
Plots y-data versus x-data
legend()
Creates a legend for a plot
comet()
Creates an animated plot
title()
Adds a title to a plot
figure()
Calls a figure window, needed to make a graph
clf
Clears the content of a figure window without deleting the window itself
close
Deletes one or more figure windows
axis()
Used to control the axes of a plot
semilogy() & loglog()
Plots normal y vs log x, and log y vs log x respectively
plot3() & surf()
Plots 3D line plots and 3D surface plots
Pass by value
The only way MATLAB passes data into functions
Function parts
Name
Inputs
Outputs
description comments
requires an end statement and must be saved in the active folder
Scope (Local vs Global)
Local variables exist only inside their function/workspace; global variables are visible and modifiable across multiple parts of code
Anonymous functions
Created using the @ symbol for quick, single-line functions without a separate .m file
input()
Requests input from the user; using 's' specifies a string input
disp()
Prints the contents of a variable to the screen without its label or formatting capabilities
num2str()
Converts a number input into a character array/string
fprintf()
Prints formatted output to the screen or a file using type fields
fprintf type field %f
Fixed-point notation
fprintf type field %e
Exponential notation
fprintf type field %d
Decimal notation; does not include trailing zeros if integer, or uses exponential notation if fractional
fprintf type field %i
Integer notation
fprintf type field %g
The shorter of %f or %e
fprintf type field %c
Character information (one character at a time)
fprintf type field %s
String of characters (displays entire string)
Formatting characters in fprintf
\n for a new line, \r for carriage return, \t for tab, and \b for backspace
ginput()
Used to get data points from a plot
dlmwrite()
Used to write delimited output
Sequence structure
Lines of code executed one after another
Selection structure
Enables branches within code based on conditions (true/false)
Repetition structure
Causes a group of statements to be executed multiple times (loops)
Pseudocode
Language-independent verbal description of computer code
Flowchart
Graphical representation of how code flows and progresses
find command
Finds the indices of elements in a matrix that meet a specific criteria
Relational Operators (==, ~=, ~)
Equality check, not equal to, and logical NOT
Logical Operators (&, |, &&, ||)
AND/OR for vectors, double opporators for scalars
xor()
Exclusive or logical operation
for loop
Used when you know exactly how many times you want to repeat an action
while loop
A pretest loop that executes as long as a specified condition remains true
For loop execution count
Runs a fixed number of times equal to the number of elements in the index matrix; can run 0 times if the index vector is empty
While loop execution count
Runs conditionally; can run 0 times (if the condition is initially false), a specific number of times, or infinitely (if the condition never becomes false)
break statement
Terminates the smallest enclosing for or while loop prematurely
continue statement
Skips the rest of the current loop iteration and advances to the next pass
Counter
A variable that keeps track of a parameter of interest (e.g., counting occurrences or accumulating sums)
Infinite loop
A loop that never terminates and runs continuously
Keyboard shortcut to stop infinite loops
Windows: Control-C; Mac: Command-Period
tic / toc
Stopwatch feature used to measure elapsed execution time in seconds
etime()
Performs a comparison between a start time and an end time (clock-based)
Pre-allocation
Allocating space for a vector or matrix beforehand (using zeros, ones, etc.) to significantly boost loop performance
Output suppression
Using a semicolon (;) at the end of statements inside loops to prevent printing every iteration and greatly improve speed
4 Performances “Guiding Principles”
only print using print statements
pre-allocating vectors
use recommendations from MATLAB GUI
run code multiple times for average times