function [root1,root2,ier]=quadroots(a,b,c) % % function [root1,root2,ier]=quadroots(a,b,c) % Function to find the roots of the quadratic equation given coefficients a,b, and c. % This function takes no account of scaling. % if a==0 % % Check odd cases when a=0 % if b==0 if c==0 ier='all numbers are roots a=b=c=0'; else ier='no roots a=b=0, c ne 0'; end; else root1=-c/b; root2=root1; ier='one root a=0'; end; else disc= b*b-4*a*c; if disc < 0 % % Two complex roots computed with real arithmetic % im=sqrt(-disc)/(2*a); re=-b/(2*a); root1=re+i*im; root2=re-i*im; else if b==0 % % Since MATLAB handles complex arithmetic without a blink, % we can just use the formula in this case. % root1=sqrt(-c)/a; root2=-root1; else % % Case where there are two real roots. % diff=sqrt(disc)/(2*a); same=-b/(2*a); if same*diff > 0 root1=same+diff; else root1=same-diff; end; root2=c/(a*root1); end; end; ier='roots are good'; end;