The solution of the quadratic equation, a*x2 + b*x + c = 0 (with real-valued coe
ID: 2248728 • Letter: T
Question
The solution of the quadratic equation, a*x2 + b*x + c = 0 (with real-valued coefficients), can be split into three cases (two real-valued roots, a pair of repeated roots, two complex-valued roots). Write a script which prints the form of the equation, prompts the user to input the coefficients, determines which case applies, computes the solution, and prints the solution. Use one or more selection statements to handle the cases. The output, in particular the complex numbers, should be displayed in a "normal" form, i.e. what would be seen in a textbook. Test your script using the following values (listed as triplets (a,b,c)): (2,1,-1), (2,-8,26), (-4,-12,-9). Use MATLAB.
Explanation / Answer
MATLAB CODE:-
clc
clear
disp('aX^2+bX+c = 0')
a = input('a = ');
b = input('b = ');
c = input('c = ');
disp('Roots of equation ')
r1 = (-b + sqrt(b^2 - 4*a*c))/2;
r2 = (-b - sqrt(b^2 - 4*a*c))/2;
if(r1==r2)
disp('a pair of repeated roots')
r1
r2
else if(imag(r1)==0&&imag(r2)==0)
disp('two real value roots')
r1
r2
else
disp('two complex roots')
r1
r2
end
end
OUTPUT:-
1)
(2,1,-1)
aX^2+bX+c = 0
a = 2
b = 1
c = -1
Roots of equation
two real value roots
r1 =
1
r2 =
-2
>>
2) (2,-8,26)
aX^2+bX+c = 0
a = 2
b = -8
c = 26
Roots of equation
two complex roots
r1 =
4.0000 + 6.0000i
r2 =
4.0000 - 6.0000i
3) (-4,-12,-9)
aX^2+bX+c = 0
a = -4
b = -12
c = -9
Roots of equation
a pair of repeated roots
r1 =
6
r2 =
6