Problem-3: If Statement and Infinite Loop The equation ax 2+bx+c=0, called a qua
ID: 2248085 • Letter: P
Question
Problem-3: If Statement and Infinite Loop The equation ax 2+bx+c=0, called a quadratic equation, has solutions provided by the quadratic formula. 2a Write a MATLAB program that takes as input the coefficients a, b, and c of the quadratic equation, then, based on the results of the discriminant D=b2-4ac, produces the solution (s) of the equation Note that if the discriminant is positive, there are two real solutions given by the above formula. On the other hand, if the discriminant is zero, there is only one solution. Finally, if the discriminant is negative, then there are no real solutions. Your program should use an if. .elseif. .else .end conditional and ask the user for the values of a, b, and c The program should run continuously until stopped by the user. Below is a sample run of the program: Enter the value of a: 2 Enter the value of b: 5 Enter the value of c 1 The discriminant (17) is positive. Hence, there are two solutions The first solution is:-2.280776 The second solution is -0.219224 Enter 1 to continue and 0 to stop: 1 Enter the value of a: 1 Enter the value of b:4 Enter the value of c: 8 The discriminant (-16) is negative. Hence, there are no real solutions. Enter 1 t° continue and 0 to stop: 0 Bye!!Explanation / Answer
close all,
clear all,
clc,
Root_1=0;
Root_2=0;
a=0; b=0; c=0;
x=1;
while(x)
prompt = 'Enter the value of a = '; a = input(prompt);
prompt = 'Enter the value of b = '; b = input(prompt);
prompt = 'Enter the value of c = '; c = input(prompt);
a=double(a);
b=double(b);
c=double(c);
D = double(power(b,2) - (4*a*c));
if(D < 0)
Root_1 = ((-b) + sqrt(D))/(2*a);
Root_2 = ((-b) - sqrt(D))/(2*a);
str = strcat('(b^2-4ac) = ',num2str(D),', i.e. = -ve, Therefore, the discriminant (',num2str(D),') is -ve.'); disp(str);
str = strcat('Hence, there is no real solution, but have two imaginary roots.'); disp(str);
% str = strcat('The first imaginary solution is ; ',num2str(Root_1)'); disp(str);
% str = strcat('The second imaginary solution is; ',num2str(Root_2)'); disp(str);
end
if(D==0)
Root_1 = (-b)/(2*a);
Root_2 = 0;
str = strcat('(b^2-4ac) = ',num2str(D),', i.e. = 0, Therefore, the discriminant (',num2str(D),') is zero.'); disp(str);
str = strcat('Hence, there is only one solution (real roots).'); disp(str);
str = strcat('And the solution is ; ',num2str(Root_1)'); disp(str);
disp(str);
end
if(D > 0)
Root_1 = ((-b) + sqrt(D))/(2*a);
Root_2 = ((-b) - sqrt(D))/(2*a);
str = strcat('(b^2-4ac) = ',num2str(D),', i.e. > 0, Therefore, the discriminant (',num2str(D),') is +ve.'); disp(str);
str = strcat('Hence, there are two solutions (two real roots).'); disp(str);
str = strcat('The first solution is ; ',num2str(Root_1)'); disp(str);
str = strcat('The second solution is; ',num2str(Root_2)'); disp(str);
end
prompt=('Enter 1 to continue or 0 to exit ');
x = input(prompt);
clc,
if(x==0),
break;
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.