Midterm 1 Review: Matlab

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

example of using signed and unsigned integers to make arrays:

% Using signed integers x = int16(-32768:32767); % Create an array of signed 16-bit integers y = int32(-2147483648:2147483647); % Create an array of signed 32-bit integers % Using unsigned integers u = uint8(0:255); % Create an array of unsigned 8-bit integers v = uint16(0:65535); % Create an array of unsigned 16-bit integers

comment

% before comment

single line comment

% everything to the right of is ignored by the interpreter % Single-line comment. The interpreter ignores any text here, like ;, 5.4, %, etc.

block comment

%{ " and "%}

continuation

(...)

swapping values

(x = 22, y = 99) >> tempVal = x; >> x = y; >> y = tempVal; x is 99, y is 22

((x > 2) || (y < 4)) && (z == 10)Given x = 4, y = 1, and z = 10, which comparisons are evaluated?

(x > 2) and (z == 10)

Scalar Arithmetic Operators

+ , - , * , /, ^

realmax

Largest possible floating-point number

command window

Main window, enters variables, runs programs

unary minus

Minus (-) used as negative

Write a statement that assigns variable myNum with the result of a call to the function defined below with arguments 3 and 9function o1 = CalcR( i1, i2 )

MyNum = CalcR(3, 9)

floating point numbers

Numbers where the decimal point can float because there is no fixed number of digits before and after the decimal point. AKA: real numbers; usually used with measured values as opposed to values that can be counted; normally written as normalized scientific notation

whos

Prints all variables in the current workspace as well as some extra information about their size, bytes, class, etc.

valid function calls for this function definition: function [ surfaceArea, baseArea, vol ] = ConeVals ( radius, height )

[surf, base, vol] = ConVals(rds, hght) %number and order of arguments match input and output [surfArea] = ConsVals (rds, hght) % last two outputs simply ignored NOT [vol] = ConeVals(rds, hght) because then the surface area will be written into vol

relational operators

a < b: a is less than b a > b: a is greater than b a <= b : less than or equal to a >= b : greater than or equal to

a ~= b

a and b are not the same

a == b

a and b are the same

Variable

a named reference to a value that can be used repeatedly throughout a program.

single-precision floating-point number

a type of value that is stored in a float; 32 bits of memory and about 7 decimal digits

local variable

a variable created inside a function and doesn't exist outside the function

eps(number)

absolute error (difference between the calculated value and the actual value)

when no variables were assigned in the workspace and you want to run a script:

assign variables first then run script

a number in base two is called a

binary number

logical variable

can be used to test logical states and hold logical values; testes if something is true or false

long statement in script

can use ellipsis; (...) to continue on the other line y = 1 + ... 2 + ... 3; is equivalent to: y = 1 + 2 + 3;

char

class that stores a single character with single quotes

clear

clears all variables from the workspace so variables are lsot

clc

clears command window

help comments

comments that appear at the beginning of the script file before the first statement or before the first blank line is encountered; prints help comments for a given script >> help ConeVol Computes volume of a cone. Creates variables radius and height. Result is assigned to variable volume and displayed.

ASCII standard

commonly-used way of encoding characters using bits; represents letters as 8-bit codes, so myChar = "s" would store 115 into myChar, for instance

assignment statement

current value of the item(variable) on the right side of the = , and variable name on the left side of the =

num1 = 5

data item num1 is allocated in memory and the value 5 is stored in that memory

a number in base ten is called a

decimal number

rounding error

difference between the calculated approximation of a number and the exact mathematical value

Truncation Error

discarding the least significant digits or a numerical result

workspace

displays current data being used by the interpreter

Short-circuit logical operators

expr1 && expr2 expr1 | | expr2

Complete the function WhatIsIt such that: The output logical variable onTime is true only if noTraffic is true and gasEmpty is false. The output logical variable delayed is false only if noTraffic is true and gasEmpty is false. Restriction: Logical expressions must be used. Do not use if statements.

function [onTime,delayed] = WhatIsIt(noTraffic, gasEmpty) onTime = noTraffic & ~gasEmpty; delayed = ~onTime; end

