You are on page 1of 2

9/25/12 7:31 PM MATLAB Command Window 1 of 2

% Script file: Q3quadratic formula.m


%
% Purpose:
% using quadratic formula evaluate the roots of a equation.
%
% Record of revisions:
% Date Programmer Description of change
% ==== ========== =====================
% 26/09/2012 Ghulam Mohy-ud-din Original code
%
% Define variables:
% a -- co-efficient of square term of equation.
% b -- co-efficient of degree 1 term of equation.
% c -- co-efficient of degree 0 term of equation.
% x1 -- root of the equation.
% x2 -- root of the equation.
% discriminant -- value of the discrimnent.

%taking input from user.


a=input('enter co-efficient a of the quadratic forumla:');
b=input('enter co-efficient b of the quadratic forumla:');
c=input('enter co-efficient c of the quadratic forumla:');

%evaluating and printing discriminent.


discriminant=b^2-(4*a*c);
fprintf('DISC value is =%f\n ',discriminant);
%if disc is positive real roots are evaluated and displayed.
if (discriminant>0)
x1=((-b+sqrt(discriminant))/(2*a));
x2=((-b-sqrt(discriminant))/(2*a));
disp('The equation has real roots.')
fprintf('The roots are %f \n',x1,x2)
%if disc is 0 real and equal roots are evaluated and displayed.
elseif (discriminant==0)
x1=-b / ( 2 * a );

fprintf('The roots are %f and %f\n',x1,x1)


disp('Equation has real and equal roots.')
%%if disc is negative complex roots are evaluated and displayed.
else

real_part=-b/(2*a);
imag_part=sqrt( abs ( discriminant ) ) / ( 2 * a );
fprintf('The real part=%f \nThe imaginary part=%f \n',real_part, imag_part)
disp('Equation has complex roots.')
end
enter co-efficient a of the quadratic forumla:8
enter co-efficient b of the quadratic forumla:9
enter co-efficient c of the quadratic forumla:7
DISC value is =-143.000000
The real part=-0.562500
The imaginary part=0.747391
9/25/12 7:31 PM MATLAB Command Window 2 of 2

Equation has complex roots.

You might also like