ENGR 111: MATLAB Test 2 Ch 2-5
Each MATLAB variable has a specific ____, also referred to as a data type, to indicate the type of data stored in the variable, such as the type of number or letter
class
Clears the command window. Does not affect the workspace, meaning variables are unchanged.
clc
Clears all variables from the workspace, so all variable values are lost.
clear
is text added to provide explanation of a program for humans to read and does not affect how the program runs. The "%" symbol supports a single-line _________. The interpreter ignores all text to the right of "%" on a line, so the programmer can write any text there.
comment
log10(x)
common logarithm
While not a MATLAB command, this keystroke sequence interrupts an endless MATLAB calculation that a programmer may have entered accidentally. Afterwards, the programmer can enter a new command.
ctrl-c
is a numerical value consisting of a real part and an imaginary part. The variables i and j are predefined values for representing the imaginary component of a ________. (can be used in loops)
Complex Number Ex: numA = 5 + 4j
Finite precision of numerical calculations
Rounding error
Imaginary unit equal to sqrt(-1), same as i
j
Identifiers must start with a _____
letter
log(x)
natural logarithm (logarithm with base e)
stores a number. A ________can either be an integer number or a real number.
numeric variable
is a positive or negative number that may have a fractional part
real number (9.62)
smallest floating point number
realmin
are described in the following table, where a scalar is a single number, and a scalar variable refers to a variable that stores one number.
scalar arithmetic operators
is a sequence of statements stored in a text file. MATLAB requires that the name of a ______ file must end in .m, in order to be executable by the MATLAB interpreter. Statements within a _______ are executed line by line from top to bottom as if the statements were directly entered on the command line. (don't end in semicolon)
script
represents both positive and negative integer values
signed integer
tell the MATLAB interpreter to store the character as a unique 8-bit ASCII number.
single quotes
represents only non-negative integers
unsigned integer
(such as age = 20; or nextAge = age;) upon being executed, writes the current value of the item on the ='s right side into the variable on the ='s left side.
Assignment statement
print a single quotation mark. Note that a single quotation mark alone would instead indicate the end of the format string.
" (double quotation marks)
print one percent character. Note that a single % character would instead indicate the start of a formatting operator like %f.
%%
Scientific notation with capital E, as in 3.141593E+00
%E
Either %f or %E, whichever is shorter
%G
is for character
%c
d is for decimal integer. The use of 'd' is due to historical reasons, and is slightly misleading because a decimal number could have a fractional part. In MATLAB, ____ means decimal integer which does not have a fraction.
%d
Scientific notation with lowercase e, as in 3.141593e+00
%e
Fixed-point notation
%f
Either %f or %e, whatever is shorter
%g
i is for integer. This operator is identical to %d when formatting output.
%i
is for string
%s ex: exitChar = 'q'; exitStr = 'quit'; fprintf('Enter %c or %s to exit.', exitChar, exitStr); Command window prints: Enter q or quit to exit.
Specifies left-justified display. Without this item, the default is right-justified.
-
fprintf('10 inch: %5.2f, prc/sqin: %.4f \n', cost10, prcPSqIn10); fprintf('12 inch: %5.2f, prc/sqin: %.4f \n', cost12, prcPSqIn12); fprintf('16 inch: %5.2f, prc/sqin: %.4f \n', cost16, prcPSqIn16);
10 inch: 9.95, prc/sqin: 0.1267 12 inch: 11.95, prc/sqin: 0.1057 16 inch: 14.95, prc/sqin: 0.0744
5e3 =
5000
Addition subtraction, multiplication(*) and division(/) and exponentiation (^)
Arithmetic operator
avg = 14.777; fprintf('The average is %f kilograms.\n', avg) fprintf('The average is %e kilograms.\n', avg) fprintf('The average is %E kilograms.\n', avg)
Command Window reads: The average is 14.777000 kilograms. The average is 1.477700e+01 kilograms. The average is 1.477700E+01 kilograms.
Limited accuracy of the measurement apparatus
Experimental Error
A reference to the function's name, as on the command line above, is known as a ___________
Function call
are comments that appear at the beginning of a function after the function name and arguments but before the first statement and automatically prints the help comments for a given function.
Help comments
Use clear ___ (name of variable)___ to remove previous variable
NOTE
A common special character is ______, which prints a new line. The \ and n together are considered one special character, known as a ______ control sequence
Newline ('\n') ex: amt = 99.42; fprintf('The final amount\nwill be:', amt); Prints on command window: The final amount will be: 99.42
Number that specifies the number of digits to the right of the decimal point
Precision
% A game inspired by "Mad Libs" where user enters nouns, % verbs, etc., and then a story using those words is output. fprintf('Provide input without any spaces.\n'); relative = input('Enter a type of relative: ', 's'); food = input('Enter a type of food: ', 's'); adjective = input('Enter an adjective: ', 's'); period = input('Enter a number: ', 's'); % Tell the story fprintf('\nMy %s says eating %s\n',relative, food); fprintf('will make me more %s \n',adjective); fprintf('so now I eat it every %s days\n',period);
Provide input without any spaces. Enter a type of relative: brother Enter a type of food: oranges Enter an adjective: good Enter a number: 10 My brother says eating oranges will make me more good so now I eat it every 10 days
The interpreter will not prit the output of a statement ending with this
Semicolon (;)
refers to the parts of a program that can access that variable's value. Recall that a variable defined in the workspace can be accessed by any statement executed at the command line, or by any script run from the command line.
The Scope (of a variable)
is defined as how close the stored number is to the desired value.
The accuracy of a floating-point number
print one backslash character. Note that a single backslash character would instead indicate the start of a special character sequence like \n.
\\
Prints a new line.
\n
Prints a tab.
\t
A floating-point number can be formatted in various ways by following the % character with a different ______________ f, e, g, E, or G, as follows:
conversion character
is a list of statements that a programmer creates and gives a specific name such as LightningDistance, with the function being stored in a file LightningDistance.m. The programmer can then execute those statements elsewhere simply by referring to the function's name
custom function
function is a less used function than fprintf that outputs unformatted variables to the command window without outputting the variable's name.
disp ex: radius = 3; circumference = 2 * pi * radius; disp(circumference); Command Window reads: 18.8496
(short for epsilon) is a built-in MATLAB command. If a double-precision computation returns the answer num, the result is only absolutely accurate to within eps(num), and relatively accurate to within eps(num)/num.
eps
Exits the MATLAB session.
exit
is a combination of items like variables, constants, and operators, that results in a value.
expression
Number that specifies the minimum number of digits that will be displayed. If the actual number of digits is less, spaces are automatically inserted to reach the minimum.
fieldWidth
The act of providing input values during a function call is known as ___________ values to the function, with those values known as ____________. A variable created inside a function, such as speedOfSound above, is known as a __________ and does not exist outside the function.
passing arguments local variable
fprintf('A circle''s diameter is 32%% of its circumference.\n');
prints on command window: A circle's diameter is 32% of its circumference.
Special two-character sequences, each known as a_________, can be used to print special items in a format string.
special character
defined in the same m-file have a separate function workspace, which means that any variable defined in the workspace of a subfunction is local and only available within that subfunction.
subfunctions
finds the absolute accuracy of the valued stored within the variable aNum. The relative accuracy is given by eps(aNum)/aNum and this value will never exceed eps(1)/1 or eps(1).
The command eps(aNum)
The "=" is known as the _____________
assignment operator
Prints all variables in the current workspace.
who
Example of rounding error:
>> aVal = 4 / 3 aVal = 1.3333 >> bVal = aVal - 1 bVal = 0.3333 >> cVal = bVal + bVal + bVal cVal = 1.0000 >> error = 1 - cVal error = 2.2204e-016
operator prints a floating-point number using fixed-point notation.
%f
A string rather than a number can be obtained from the user by the programmer passing a second argument ___ to the input function: treats input as a string (word) even if its a number
's' ex: myStringVar = input ('Text prompting user to enter a string: ', 's');
precedence rules for arithmetic operators:
( ) Items within parentheses are evaluated first ^ Power is evaluated next. Thus 2^3*3 evaluates as (2^3)*3 or 8*3 which is 24. * / Next to be evaluated are * and / having equal precedence. + - Finally come + and - with equal precedence. Thus, 3+2*A is evaluated as 3+(2*A) . left-to-right If more than one operator of equal precedence could be evaluated, evaluation occurs left to right. Thus, A*2/3;" evaluates as (A*2)/3
an epression that indicates that an equation continues on the next line
. . . (tree periods spaced) ex: >> x1 = (1/2)^1+(1/2)^2+(1/2)^3+... (1/2)^4+(1/2)^5+(1/2)^6+... (1/2)^7+(1/2)^8+(1/2)^9;
Exaple of up-arrow key:
>> height = 1; bounce = 0; distance = 0; >> height = 0.75 * height; bounce = bounce + 1, distance = distance + 2 * height bounce = 1 distance = 1.5000 (press up-arrow key) >> height = 0.75 * height; bounce = bounce + 1, distance = distance + 2 * height bounce = 2 distance = 2.6250 (press up arrow key) >> height = 0.75 * height; bounce = bounce + 1, distance = distance + 2 * height bounce = 3 distance = 3.4688 (etc....)
Initially capitalizing each word is known as ________ because the capital letters appear like the humps on a camel.
Camel Case
which has the class char, stores a single character like the letter 'm' or the symbol '%'. Values of type char are denoted by single quotes (e.g., myChar = 's').
Character variable
Multiple MATLAB statements can appear on the same line by separating those statements with a ______ or __________. A statement followed by a _______ will have its result printed, while a statement followed by a ________ will not have its result printed.
Comma or Semicolon Comma Semicolon
is to overwrite the value in a variable before that variable's value was used. ex: >> firstBid = 85 firstBid = 85 >> secondBid = 119 secondBid = 119 >> firstBid = secondBid firstBid = 119 >> secondBid = firstBid secondBid = 119
Command error
provides the basic method in which a programmer types statements to be executed by the MATLAB interpreter and presents users with interactive environment.
Command window
Commands that alter how output appears in the command window
Format commands
Numbers are displayed using a fixed-point representation , wherein each number is displayed with a fixed number of digits after the decimal point, the default being 4 digits after the decimal point. The Long command sets the display to show 15 digits after the decimal point and short commands returns to display 4 digits.
Format long/short
AnswerLookedFor = variableName(var1, var2, var3)
Function (at beginning of code)
which provides information on how to use internal functions, can be obtained from the command line, as summarized in the following table.
Function documentation
are comments that appear at the beginning of a script file before the first statement. The help command prints the help comments for a given script.
Help comment EX: >> help ConeVol Computes volume of a cone. Creates variables radius and height. Result is assigned to variable volume and displayed.
Usually does not matter Ex: Hoursperweek=4; Hoursperweek = 4 ;
Horizontal space (white space)
A variables name. A valid ______ consists of a sequence of letters (a-z, A-Z), digits (0-9), or underscores (_), and must start with a letter. Other symbols are not allowed. Upper and lower case letters differ, meaning MATLAB is case sensitive, so variables named "balance", "BALANCE" and "Balance" represent three different variables.
Identifier
infinity
Inf
is a variable that has a logical data type (class).
Logical variable
are special variables with predefined numerical values (e.g., pi) built into MATLAB.
Mathematical Constants
Not-a-number
NaN
is the difference between the calculated approximation of a number and the exact mathematical value.
Rounding errow
is an indication of the actual numbers that can be represented, usually written from smallest to largest.
The range (of a number class)
Discarding the least significant digits or a numerical result
Truncation error
A useful feature of the MATLAB command line is that a command (or sequence of commands) from a previously-entered line can be retrieved by pressing the _________ key
Up-arrow
is a named place for a programmer to store data in memory. A programmer defines a new ______ by writing a statement like age = 20;
Variable
Displays current data being used by the computer
Workspace
which is the difference between the calculated value and the actual value.
absolute error eps(number)
Assign variable allowBoat with string: OK'ed
allowBoat = 'OK''ed' (two single quotes)
is 8 bits
byte
Records into a file almost everything that appears in the command window. The file's name is chosen by the programmer, such as "mysession.txt". "diary off" ends the recording.
diary
Detailed information on the function known as functionname
doc functionname
a way that a programmer can check whether a name already exist in a current workspace:
exist ___(name of variable)_____ If: result is ans = 1 then it means that MATLAB interpreter already recognizes that name and a different name should be chosen.
which is the difference between a measured value and the actual value of the quantity being measured.
experimental error
The command window by default prints output text using vertical double spacing. The ________ command changes to single spacing. ________ reverts to double spacing. Many subsequent examples will be shown using compact output.
format compact format loose
Above, the fprintf function call has a single argument consisting of a string and can contain literal text as above, as well as formatting operators and special characters as described below.
format string
indicate that a value should be printed at that place in the string, such as the %f operator below.
formatting operators ex: amt = 99.42; fprintf('The final amount will be %f units.', amt); This will appear on command window as: The final amount will be ______ units
function prints formatted output from a program.
fprintf ex: fprintf ('The final amount will be: '); (prints on the command window as: The final amount will be: ____)
List of the elementary mathematical functions
help elfun
Summary of how to use the function known as functionname
help functionname
List of the advanced specialized mathematical functions
help specfun
Imaginary unit equal to sqrt(-1)
i
Numerical input from a user can be obtained with the ______ function and stored into a variable
input ex: celsius = input('Enter a temperature in Celsius: '); fahrenheit = ((9*celsius) / 5) + 32
is a positive or negative number that does not have a fractional part.
integer number (9)
is a value that is either true (indicated by a non-zero number, typically 1) or false (indicated by the number 0).
logical data type
clear x
only clears the stated value of x
occurs when the result of a expression exceeds the maximum value that can be stored within a variable.
overflow
may be used in expressions to dictate a specific order of operations
parentheses
The ratio of circle's circumference to its diameter, which is approximated as 3.14159265...
pi
Largest floating point number
realmax
which is the difference between the calculated value and the actual value divided by the actual value.
relative error
is any sequence of characters, including special characters like "$" or numbers like "9". In MATLAB, a ______ is not a separate data type (class), but rather a sequence (i.e., array) of characters.
string
A _____ variable is defined by enclosing the characters in single quotes.
string Ex: >> name = 'Julia!' name = Julia! >> happyMessage = 'Isn''t today a great day?' happyMessage = Isn't today a great day?
of a number represented in floating point, which is given by eps(aNum), decreases with the magnitude of aNum. Stated differently, the absolute error gets larger with the magnitude of aNum. It is the relative accuracy that will be bounded by eps(1).
the absolute accuracy
that occurs when the least significant digits to the right of the decimal point are truncated (i.e., discarded).
truncation error
Prints all variables in the current workspace as well as some extra information about their size, bytes, class, etc.
whos