Write a program script to solve the quadratic equation ax2bx
Write a program (script) to solve the quadratic equation ax^2+bx+c=0 for any values of user inputs a, b, and c. This problem was discussed in class and a solution is presented in your book in section 3.4.1. The solution in the book is not complete however. Your program should improve on the solution of the book by checking for all input possibilities for a, b, and c including zeros. Book is Matlab Programming for engineering, 4th edition.
Solution
clear all;
a=input(\'Enter a : \');
b=input(\'Enter b : \');
c=input(\'Enter c : \');
if a==0
fprintf(\'This is not a quadratic equation \ \');
else
d=b*b-4*a*c;
if d<0
fprintf(\'The equation does not have real solutions\ But the complex solutions are : \');
elseif d>0
fprintf(\'The equation has 2 solutions \ They are : \');
end
sol1=(-b+sqrt(d))/(2*a);
sol2=(-b-sqrt(d))/(2*a);
if d==0
fprintf(\'The equation has only 1 solution and it is : \');
disp(sol1);
else
disp([num2str(sol1) \' and \' num2str(sol2)]);
end
end
Running the script we get :
Untitled1
Enter a : 2
Enter b : 4
Enter c : 6
The equation does not have real solutions
But the complex solutions are : -1+1.4142i and -1-1.4142i
>> Untitled1
Enter a : 1
Enter b : 2
Enter c : 1
The equation has only 1 solution and it is : -1
>> Untitled1
Enter a : 1
Enter b : -3
Enter c : 2
The equation has 2 solutions
They are : 2 and 1
