Description
Read and execute the commands described Sections 1–11 of Basic Concepts in Matlab and then
answer the following questions. Please create a script in Matlab that performs the calculations
needed to answer each of the questions, one cell for each part of each question. Please submit
your script and diary output file via Moodle. (The diary command in Matlab, see help
diary, is used to record your commands and results to a text file; record your diary only after
you are finished with script and you know that it is working as you intended. Alternatively to use
of the diary, you can “publish” your solution using Matlab.)
1. Given the array x = [3 1 2 9 5 4], provide the single command (namely, one line
of code with at most one assignment (i.e., equals sign =)) needed to perform the following
actions. Unless noted, all of the actions should not modify x; instead, the result of the
action will be assigned to the default variable ans. If x is modified, then the modified x
should be used for subsequent actions.
(a) Extract the third element from x
(b) Extract all but the last element from x
(c) Extract the first, third, first, sixth, and first element from x
(d) Reverse the elements of x
(e) Calculate the sum of all of the elements of x
(f) Calculate the sum from the first to the ith element of x, for all elements 1 to i in x
(g) Modify x by setting the second and sixth elements of x equal to zero
(h) Using the result from (g), modify x by deleting its third element
(i) Using the result from (h), modify x by adding 7 to its end
(j) Using the result from (i), modify x by converting it into a 2 3 matrix, where the first
row of the matrix has the first three elements of x
2
2. Let x = [6 2 1 4] and A = [2 7 9 7; 3 2 5 6; 8 2 1 5], provide the
single command needed to:
(a) Add x to each row of A
(b) Add x to the sum of each column of A
(c) Add twice the sum of x to each element of A
(d) Calculate the element-by-element product of each row of A and x
(e) For each row of A, calculate the sum of the element-by-element product of each row
and x
3. Given that x = [1 5 2 7 9 0 1] and y = [5 1 2 8 0 0 2], provide the
single command needed to:
(a) Extract from x those values that are greater than the corresponding values of y
(b) Extract from x those values that are both greater than the corresponding values of y
and less than 6
(c) Extract from x those values that are either less than 2 or greater than 6
(d) Modify y by adding 1 to each of its nonzero values
(e) Divide each element of y by the corresponding element of x as long as the element of
x is nonzero (to avoid dividing by zero)
(f) Modify y by setting all of its zero values to 1
4. Modify the Minimum-Distance Location example in Basic Concepts so that it can be used
to find the location that minimizes the maximum distance traveled between x and the three
points in P. (Note: you need to create the function mydist discussed in the example.)
5. Modify the Minimum-Distance Location example in Basic Concepts so that it can be used
to find the location that minimizes the sum of distance traveled assuming that 3, 4, and 2
trips are made between x and the three points in P, respectively.
3
6. Write a code to generate the first 10 numbers of the Fibonacci Sequence.
7. Plot the quadratic function, 𝑓(𝑥) = 𝑥
2 + 2𝑥 + 1, for 𝑥 ∈ [−20,20].