Reference no: EM132463273
CEE/GLE 291 Problem Solving using Computer Tools Assignment - University of Wisconsin-Madison, USA
MATLAB Problems - Complete the following problems. Submit the code for each function. When submitting the code for a function, make sure the file is named accordingly.
Problem 1 - Write a function named getEnds which receives one vector as input. The function must return a vector containing two elements. The first element of the returned vector must be the first element of the input vector. The second element of the returned vector must be the last element of the input vector. You can assume that the input vector contains at least two elements.
Required Function Name and Parameters
getEnds(nums)
Function Call
|
Returned by the Function
|
getEnds([6,2,7,4,5,12,54,87,10,7])
|
[6, 7]
|
getEnds([4,7])
|
[4, 7]
|
getEnds([-5,-10, 4])
|
[-5, 4]
|
Problem 2 - Write a function named getMaxBelow which receives one matrix and one scalar as input. The matrix (myMatrix) contains positive and negative numbers. The scalar (limit) is an integer. The function must return the highest number in the matrix that is lower than the integer passed as the limit parameter. You can assume that at least one number in the matrix will be lower than the integer passed as the limit parameter.
Required Function Name and Parameters
getMaxBelow(myMatrix, limit)
Function Call
|
Returned by the Function
|
getMaxBelow([6,2,7,4,5,12,54,87,10,7], 50)
|
[12]
|
getMaxBelow([7], 10)
|
[7]
|
getMaxBelow([-7, -8, -3], 0)
|
[-3]
|
Problem 3 - Write a function named getDistanceToPoint which receives two vectors as input. Both vectors contain the coordinates two different points in [x, y] form. When called, the function must return the distance between the two points.
Required Function Name and Parameters
getDistanceToPoint (p1, p1)
Function Call
|
Returned by the Function
|
getDistanceToPoint([0,0], [0,1])
|
[1]
|
Problem 4 - Write a function named getDecimalAngle which receives three numbers as input. The first number (d) represents the degrees associated with the DMS representation of an angle. The second number (m) represents the minutes while the third number (s) represents the seconds. When called, the function must return the decimal form of the angle.
Required Function Name and Parameters
getDecimalAngle (d, m, s)
Function Call
|
Returned by the Function
|
getDecimalAngle(45, 30, 30.12)
|
[45.50836]
|
getDecimalAngle(15, 0, 10)
|
[15.00277]
|
Problem 5 - Write a function named getCappedValue which receives two numbers as input. If the first number received (n1) is less than or equal to the second number (n2) then the function must return n1. However, if n1 is greater than n2 then the function must return n2.
Required Function Name and Parameters
getCappedValue(n1, n2)
Function Call
|
Returned by the Function
|
getCappedValue(1, 3)
|
[1]
|
getCappedValue(5, 3)
|
[3]
|
getCappedValue(4.9999, 5)
|
[4.9999]
|
Problem 6 - Write a function named getAzimuth which receives two vectors as input. Both vectors (p1 and p2) represent a point in [x,y,z] form. The function must return the azimuth of the line in decimal form.
Required Function Name and Parameters
getAzimuth(n1, n2)
Function Call
|
Returned by the Function
|
getAzimuth([0,0,0],[1,1,1])
|
[45.0000]
|
getAzimuth([0,0,0],[0,1,1])
|
[0.0000]
|
getAzimuth([0,0,0],[-1,1,1])
|
[315.0000]
|
Problem 7 - Write a function named getDMS which receives one number as input. The number received by the function is an angle in decimal form. The function must return a 3-by-1 matrix (i.e. a vector) in which the first element represents the degrees, the second the minutes, and the third the seconds of the DMS representation of the angle.
Required Function Name and Parameters
getDMS(angle)
Function Call
|
Returned by the Function
|
getDMS(20.5)
|
[20, 30, 0]
|
getDMS(50.51)
|
[50, 30, 36]
|
getDMS(75.50275)
|
[75, 30, 9.9]
|
Problem 8 - Write a function named getAngle which receives two numbers as input. The first number (a) represents the adjacent side of a 90-deg a triangle while the second number (b) represents the opposite side. The function must return the angle (in decimal form) associated with side lengths passed as input to the function.
Required Function Name and Parameters
getAngle(a, b)
Function Call
|
Returned by the Function
|
getAngle(2, 2)
|
[45.0000]
|
getAngle(2, 4)
|
[26.5651]
|
Problem 9 - Write a function named getDivision which receives two numbers as input. The first number (n1) will be divided by the second number (n2). The function must return the division of the two numbers. However, if the division results in an Infinite (Inf) value then the function must return 1.
Required Function Name and Parameters
getDivision(n1, n2)
Function Call
|
Returned by the Function
|
getDivision(2, 2)
|
[1]
|
getDivision(2, 4)
|
[0.5]
|
getDivision(1, 0)
|
[1]
|
Problem 10 - Write a function named replaceValue which receives one matrix and two numbers as input. The function replaces instances of the first number (n1) in the matrix (m) with the second number (n2). The function must return the modified version of the matrix.
Required Function Name and Parameters
replaceValue(m, n1, n2)
Function Call
|
Returned by the Function
|
replaceValue([1,2;1,3], 1, 0)
|
[0,2;0,3]
|
replaceValue([1,2;1,3], 2, 8)
|
[1,8;1,3]
|
Problem 11 - Write a function named getColumnSum which receives one matrix and a number as input. The number (j) passed to the function represents a column in the matrix (m). The function must return the sum of the numbers in the specified column.
Required Function Name and Parameters
getColumnSum(m, j)
Function Call
|
Returned by the Function
|
getColumnSum([8,2;1,3], 1)
|
[9]
|
getColumnSum([1,2;1,3], 2)
|
[5]
|
Problem 12 - Write a function named getRowSum which receives one matrix and a number as input. The number (i) passed to the function represents a row in the matrix (m). The function must return the sum of the numbers in the specified row.
Required Function Name and Parameters
getRowSum(m, i)
Function Call
|
Returned by the Function
|
getRowSum([8,2;1,3], 1)
|
[10]
|
getRowSum([1,2;1,3], 2)
|
[4]
|
Problem 13 - Write a function named getLastItem which receives one vector as input. The function must return the last number in the vector.
Required Function Name and Parameters
getLastItem(v)
Function Call
|
Returned by the Function
|
getLastItem([8,2,1,3])
|
[3]
|
getLastItem([8,2,1,3,6])
|
[6]
|
Problem 14 - Write a function named addToEndOfVector which receives one vector and a number as input. The function must return a modified version of the vector (v) in which the number (num) has been added as the last element. The function must work even if the vector passed is empty.
Required Function Name and Parameters
addToEndOfVector(v, num)
Function Call
|
Returned by the Function
|
addToEndOfVector([8,2,1,3], 7)
|
[8,2,1,3,7]
|
addToEndOfVector([], 7)
|
[7]
|
Problem 15 - Write a function named getAscending which receives one vector as input. The function must return a modified version of the vector (v) in which the numbers are in ascending order.
Required Function Name and Parameters
getAscending(v)
Function Call
|
Returned by the Function
|
getAscending([8,2,1,3])
|
[1,2,3,8]
|
getAscending([8,2,1,3,4,4,9])
|
[1,2,3,4,4,8,9]
|
Problem 16 - Write a function named deleteColumn which receives one matrix and one number as input. The function must return a modified version of the matrix (m) with the column specified by the number (i) removed. If passed an n-by-1 matrix (i.e. a vector) and asked to delete the only column then it must return [ ] not a 3x0 empty matrix.
Required Function Name and Parameters
deleteColumn(m, i)
Function Call
|
Returned by the Function
|
deleteColumn([8,2;1,3], 1)
|
[2; 3]
|
deleteColumn([8,2,0;1,0,3], 2)
|
[8, 0; 1, 3]
|
deleteColumn([1;5;7], 1)
|
[]
|
Problem 17 - Write a function named getWithDecimals which receives two numbers as input. The first number (n1) is an arbitrary number and the second number (n2) represents a desired number of decimal places. The function must return the character array representation of n1 using n2 decimal places. Note: the process of creating a character array from multiple ones is called concatenation. In MATLAB the use of the term character array and string is often used in the same context but technically speaking these two are not the same, a common source of confusion. To the extent possible, we will use character arrays.
Required Function Name and Parameters
getWithDecimals(n1, n2)
Function Call
|
Returned by the Function
|
getWithDecimals (3.1416, 3)
|
'3.142'
|
getWithDecimals (7.23, 4)
|
'7.2300'
|
getWithDecimals (6.43, 0)
|
'6'
|
Problem 18 - Write a function named characterCount which receives a 1-by-n character array (a) and a 1-by-1 character array (c). The function must return the number of times that the character c is found in a.
Required Function Name and Parameters
characterCount(a, c)
Function Call
|
Returned by the Function
|
characterCount('abcdaa', 'a')
|
[3]
|
characterCount('abca', 'x')
|
[0]
|
Problem 19 - Write a function named flipCase which receives a character array (a) as input. The function must return a modified version of that character array in which uppercase letters become lowercase and lowercase letters become uppercase. You can assume that the only A-Z or a-z characters will be part the character array passed to the function. Finally, the result of int8('a') is 97 and the result of int8('A') is 65. Similarly, the result of char(65) is 'A' and the result of char(97) is 'a'.
Required Function Name and Parameters
flipCase(a)
Function Call
|
Returned by the Function
|
flipCase('aBc')
|
'AbC'
|
flipCase('adc')
|
'ADC'
|
flipCase('AAc')
|
'aaC'
|
Problem 20 - Write a function named getDistanceToPlane that receives four 1-by-3 matrices (i.e. vectors) named b, r, s, and t. Each vector represents the x-y-z coordinates of a point in space. The points represented by b, r, and s are on a plane. Your function must return the absolute value of the perpendicular distance between the point represented by t and the plane defined by b, r, and s.
Required Function Name and Parameters
getDistanceToPlane(b, r, s, t)
Function Call
|
Returned by the Function
|
getDistanceToPlane([0,0,0], [0,1,0], [1,0,0], [1,1,1])
|
[1]
|