I have to code this in Matlab, but a written out version works. The output for t
ID: 3937464 • Letter: I
Question
I have to code this in Matlab, but a written out version works. The output for the code is the second picture. Program 5-Roots (homework) CSCI 251 For program 5, we will revisit program 2, and rewrite it using a function and a main program instead. The two roots of a quadratic equation ax2 +bx +c can be obtained using the following formula: -b+ b2-4ac -b-vb2-4ac 2a 2a b2- 4ac is called the discriminant of the quadratic equation, and its value determines the number of roots as follows: b2-4ac >0, two real roots: rl and r2 b2-4ac-0, one real root:r b2-4acExplanation / Answer
roots_yourLastName.m
function [numOfRoots] = roots_yourLastName( a, b, c)
discriminant = b*b - 4*a*c;
if discriminant < 0
numOfRoots = 0;
elseif discriminant == 0
numOfRoots = 1;
else
numOfRoots = 2;
end
end
rootsTest_yourLastName.m
function [] = rootsTest_yourLastName()
a = input('Enter the A coefficient: ');
b = input('Enter the B coefficient: ');
c = input('Enter the C coefficient: ');
numOfRoots = roots_yourLastName(a,b,c);
fprintf('%d, %d, and %d coefficients have',a,b,c);
discriminant = b*b - 4*a*c;
if numOfRoots==0
fprintf(' no real roots ');
elseif numOfRoots==1
fprintf(' one real root: ');
fprintf(' r=%.2f ', (-b*1.0)/2*a );
else
fprintf(' two real roots: ');
fprintf(' r1 = %.2f ',(-b+sqrt(discriminant)+0.0)/2*a);
fprintf(' r2 = %.2f ',(-b-sqrt(discriminant)+0.0)/2*a);
end
end