Write the program discussed in the problem, but just enter the values in the fil
ID: 2848403 • Letter: W
Question
Write the program discussed in the problem, but just enter the values in the file rather than asking the user to enter them. Run the Program 3 times to find the real roots of the following equations.
a) x^2 + 6x + 2 = 0
b) -2x^2 + 20x - 25 = 0
c) 3x^2 + 6x + 9 = 0
The orgiinal progblem (Pg 211 Problem 11 Matlab):
Write a program in a script file that determines the real roots of a quadratic equation ax^2 + bx + c = 0 . Name the file quadroots . When the file runs, itasks the user to enter the values of the constants a , b , and c . To calculate theroots of the equation the program calculates the discriminant D , given by:
D = b^ 2 - 4ac
If D > 0, the program displays message
Explanation / Answer
array=input('Specify the coefficients of the quadratic as a row array"[a,b,c]":');
a=array(1);b=array(2);c=array(3);
if (a==0) && (b==0)
fprintf('The equation is degenerate');
end
if (a==0) && (b~=0)
fprintf('There is a single root at %g. ', -c/b);
end
if (a~=0) && (c==0)
fprintf('There are two real roots at 0 and %g. ', -b/a);
end
if (a~=0) && (c~=0)
root1=((-b+sqrt(b*b-4*a*c))/(2*a));
root2=((-b-sqrt(b*b-4*a*c))/(2*a));
if isreal(root1)
fprintf('There are two real roots at %g and %g. .', root1,root2);
else
fprintf('The roots are complex: ');
fprintf(' ');
disp(root1);
fprintf(' and ');
disp(root2);
end
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.