You are on page 1of 2

IT131-8L Machine Problem Answer Sheet

Machine
8 (Nested & Recursive Functions) Score
Problem #
Name Alonzo, Isaiah Benjamin Section A11s

1.
Command Window Results:
getreal(1,9,20)
Roots are -4.00 & -5.00
>> getreal(0,1,1)
Invalid Input, a should not be 0
>> getreal(2,9,5)
Roots are -0.65 & -3.85

getreal.m
Script:
function getreal(a,b,c)
%this function solves for the roots using the given input
function in = getinput
%this function checks if a is not equal to 0
if a == 0
in = 0;
disp('Invalid Input, a should not be 0');
else
in = 1;
end
end
function dis = calcdisc
%this function solves for the discriminant of the equation
dis = b^2-(4*a*c);
end
if getinput == 1
x1 = (-b+sqrt(calcdisc))/(2*a);
x2 = (-b-sqrt(calcdisc))/(2*a);
fprintf('Roots are %0.2f & %0.2f\n', x1,x2);
end
end

Prepared by: Geldof Resuello


Prepared date: March 2020
2.
Command Window Results:
combi(1,0)

ans =

>> combi(2,2)

ans =

>> combi(5,4)

ans =

combi.m
Script:
function C = combi(n,m)
%this function solves for the combinatioral coefficients of the given m & n
if m == 0 || m == n
C = 1;
else
C = combi(n-1,m-1) + combi(n-1,m);
end

Prepared by: Geldof Resuello


Prepared date: March 2020

You might also like