Function: Output

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Q6

function grades = letterGrade(A )

% get the size of input matrix A


[r,c] = size(A); % r=row and c=column
% pre assign the output matrix grades of type cell
grades = cell(r,2);

% loop through rows [1 to r]


for i= 1:r

% calculate numericGrade (average) of each row


numericGrade = sum(A(i,:))/c;
% assign numericGrade to first column of grades
grades{i,1}= numericGrade;

% check the condition of numericGrade value


if numericGrade >= 80 && numericGrade <= 100
% assign letter grades to second column of grades
grades{i,2} ='A';

elseif numericGrade >= 60 && numericGrade < 80


grades{i,2} ='B';

elseif numericGrade >= 40 && numericGrade < 60


grades{i,2} ='C';

else
grades{i,2} ='F';

end % end of if

end % end of for loop

end % end of function

OUTPUT

>> A = [80 90 100; 75 45 60 ; 20 20 20 ; 15 20 25; 100 100 100; 60 60 60;50 50 50]

A=

80 90 100
75 45 60
20 20 20
15 20 25
100 100 100
60 60 60
50 50 50

>> grades = letterGrade(A )


grades =

[ 90] 'A'
[ 60] 'B'
[ 20] 'F'
[ 20] 'F'
[100] 'A'
[ 60] 'B'
[ 50] 'C'

>>

Q7

function [y, N] = myexp(x, etol)

% format for long decimal representation


format long;

N=0 ; % degree of Taylor polynomial


y=0 ; % estimate of e(x)
RN= 1000; % random reminder value to enter the loop

% loop till |RN| < etol


while( RN > etol)

% calculate the reminder term


if(x >= 0)
RN = abs((2.8^x* x^(N+1) )/ factorial(N+1));
else
RN = abs((x^(N+1) )/ factorial(N+1));
end

% evaluate the resulting estimate of e(x) for the Nth term


% and add to the total i.e y
y =y+ x^N/factorial(N);

% increase the degree by one


N=N+1;

end
% decrease the degree by one to compensate the last increment of N
% before coming out of the while loop.
N=N-1;

End

OUTPUT

>> [y, N] = myexp(1, 10^-8)


y=

2.718281826198493

N=

11

>> [y, N] = myexp(-1, 10^-8)

y=

0.367879439233606

N=

11

>> [y, N] = myexp(3, 10^-8)

y=

20.085536922950844

N=

20

>> [y, N] = myexp(-3, 10^-8)

y=

0.049787076669269

N=

18

You might also like