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

Matlab code assistance - task 1 and 6 I think I am right and just need to be poi

ID: 3785595 • Letter: M

Question

Matlab code assistance - task 1 and 6 I think I am right and just need to be pointed in the right direction if I should change anything

%% Task 1

% create the 3 row vectors shown in Ex 8 using 3 different approaches

Ex 8 = using the colon operator and also the linspace function, create the following row vectors

-5 -4 -3 -2 -1

5 7 9

8 6 4

% 1. list the values explicitly (square brackets)

vector1=[-5 -4 -3 -2 -1]

% 2. use the colon operator

vector2=

% 3. use the linspace function

vector3=

.

%% Task 6

% Step 1: Assign to myc the current value for time (use the built-in value, clock)

myc = clock;

% Step 2: Store the first three elements of myc in a variable named today

today = myc(1:3);

% Step 3: Store the last three elements of myc in a variable named now

now = myc(length(myc)-2:end);

% Step 4: Use the fix function on now to get the integer portion of time

int_part=fix(now);

.

%% Task 7

% create a 3d matrix having dimensions 2 x 4 x 3.

% Step 1: use the zeros function to initialize the 2 x 4 x 3 matrix

% Step 2: update mat so that the 3rd column of the 2nd layer equals 5.

% Step 3: update mat so that the element in row 2, col 4, layer 2 equals 8.

% Step 4: update mat so that row 2 on layer 2 equals 80% of its former

% value

.

%% Task 8

% Compute the sum of the first 79 terms of the harmonic series shown in Exercise 30.

% Step 1: Use the ones function to create numer, a row vector of 79 ones.

% Step 2: Create denom, a vector of the integers from 1 to 79.

% Do NOT explicitly list 79 values!

% Step 3: Use the ./ operator to compute the quotient of each element pair

% Step 4: Compute the sum of the quotients

Explanation / Answer

Task 1

vector1=[-5 -4 -3 -2 -1];
vector2 =5:2:9;
vector3 = linspace(8,4,3);

Matlab Result

vector1 =

-5    -4    -3    -2    -1
vector2 =

     5     7     9
vector3 =

     8     6     4

Task 6

Your code is perfact

Task 7

% Step 1: use the zeros function to initialize the 2 x 4 x 3 matrix
mat = zeros(2,4,3);
% Step 2: update mat so that the 3rd column of the 2nd layer equals 5.
mat(:,3,2)=5;
% Step 3: update mat so that the element in row 2, col 4, layer 2 equals 8.
mat(2,4,2) = 8;
% Step 4: update mat so that row 2 on layer 2 equals 80% of its former value
mat(2,:,2) = mat(2,:,2)*80/100;

Final mat

mat(:,:,1) =

     0     0     0     0
     0     0     0     0


mat(:,:,2) =

         0         0    5.0000         0
         0         0    4.0000    6.4000


mat(:,:,3) =

     0     0     0     0
     0     0     0     0

Task 8

% Step 1: Use the ones function to create numer, a row vector of 79 ones.
vect = ones(1,79);
% Step 2: Create denom, a vector of the integers from 1 to 79.
denom = 1:79;
% Step 3: Use the ./ operator to compute the quotient of each element pair
quotients = vect./denom;
% Step 4: Compute the sum of the quotients
s = sum(quotients);