MATLAB Basic Programming

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/113

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

114 Terms

1
New cards

What will the statement "myNum = 2 + 2" store in myNum?

4

2
New cards

Suppose you want to automatically generate a vector of with 2 entries; the 1st entry starting at 0 and ends at 1. How would you create this vector in MATLAB?

x = linspace(0,1,2)

3
New cards

Whats another way to do this?

0:1:2

4
New cards

Now Suppose you want to automatically generate a vector of 11 entries; the 1st entry starting at 0 and ends at 1. How would you create this vector in MATLAB?

x = linspace(0,1,11)

5
New cards

Whats another way to do this?

0:1:11

6
New cards

Now Suppose you want to automatically generate a vector with 8 entries; the 1st entry starting at 0 and ends at 3. How would you create this vector in MATLAB?

x = linspace(0,3,8) or x = 0:3:8

7
New cards

How would you access the first twelve elements for y?

y = (1:12);

x(1:12)

OR

y = (1:12);

x(1:12)

8
New cards

How would you access the 1st four multiples of 3 specified in a vector?

y([3 6 9 12])

9
New cards

Write a statement transposing x

x'

10
New cards

Write two statements that notes the difference in tranpose of z

z' and z.'

11
New cards

Elementary functions in MATLAB

Use the following few commands (a script) to make a plot. The evaluation of v=cos(u) creates a vector whose elements are:

v(k) = cos(u(k)) where k = 1, 2, ...n

n = anu number;

u = linspace(0, 2pin);

v = cos(u);

plot(u,v)

12
New cards

How would you clean up this plot?

By plotting axis label and a title; the text inside the single quotes is a string which we intend to be the labels.

xlabel('u');

ylabel('v');

title('v = cos(u)');

command "help plot" would be very helpful

13
New cards

Write a statement that would transpose a 3x4 matrix

x = [1 2 3 4; 5 6 7 8; 9 10 11 12];

x'

x =

1 2 3 4

5 6 7 8

9 10 11 12

ans =

1 5 9

2 6 10

3 7 11

4 8 12

14
New cards

If the previously-entered statement was "num1 = 5", what value will be stored in num2 after the statements "num1 = num1 + num1" followed by "num2 = num1 + 3".

13;;

The interpreter computes 5 + 5, which is 10, then stores 10 in num1. The interpreter then computes num1 + 3, which is 10 + 3 or 13, and stores 13 in num2.

15
New cards

If the previously-entered statement was "num1 = 9", what value will be stored in num2 after the statement "num2 = num1 * num1"?

81

16
New cards

If all previous statements dealt only with num1, num2, and num3, what value will the statement "num4" print?

error

17
New cards

If all previous statements dealt only with num1, num2, and num3, what value will the statement "num4" print?

error

18
New cards

What symbol at the end of a statement suppresses printing of the result?

; (semicolon)

19
New cards

Test your understanding of what will be output to the command window as a result of entering the following lines.

1)

X = 14; Y = 2, Z = 5

Only X will display.

Only Y and Z will display.

None of the above.

Only Y and Z will display.

The semicolon suppresses the output of X, commas do not suppress output.

20
New cards

C = 100 K = C + 273

Only C will display.

Both C and K will display.

The statement results in an error.

Statements must be separated by a semicolon or comma, else the line is treated as one statement, which in this case is not a valid statement, yielding an error.

21
New cards

Matt Labler is a novice MATLAB programmer. He wants to set variable A to 6, B to 9, and C to their sum, with only the values of A and C displaying in the command window. He asks you to check the following statement for bugs: A = 6; B = 9; C = A + B

The code is correct as is.

Matt should use "A = 6, B = 9; C = A + B" instead.

Matt should use "A =6; B = 9, C = A + B;" instead.

Matt should use "A = 6, B = 9; C = A + B" instead.

22
New cards

X = 39; Y = 41, Z = X + 17

The statement results in an error.

Only Y is displayed.

Only Y and Z are displayed.

only Y and Z displayed

23
New cards

Matlab challenge activities

Multiply x by 2 and put the result in y.

Ex: If x is 2, then output y should be 4.

x=2;

y=x+2

24
New cards

A bit is a 0 or 1, interpreted from voltage.

True

False

