Вы находитесь на странице: 1из 3

4.

Roots of Quadratic Equation


MATLAB Script File
clc; clear all; close all
disp('For the equation ax^2+bx+c')
a = input('Enter a:');
b = input('Enter b:');
c = input('Enter c:');
D = b^2-4*a*c;
x = -5:0.1:5;
eq = (a*x.^2)+(b.*x)+c;
if D<0
fprintf('\nThe equation has no real roots.\n\n')
elseif D==0
root = -b/(2*a);
fprintf('\nThe equation has equal real roots.\n')
fprintf(' %.3f\n\n',root)
else
x1 = (-b+sqrt(D))/(2*a);
x2 = (-b-sqrt(D))/(2*a);
fprintf('\nThe equation has two roots.\n')
fprintf(' %.3f and %.3f\n\n',x1,x2)
end
plot(x,eq)
grid on

In the below table, equal roots are colored yellow while real roots are colored sky
blue and non-real roots are uncolored.

Equations Roots Graph

6 x2  5x  11  0 -1.83 and 1
x2  6 x  9  0 3

4 x 2  3x  8  0 Not Real

4 x2  12 x  9  0 1.5
-0.333 and
6 x2  13x  5  0 2.5

3x 2  8 x  5  0 1.667 and 1

Вам также может понравиться