function definition

function [output1, output 2] = FunctionName( input1, input2, input3) end

The variable baggageWeight is the weight of an item. Complete the function CheckWeight(baggageWeight, maximumWeight) to return a logical value that indicates true wherever baggageWeight is above maximumWeight. For example: If baggageWeight = 28 and maximumWeight = 50, then overweightBaggage = CheckWeight(baggageWeight, maximumWeight) will return overweightBaggage = false

function overweightBaggage = CheckWeight(baggageWeight, maximumWeight) overweightBaggage = (baggageWeight > maximumWeight); end

Type the first line of a function definition for a function named CubeNum with input xVal and output yVal .

function yVal = CubeNum (xVal)

a function accepts ________ variables and proves _________ variables

inputs, outputs

complex numbers

j used as i; 4j or 4*j but NOT j4

experimental error

limited accuracy of the measurement apparatus

custom function

list of statements that a programmer creates and gives a specific name; can be executed elsewhere by simply referring to the functions name

matlab statement example

num1 = 5

requirements of scientific notation:

number is written as a digit (1-9), decimal point, fractional part, times 10 to a power; only one digit preceding decimal point, first digit is more than 0

semicolon

output not printed

who

prints all variables in the current workspace

diary

records into a file almost everything that appears in the command window

function call

reference to a function's name on the command line

realmin

returns the smallest possible floating point number

saving variables in matlab

save and load variables in the MATLAB binary format known as a MAT-file

save("filename") command

saves all variables in current workspace; saved as binary format by default

string

sequence of characters enclosed by double quotes

script

sequence of statements stored in a file; simple form of a program in MATLAB

int8

signed 8-bit integer, -128 to 127

short circuit evaluation

skips evaluating later operands if the result of the logical operator can already be determined (logical AND short circuit operator evaluates to false if first operand is false, and skips evaluating second operand; logical OR short-circuit operator evaluates to true if the first operand is true; skips others)

mathematical constants

special variables with predefined numerical values. (pi , i, j, Inf, NaN (Not-a-Number), realmin, realmax, eps)

numeric variables

store numbers

double-precision floating-point number

stored in a 'double'; 64 bits of memory and about 15-16 decimal digits; higher accuracy and more memory space

string scalars

stores text in a variable that has a class string

passing values to the function

the act of providing input values during a function call

name of the file must be

the exact same as that of the function or else function is undefined

camel case notation

the first word is all lower case and each first letter of subsequent words are Capitalized. (Example: "myVariable" or "faceColor" or "upperLeft")

abs(a - b) < threshold

to compare floating point numbers

T OR F: Using a double precision floating-point representation, the relative accuracy of variable aNum will never exceed eps(1)

true

dont compare floating point numbers using equality, instead use a threshold

true

logical value (boolean value)

true (1) or false (0)

int32

type represented 32-bit number with no fractional component

uint8

unsigned 8-bit integer, 0 to 255

Specifying the range

use two < operators along with an AND operator

Live Script Editor

used to create .mlx script or live script

script editor

used to create a .m script (also by text editor); can be run from the command window

arguments

values that are passed when a function is called

identifier

variable's name; sequence of letters, digits, underscores and MUST start with letter; NO other symbols allowed; case-sensitive;

loading variables from different file formats

via wizards

Different amounts of calories can be burned during various forms of exercise. One metric used to differentiate between the intensity of different exercise activities is the Metabolic Equivalent of Task (MET) (source).The following equation estimates the calories burned when doing an exercise with a specific MET value (source): Calories Burned = Weight (kilograms) * MET value * Time (hours) Write a program using the variables weight (weight in kilograms), MET (MET value), and time (time in hours). Given weight is provided in pounds (lbs) and should be converted to kilograms (kg) by dividing by 2.2. Given time is provided in minutes and should be converted to hours. Assign the value for the calories burned to the variable caloriesBurned.

weightInPounds = randi([100,260],1) MET = randi([1, 16],1) timeInMinutes = randi([30,120],1) weight = weightInPounds/2.2; time = timeInMinutes / 60; caloriesBurned = weight*MET*time

