Write an MATLAB function Mfile to solve a secondorder polyno
Write an MATLAB function (M-file) to solve a second-order polynomial:
Ax2 + Bx + C = 0
Request input from the user (three numbers: A, B, and C).
Check for real/imaginary roots
Solve mathematically and store the solutions in variables x1 and x2
Return the solutions to the user (x1, x2)
Remember: annotation and comments are very important in the understanding of your code by another (a grader or faculty member) and you will be graded by the functionality of your code AND the quality of your code, including annotation.
Solution
function [] = Quad()
display(\'Please provide values of ccoefficients\')
a = input(\'Input A: \' );
b = input(\'Input B: \');
c = input(\'Input C: \');
Desc = b^2 - 4*a*c
if Desc > 0 % If Descriminant is greater than zero, equation has two real roots
x1 =(-b+ sqrt(b^2-4 *a*c))/(2*a);
x2 = (-b- sqrt (b^2-4 *a*c))/(2*a);
display(\'The given quadratic equation has two real roots\')
sprintf(\' These are %d , %d and Descriminent is %d \', x1,x2,Desc)
elseif Desc == 0 % If Descriminant is zero, equation has one real root
x1 = -b/2*a;
x2 = x1;
display(\'The given quadratic equation has one real root\')
sprintf(\' It is %d , %d and Descriminent is %d \', x1,Desc)
elseif Desc < 0 % If Descriminant is less than zero, equation has no real roots
x1 =(-b+ sqrt(b^2-4 *a*c))/(2*a);
x2 = (-b- sqrt (b^2-4 *a*c))/(2*a);
disp(\'The given quadratic equation has no real roots\')
sprintf(\' Roots are %d , %d and Descriminent is %d \', x1,x2,Desc)
end
end
