Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Write a MATLAB® expression that creates the 5-element row vector, x = [1 3 5

ID: 2079667 • Letter: 1

Question

1) Write a MATLAB® expression that creates the 5-element row vector, x = [1 3 5 7 9] by using the colon operator
instead of listing out each number.

2) Write a MATLAB® expression that sets y equal to the transpose of the vector x.

3) Assuming x and y are both vectors, translate the following into MATLAB®

4) Write MATLAB® code to implement the following structure plan steps:
a: Set x equal to a uniform random number between 0 and 1

4) Write MATLAB® code to implement the following structure plan steps:
b: If x is less than 0.33, then set y equal to the square of x

4) Write MATLAB® code to implement the following structure plan steps:
c: Else if x is less than 0.67, then set y equal to the tangent of x

4) Write MATLAB® code to implement the following structure plan steps:
d: Else set y equal to the cosine of x

Interactive Quiz - Question 4-e
4) Write MATLAB® code to implement the following structure plan steps:
e: Close the IF loop

5) Assuming that the variable ‘y’ has already been initially set to zero, write a MATLAB® FOR
loop to add up the integers from 10 to 20. Use “k” as the loop variable and use “y” to hold the sum
as it is being accumulated. Note: Do not put multiple statements on a single line.

3) Assuming x and y are both vectors, translate the following into MATLAB®

Explanation / Answer

(1)

x=1:2:9

(2)

y=x'

(4)

x=rand(1,10)

for i=1:1:length(x)

if x(i)<0.33

y(i)=x(1,i)^2;

elseif x(i) <0.67

y(i)=tan(x(1,i));

else

y(i)=cos(x(1,i));

end

end

y=y'

OUTPUT:

x =
Columns 1 through 9
0.8407 0.2543 0.8143 0.2435 0.9293 0.3500 0.1966 0.2511 0.6160
Column 10
0.4733
y =
Columns 1 through 9
0.6669 0.0647 0.6864 0.0593 0.5984 0.3650 0.0386 0.0630 0.7080
Column 10
0.5121

(5)

y=0;

for k=10:20

y=y+k;

end

y

OUTPUT:

y =
165

(6)

a = [2 6 3 7 5 4 12 5 8];

b=a-6

OUTPUT

b =
-4 0 -3 1 -1 -2 6 -1 2

(3)

x=rand(1,10)

y=2*sqrt(cos(x.^3))./(exp(x)-exp(-x))

OUTPUT:

x =
Columns 1 through 9
0.7803 0.3897 0.2417 0.4039 0.0965 0.1320 0.9421 0.9561 0.5752
Column 10
0.0598
y =
Columns 1 through 9
1.0942 2.4998 4.0973 2.4071 10.3515 7.5553 0.7528 0.7226 1.6313
Column 10
16.7182