overflow

when the result of the binary operation is too large to fit in allowed number of bits (ex. four-bit numbers that are added and yield a 5 bit number, causing overflow)

Since 1896, the Summer Olympic Games have been held every 4 years, with a few exceptions for global impact events (ex: WWI, WWII, and the 2019/2020 global pandemic). (source). Write a program to determine if a given random year between 1948 and 2019 has held a Summer Olympic Games. Use relational operators to assign 'true' to the logical variable summerOlympicsYear if the variable year is greater than or equal to 1948 and is divisible by 4. Otherwise, the variable summerOlympicsYear should be assigned the logical value 'false'. Ex: Given: year = 2000 then the output of the program is: summerOlympicsYear = logical 1 For this program, use the Matlab function mod (source), which returns the remainder of the division of two given values. b = mod (a,m) returns the remainder of the division of a by m. Ex: remainder = mod(4,2) results in remainder = 0

year = randi ( [1948 , 2019] , 1); summerOlympicsYear = (year >= 1948) & mod(year, 4) == 0

Overwriting the Value

Changing the value stored inside a variable by performing another assignment statement with the same variable.

running a script twice

CostDrive ; CostDrive (use semicolon)

Int64

64 bit signed integer

uint64

64-bit unsigned integer

Byte

8 bits

four-bit binary digit weights

8, 4, 2, and 1

example Matlab session

>> format compact >> frac = 9 / 5 frac = 1.8000 >> F = C * frac + 32 Undefined function or variable 'C'. >> C = 0; >> F = C * frac + 32 F = 32 >> C = 100; >> F = C * frac + 32 F = 212 >> who Your variables are: C F frac >> C C = 100 >> clear >> C Undefined function or variable 'C'. >> who >> exit

unsigned integer

A data type that stores positive integer values as ordinary binary numbers; its value is always assumed to be positive.

logical operators

A&B: and(A,B) outputs 1 if both A and B are true; if one or both are false, outputs 0 A | B: or(A, B) Or function; outputs 1 if one or both true, and 0 if neither is ~A : not(A) negates or complements the input logical variable A by returning a logical variable that has the opposite logical state xor(A,B) exclusive-OR; output 1 if either A or B is true BUT NOT BOTH

signed integer

An integer that uses a sign bit to indicate whether the value is negative or positive.

file extensions

.m or .mlx

1-10 in binary

0 , 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1100, 1110, 1111

Adding binary numbers

1 + 1 = 10; carry one 1 + 1 + 1 = 11; carry one 1 + 0 = 1 0 + 0 = 0

precedence rules for arithmetic, logical, and relational operators

1. ( ) 2. ^ 3. Negation 4. * / + - 5. < <= > >= 6. & 7. | 8. && 9. | |s

precedence rules

1. () 2. ^ 3. negation (unary minus) 4. * / 5. + - 6. left-to-right

creating a live script

1. new live script and name the file with .mlx extension 2. type statements and set variables+ computations 3. script can be run by writing the file's name without the .mlx into the command line and the answer to the computations are outputted 4. save every change before running a script

Int16

16-bit signed integer

uint16

16-bit unsigned integer

uint32

32-bit unsigned integer


Ensembles d'études connexes

N20025: E3: Drugs to Decrease Histamine Effects & Allergic Response

View Set

Chapter 3 Methods and Encapsulation

View Set

Chapter 11: Drinking Alcohol Responsibly

View Set

Chapter 9 : The Master Budget and Responsible Accounting

View Set

Retirement Planning and Employee Benefits - Final Exam

View Set

Introduction to U.S. National Politics: Chapter 15 Quiz

View Set

Organizational Behavior, Organizational Behavior: Chapter 9, Organizational Behavior Exam 2, Organizational Behavior, Organizational Behavior - Chapter 8, Organizational Behavior Exam 2, Organizational Behavior, Organizational Behavior - Chapter 13

View Set

Wellness study guideWhich of the following is not an intervention strategy?

View Set

Chapter 12, "Inference on Categorical Data,"

View Set