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

In (his homework problem, and in all subsequent homework problems, you will part

ID: 3786666 • Letter: I

Question

In (his homework problem, and in all subsequent homework problems, you will part mon your script m-file into sections There will be one section per problem. Because Homework I has two problems, your script m-file for this homework will contain 2 sections. Have MATLAB print (to the command window) the following. e in fixed-point notation to seven decimal places (plus a new line) e in exponential (scientific) notation with five decimal places (plus a new line) -256 in (signed) integer notation (plus a new line) In the above, e is the base of the natural logarithm. MATLAB will generate this number whenever you ask for this: exp(1). (With this statement, you are asking for e^1.) You will use fprintf. You should first read the MATLAB documentation on the function fprintf. Pay particular attention to the forma tSpac portion of the documental ion (On the fprintf documentation page, click on the formatSpuc hyperlink.) In this problem you won't be printing to a file, so you won't supply the optional fileID input If you are familiar with the C/C++ function printf. you will see that the MATLAB fprintf function works in a similar way. The purpose of this assignment is for you to practice formatted printing of numbers. You are asked not to print pure literal strings. For example, don't do this: fprintf('2.7182818 ') % Don't do this! It is true that this statement will produce the desired first line. However, you will not lean the formatted printing of numbers if you do this. Instead, you will ask MATLAB to print exp (1) using a total of 9 characters. 7 of which will be to the right of the decimal place. (There will also be 1 character to the left of the decimal place and the decimal point, making a total of 9 characters.) Use the function sum to do this problem First, create the following matrix: [1 2 3 4 5 6 7 8 9] From this matrix, create a row vector, each element of which is the sum of the elements in one column of the above matrix. For example, the first element in this vector should be 12. Display this row vector. From this matrix, create a column vector, each element of which is the sum of the elements in one row of the above matrix. For example, the first element in this vector should be 6. Display this column vector. Calculate the sum of all elements in the matrix above and display this sum.

Explanation / Answer

Question 1) Matlab script

% e in fixed-point notation to seven decimal place:
fprintf('e = %1.7f in fixed point notation ',exp(1));
% e in exponential notation with five decimal place
fprintf('e = %1.5e in exponential notation ',exp(1));
% - 256 in signed integer notation
fprintf('%d in signed integer notation ',-256);

Output

e = 2.7182818 in fixed point notation
e = 2.71828e+00 in exponential notation
-256 in signed integer notation

Question 2) Matlab Script

A = [1 2 3;4 5 6;7 8 9]; % The given matrix
B = sum(A); % Creating the row vector with colum sum
C = sum(A')'; % Creating a colum vector with row sum
D = sum(sum(A)); % Creating the sum of all the elements in the matrix
fprintf('The given matrix '); disp(A);
fprintf( 'The row vector with colum sum ');disp(B);
fprintf('The colum vector with row sum ');disp(C);
fprintf('The sum of all the elements in the matrix ');disp(D);

OUTPUT

The given matrix
     1     2     3
     4     5     6
     7     8     9

The row vector with colum sum
    12    15    18

The colum vector with row sum
     6
    15
    24

The sum of all the elements in the matrix
    45