













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!














What would the statement "myNum = 2 + 2" store in myNum? - ANSWER 4 Consider you want to generate, programmatically a vector of with 2 entries; whose 1stentry starting at 0 and ends at 1. How would you create this vector in MATLAB? - ANSWER x = linspace(0,1,2) Whats another way to do this? - ANSWER 0:1: Now Suppose you want to automatically create a vector of 11 entries; the 1st entrystarting at 0 and ends at 1. How would you accomplish this in MATLAB? - ANSWER x = linspace(0,1,11) Whats another way to do this? - ANSWER 0:1: Now Suppose you want to automatically generate a vector with 8 entries; the 1st entrystarting at 0 and ends at 3. How would you create this vector in MATLAB? - ANSWER x = linspace(0,3,8) or x = 0:3: How would you access the first twelve elements for y? - ANSWER y = (1:12); x(1:12) OR y = (1:12); x(1:12)
How would you access the 1st four multiples of 3 specified in a vector? - ANSWER y([3 69 12])
Write a statement transposing x - ANSWER x' Write two statements that notes the difference in tranpose of z - ANSWER z' and z.' 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 - ANSWER n = anu number; u = linspace(0, 2pin);v = cos(u); plot(u,v) How would you clean up this plot? - ANSWER By plotting axis label and a title; the textinside 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 Give a statement that would transpose a 3x4 matrix - ANSWER x = [1 2 3 4; 5 6 7 8; 9 1011 12]; 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
Only C will appear. Both C and K will display. The statement produces an error. - ANSWER Statements have to be complete andseparated by semicolon or comma, otherwise the line is considered as one single statement, that in this case is not a valid statement, thus producing error. Matt Labler is a beginning MATLAB programmer. He wants to have variable A be set to6, B be set to 9 and C to be the sum of A and B, but he only wants to see the values of A and C in the command window. He would like you to look at this statement for bugs: A =6; B = 9; C = A + B.
Matt should use "A = 6, B = 9; C = A + B" instead. Matt should use "A =6; B = 9, C = A + B;" instead. - ANSWER Matt should use "A = 6, B =9; C = A + B" instead.
X = 39; Y = 41, Z = X + 17
The statement returns an error. Only Y appears.
Only Y and Z appear. - ANSWER only Y and Z appear Matlab challenge exercises Doule x and put the result into y. Ex: If x=2, then output y=4. - ANSWER x=2; y=x+ A bit is a 0 or 1, and is determined from voltage. True False - ANSWER true1 is usally a high voltage (e.g., 2.0 V), 0 is usally a low voltage (e.g., 0 V).
A processor is a circuit that executes instructions. TRUE FALSE - ANSWER true A memory is a circuit that stores one machine instruction. TRUE FALSE - ANSWER false
luckyNumber = 7; luckyNumber = 4; ANSWER 4 The second write to luckyNumber erases the first value written into the variable. Assume apples now has the value of 5 and oranges now has the value of 9. What value isstored in apples after this statement?
apples = apples + 1; ANSWER 6 5 + 1 = 6 Suppose apples has the current value 5 and oranges has the value 9. What value isstored in apples after the following statements?
apples = oranges; oranges = oranges + 1; - ANSWER 9 Write a statement, ending with " - 1;", that decreases the value stored in apples by 1. -ANSWER apples = apples - 1;
Write two statements: one to declare and initialize apples to 7; the other to declare andinitialize oranges to apples plus one. The program should end with a semicolon after each statement. ANSWER apples = 7; or oranges = apples + 1; "apples = 7" has to be runfirst, then "oranges = apples + 1" will set 7+1 or 8 into oranges
Given x = 22 and y = 99. What is x and y after the code below?
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. - ANSWER x is 99 and y is 99. x = y; y = x;> x = y;
x is 99 and y is 22. x is 99 and y is 99. x = 22 and y = 22. - ANSWER x = 99 and y = 99.
tempVal = x;>> x = y; y = x; x = 99 and y = 22. x = 99 and y = 99. - ANSWER x = 99 and y = 99.
Which of the following are valid identifiers?
True False - ANSWER false The range of a double-precision number is about 10 times greater than asingle-precision number's range.
True False - ANSWER false
MATLABs default floating-point representation is capable of representing variousnumbers in the range of -1.79e+308 to +1.79e+308. - ANSWER true
The smallest positive number that can be stored in the default MATLAB floatingpointrepresentation is 1.1755e-
True False - ANS false Suppress the display of the results of all assignment statements. 1)Give a statement that declares the double precision floating-point variable tempKelvin and initialize it with 0.0. - ANSWER tempKelvin = 0.0; Declare and initialize the variable celsiusVal to have the value of kelvinVal minus 273.15.- ANSWER celsiusVal = kelvinVal - 273.15;
Use notation such as 6.02e23 and starting with 3.1 write a statement that assignsvariable wrenchTorque the value 3125.99. - ANSWER wrenchTorque = 3.12599e3;
Complex numbers. Assume that i and j are pre-defined values for the imaginary part of an imaginarynumber.
1)If numA = i * i then what is the result? - ANSWER -
circleArea = picircleRadiuscircleRadius Evaluating expressions based on precedence rules.Note: This exercise does not use parentheses to test knowledge of precedence rules. However programmers should use parentheses to make the order of evaluation explicit.
Does 2 / 2 * 3 = 2/6? - ANSWER Incorrect / and * have the same precedence so evaluated from left to right, so (2/2)3, which is 13,giving 3.
Does 2 / 3 ^2 = 4/9? - ANSWER Incorrect Exponent higher precedence, so 2/(3^2) which is 2/9. Is 2 + 3 * 4 - 4 = 16? - ANSWER Wrong
Precedence rules. Select the expression whose parentheses force the compiler to do the evaluations in theorder of the original expression. Note: It is usually better to include such parentheses rather than depend on precedence rules. y + 2 * z - ANSWER y + (2 * z) z / 2-x - ANSWER (z / 2) - x x * y * z - ANSWER (x * y) * z x + y ^ 3 - ANSWER x + (y ^ 3) x + 1 * y / 2 - ANSWER x + ((1 * y) / 2) x / 2 + y / 2 - ANSWER (x / 2) + (y / 2) What is totCount after executing the following? numItems = 5; totCount = 1 + (2 * numItems) * 4; - ANSWER 44 After (2 * 5) is evaluated, * 4 is evaluated because * has precedence over + Several statements on a line. 1)What is val after executing the line num = 6; num = num * 2; val = num + 1; - ANSWER 13 num will be assigned with 6 * 2 or 12. Then val will be assigned with 12 + 1 or 13.
What is the next number after 34 in the Fibonacci rabbits example? - ANSWER 55 Characters. 1)Create a statement that declares variable delimChar to hold the comma character. Do not forget the semi-colon - ANSWER delimChar = ', '; Given x='a'; y='b', assign x with the current value of y. End with a semicolon. - ANSWER x= y;
What is the decimal number stored in a char variable delimChar after the followingstatement: delimChar = 'a'; - ANSWER 97
Declaring a character 'T';Write a statement that assigns middleInitial with the character T. - ANSWER myChar = middleInitial = myChar String scalars and character vectors.
Assign variable unitName with character vector: miles - ANSWER unitName = 'miles'; Assign variable unitName with string scalar: nautical miles - ANSWER unitName ='nautical miles';
Assign variable allowBoat with character vector: OK'ed - ANSWER allowBoat = 'OK''ed'; Assign variable phrase with string scalar: Isn't it OK? - ANSWER "Isn't it OK?"
Write a statement that assigns the logical variable isAvailable with the logical value'true'. - ANSWER isAvailable = true
Consider the following script, which is saved in file CostDrive.m: miles = 100; costPerMile = 0.30;cost = miles * costPerMile;
What is the command line statement that would run the script? - ANSWER CostDrive(omit the .m)
Assume that the workspace has not been assigned any variables as yet. Which of thefollowing would calculate the average speed in miles per hour for 56.9 miles driven in 73 minutes? - ANSWER distanceMiles = 56.9; timeSecs = 73*60; CalculateMPH Assume that the workspace has not been assigned any variables as yet. How manyvariables would be created by the execution of the following code: distancePes = 1000; timeSecundae = 55; RomanUnitsToMPH - ANSWER 8 Consider distanceMiles = 1870, timeSecs = 343440, and speedMPH = 55.0. After thefunction call CalculateMPH what does speedMPH =? - ANSWER 19.
What is the only character that identifies this as a comment? - ANSWER % Write the first line of a function definition for a function named CubeNum that has inputxVal and output yVal. - ANSWER function yVal = CubeNum (xVal)
This should be a statement that assigns variable myNum with the result of a call to the
end DoubleDown(5) % Change the input to DoubleDown to test other values Function definition: Volume of a pyramidDefine a function CalculatePyramidVolume that takes inputs baseLength, baseWidth and pyramidHeight. The function returns pyramidVolume - the volume of a pyramid witha rectangular base. Hint: Remember that a function declaration starts with 'function'. Be careful about variable spelling and case sensitivity. Ex: pyramidvalue and pyramidValueare different variables. Relevant geometry formula: pyramidVolume = baseLength * baseWidth * pyramidHeight* 1/ Ex: CalculatePyramidVolume(1, 1, 1)returns ans = 0.3333 - ANSWER function pyramidVolume = CalculatePyramidVolume(baseLength,baseWidth, pyramidHeight) pyramidVolume = baseLength * baseWidth * pyramidHeight * (1/3) end %function declaration
% Assign totalCost with the discounted cost of items 1 and 2 totalCost = DiscountedPrice(item1Cost, item1Discount) + DiscountedPrice(item2Cost,
item2Discount)
end Fill in the blank: This statement asks the user for input with the prompt 'Enter your age: 'and sets the variable userAge to the value the user types in. ANSWER userAge = input('Enter your age: ') Fill in the blank so that this function call reads in a string rather than a number. userName = input('Sally', ______________ ); ANSWER 's' 's' tells python to read the users input as a character vector -- even if the user types anumber.
Unformatted output. Write a statement that would write to the screen using the function disp. The statementshould end with a semicolon.
1)Display on screen the value of a variable named distance. - ANSWER disp(distance);
Format operator %f for floating-point output.1)