true

1 is usually a higher voltage (e.g., 2.0 V), 0 is usually a lower voltage (e.g., 0 V).

25
New cards

A processor is a circuit that executes instructions.

True

False

true

26
New cards

A memory is a circuit that stores one machine instruction.

True

False

false

27
New cards

An assembler translates an assembly program into machine instructions.

True

False

true

28
New cards

A high-level language eases a programmer's task of writing complex programs.

true

29
New cards

See PARTICIPATION

ACTIVITY

1.5.2: Binary numbers.

30
New cards

Defining numeric variables.

1)

Write a variable assignment statement that assigns totalPets with 3.

totalPets = 3;

or

totalPets = 3

31
New cards

Write a variable assignment statement that assigns luckyNumber with 7 and suppresses the output to the command window.

luckyNumber = 7;

32
New cards

Write a variable assignment statement that assigns birdFeeders with the current value of variable stockCount, and prints the value of the variable to the command window.

birdFeeders = stockCount

33
New cards

What is the value stored in luckyNumber after these two statements execute?

luckyNumber = 7;

luckyNumber = 4;

4

The second write to luckyNumber overwrites the first value written into the variable.

34
New cards

Assume apples currently has the value 5 and oranges the value 9. What is the value stored in apples after the following statement?

apples = apples + 1;

6

5 + 1 yields 6

35
New cards

Assume apples currently has the value 5 and oranges the value 9. What is the value stored in apples after the following statements?

apples = oranges;

oranges = oranges + 1;

9

36
New cards

Write a statement, ending with "- 1;", that decreases the value stored in apples by 1.

apples = apples - 1;

37
New cards

Write two statements, the first setting apples to 7, and the second setting oranges to apples plus one. Terminate each statement with a semicolon.

apples = 7;

oranges = apples + 1;

"apples = 7" must be done first, then "oranges = apples + 1" will store 7+1 or 8 into oranges

38
New cards

Given x = 22 and y = 99. What are x and y after the given code?

1)

>> x = y;

>> y = x;

x is 99 and y is 22.

x is 22 and y is 99.

x is 99 and y is 99.

x is 99 and y is 99.

39
New cards

>> x = y;

>> y = x;

>> x = y;

x is 99 and y is 22.

x is 99 and y is 99.

x is 22 and y is 22.

x is 99 and y is 99.

40
New cards

>> tempVal = x;

>> x = y;

>> y = x;

x is 99 and y is 22.

x is 99 and y is 99.

x is 99 and y is 99.

41
New cards

>> tempVal = x;

>> x = y;

>> y = tempVal;

x is 99 and y is 22.

x is 99 and y is 99.

x is 99 and y is 22.

42
New cards

numNickels = 5

numDimes = 10

% Write a statement that assigns numCoins with numNickels + numDimes

numCoins = numNickels + numDimes

43
New cards

Given a Fahrenheit value temperatureFahrenheit, write a statement that assigns temperatureCelsius with the equivalent Celsius value. While the equation is C = 5/9 * (F - 32), as an exercise use two statements, the first of which is "fractionalMultiplier = 5/9;".

77 degrees fahrenheit

% Write a statement that assigns fractionalMultiplier with 5/9

% Modify the statement below to convert Fahrenheit to Celsius,

% assigning temperatureCelsius with the Celsius value

temperatureCelsius = (5/9) * (77 - 32) = 25

44
New cards

% Write a statement that assigns myExamScore with 82

myExamScore = 82

45
New cards

% Write a statement that assigns myCurvedExamScore with myExamScore + 5

myCurvedExamScore = myExamScore + 5

46
New cards

Which are valid identifiers?

1) r2d2

2) name$

3) _2a

4) pay_day

5) pay-day

6) 2a

7) y____5

8) switch

1, 4 and 7

47
New cards

The statement "myInt = 44" creates an integer variable.

True

False

false

48
New cards

The statement "myNum = 0.1" creates a single-precision floating-point number because 0.1 is small enough for single precision.

True

False

false

49
New cards

A double-precision number's range is about 10 times greater than a single-precision number's range.

True

False

false

50
New cards

The default MATLAB floating-point representation can represent various numbers from -1.79e+308 to +1.79e+308.

true

51
New cards

