Write a MATLAB program to find the roots of a second order polynomial equation o
ID: 3940127 • Letter: W
Question
Write a MATLAB program to find the roots of a second order polynomial equation of the form ax^2 + bx + c = 0. Recall that the quadratic equation can be used to find the roots as follows: Root 1, Root2 =- b plusminus squareroot b^2 - 4 middot a middot c/2a Also recall that the roots may be real and distinct, repeated, or complex as described below. Real, distinct roots(b^2 > 4 middot a middot c): Root 1 = -b + squareroot b^2 - 4 middot a middot c/2a Root 2 = -b- squareroot b^2 - 4 middot a middot c/2a Repeated roots (b^2 = 4 middot a middot c): Root 1 = Root 2 = -b/2a Complex roots(b^2Explanation / Answer
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.