Write a MATLAB program to find the roots of a second order p
Solution
disp(\"This program calculates the roots of a given quadratic equation.\");
disp(\"Quadratic Equation are of form ax^2 + bx + c\");
a = input(\"Enter a of quadratic Equation : \");
b = input(\"Enter b of quadratic Equation : \");
c = input(\"Enter c of quadratic Equation : \");
if (a > 0) && (b > 0) && (c > 0)
d = (b*b) - (4*a*c);
if d > 0
% Real and Distinct roots
fprintf(\'Roots are real and distinct\ \' );
r1 = (-b + sqrt(d))/(2*a);
r2 = (-b - sqrt(d))/(2*a);
fprintf(\'Root 1 = %i\ Root 2 = %i\ \',r1,r2 );
elseif( d == 0 )
% Real and Equal roots
fprintf(\'Roots are repeated\ \' );
r = (-b)/(2*a);
fprintf(\'Root 1 = Root 2 = %i\ \',r );
else
% Imaginary Roots
fprintf(\'Roots are complex\ \');
d = (4*a*c) - (b*b);
r = (-b)/(2*a);
ri = sqrt(d)/(2*a);
fprintf(\'Root 1 = %i + (%i)i\ Root 2 = %i - (%i)i\ \',r,ri,r,ri );
end
else
fprintf(\'Error..!! Value of a or b or c is zero\ \');
end