The smallest positive number that the default MATLAB floating-point representation can store is 1.1755e-38

True

False

false

52
New cards

Suppress the output of all assignment statements.

1)

Write a statement that assigns the double precision floating-point variable tempKelvin with 0.0.

tempKelvin = 0.0;

53
New cards

Assign celsiusVal with kelvinVal minus 273.15.

celsiusVal = kelvinVal - 273.15;

54
New cards

Using notation similar to 6.02e23 and starting with 3.1, write a statement to assign variable wrenchTorque with the value 3,125.99.

wrenchTorque = 3.12599e3;

55
New cards

Complex numbers.

Assume i and j are the predefined values for the imaginary component of an imaginary number.

1)

What is the result of numA = i * i?

-1

56
New cards

What will the command window display after the assignment numB = 4.5 + 2*i?

numB = 4.5000 + 2.0000i

57
New cards

What will the command window display after the assignment numC = 3.2 + 5*j?

numC = 3.2000 + 5.0000i

58
New cards

Rewrite the following expression without using the * symbol:

numD = 7 + 5*j;

numD = 7+5j;

59
New cards

Rewrite the following expression without using i:

numE = 3 + 6i;

numE = 3 + 6j

60
New cards

Mathematical constants.

Which of the following variables are predefined mathematical constants?

1) Eps

2) J

3) NaN

4) e

5) inf

3

61
New cards

Write a statement that assigns circleArea with the circle's area given circleRadius

circleRadius = 5

% Modify to use built-in math constant pi.

circleArea = picircleRadiuscircleRadius

62
New cards

Evaluating expressions using precedence rules.

Note: This activity omits parentheses to test knowledge of precedence rules. But programmers should use parentheses to make evaluation order explicit.

1)

Does 2 / 2 * 3 equal 2/6?

Incorrect

/ and have equal precedence so are evaluated left to right, thus (2/2)3, which is 1*3, yielding 3.

63
New cards

Does 2 / 3 ^2 equal 4/9?

Incorrect

Exponent has higher precedence, so 2/(3^2) is 2/9.

64
New cards

Does 2 + 3 * 4 - 4 equal 16?

Incorrect

* has higher precedence, so 2 + 12 - 4 is 10.

65
New cards

Does 2 ^ 3 ^ 2 equal 64?

Correct

The power operators evaluate left to right, so (2^3)^2 or 8^2, which is 64.

66
New cards

Does -4 ^ 2 equal -16?

Correct

^ has higher precedence than -, so -(4^2) is -(16).

67
New cards

Precedence rules.

Select the expression whose parentheses enforce the compiler's evaluation order for the original expression. Note: Using such parentheses is preferable over relying on precedence rules.

y + 2 * z

y + (2 * z)

68
New cards

z / 2-x

(z / 2) - x

69
New cards

x y z

(x y) z

70
New cards

x + y ^ 3

x + (y ^ 3)

71
New cards

x + 1 * y / 2

x + ((1 * y) / 2)

72
New cards

x / 2 + y / 2

(x / 2) + (y / 2)

73
New cards

What is totCount after executing the following?

numItems = 5;

totCount = 1 + (2 numItems) 4;

44

After (2 5) is evaluated, 4 is evaluated because * has precedence over +

74
New cards

Multiple statements on a line.

1)

What is val after executing the line num = 6; num = num * 2; val = num + 1;

13

num will assigned with 6 * 2 or 12. Then val will be assigned with 12 + 1 or 13.

75
New cards

What is x after executing the line x = 5; y = 9; y = x; x = y;

5

76
New cards

Write a statement that assigns finalResult with firstSample plus secondSample, divided by 3. Ex: If firstSample is 18 and secondSample is 12, finalResult is 10.

finalResult = (1/3) * (firstSample + secondSample)

77
New cards

A drink costs 2 dollars. A taco costs 3 dollars. Write a statement that assigns totalCost with the total meal cost given the number of drinks and tacos.

Ex: 4 drinks and 6 tacos yields totalCost of 26.

numDrinks = 4 % Number of drinks

numTacos = 6 % Number of tacos

% assigns totalCost with the total meal cost given the number of drinks and tacos

totalCost = (24) + (63)

78
New cards

Expression with multiple exponents: Computing wind chill

Wind chill (C) is computed from the air temperature (T) and wind speed (W): C = 35.7 + 0.6T - 35.7W^0.16 + 0.43TW^0.16

Write a statement that assigns windChill (C) with the wind chill given airTemp (T) and windSpeed (W).

Ex: If airTemp is 32 and windSpeed is 10, then windChill is assigned with 23.1871.

airTemp = 32;

windSpeed = 10;

% Assigns windChill with the temperature felt given airTemp and windSpeed

windChill = 35.7 + 0.6(32) - 35.7(10)^0.16 + 0.43(32)(10)^0.16

79
New cards

Repeated commands.

1)

Variable num is initially 2. The command num = num * 2 is typed and entered once; then the up-arrow key is pressed followed by enter, three times. What is num?

32

num = num 2 is executed four times, leading to 22 or 4, then 42 or 8, then 82 or 16, and finally 16*2 or 32.

80
New cards

What is the next value after 34 in the Fibonacci rabbits example?

55

81
New cards

Characters.

1)

Write a statement that assigns variable delimChar with the comma character. End with a semicolon.

delimChar = ', ';

82
New cards

Given x='a'; y='b', assign x with the current value of y. End with a semicolon.

x = y;

83
New cards

What is the decimal number stored in a char variable delimChar after the following statement: delimChar = 'a';

97

84
New cards

Declaring a character

Write a statement that assigns middleInitial with the character T.

myChar = 'T';

middleInitial = myChar

85
New cards

String scalars and character vectors.

1)

Assign variable unitName with character vector: miles

unitName = 'miles';

86
New cards

Assign variable unitName with string scalar: nautical miles

unitName = 'nautical miles';

87
New cards

Assign variable allowBoat with character vector: OK'ed

allowBoat = 'OK''ed';

88
New cards

Assign variable phrase with string scalar: Isn't it OK?

"Isn't it OK?"

89
New cards

Write a statement that assigns the logical variable isAvailable with the logical value 'true'.

isAvailable = true

90
New cards

Given the following script saved in file CostDrive.m:

miles = 100;

costPerMile = 0.30;

cost = miles * costPerMile;

What is the command line statement to run the script?

CostDrive (with .m omitted)

91
New cards

Assume that no variables have been assigned yet in the workspace. What statement would calculate the speed in miles per hour for a distance of 56.9 miles and elapsed time of 73 minutes?

distanceMiles = 56.9; timeSecs = 73*60; CalculateMPH

92
New cards

Assume that no variables have been assigned yet in the workspace. How many variables would be assigned after executing the following code:

distancePes = 1000; timeSecundae = 55; RomanUnitsToMPH

8

93
New cards

Suppose distanceMiles equals 1870, timeSecs equals 343440, and speedMPH equals 55.0. What is the value of speedMPH after calling CalculateMPH?

19.6

94
New cards

What is the only character that indicates a comment?

%

95
New cards

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

function yVal = CubeNum (xVal)

96
New cards

Write a statement that assigns variable myNum with the result of a call to the function defined below with arguments 3 and 9

function o1 = CalcR( i1, i2 )

myNum = CalcR(3, 9)

97
New cards

What happens to input and output variables in a function call?

The following function purposely violates good practice in order to test understanding of the underlying concepts of a function:

function out = PlusOne( inVar )

inVar = inVar + 1;

out = inVar;

end

1)

What is the value of inVar in the global workspace after executing:

inVar = 1;

out1 = PlusOne(inVar);

1

98
New cards

What is the value of inVar in the global workspace after executing:

inVar = 1;

inVar = PlusOne(inVar);

2

99
New cards

Assign y with 5 plus x.

Ex: If x is 2, then y is 7. If x is 19, then y is 24.

function y = IncreaseValue(x)

% x: User defined value that is added to 5

% Modify the line below to y = 5 + x

y = 5 + x;

end

Code to call your function :

IncreaseValue(2)

100
New cards

Function definition: Double down

Complete the DoubleDown function to return twice the initialValue.

function finalValue = DoubleDown( initialValue )

% Complete the function to return twice the initialValue

finalValue = 2*initialValue

end

DoubleDown(5) % Change the input to DoubleDown to test other values