ENGR 1204 Programming Languages in Engineering
MATLAB Lab 2
Note: For all labs and assignments copy and paste into a Word file your program (script file), the relevant command window and plots if applicable. Try to condense the print-out as best as possible.
Write a MATLAB program (.M script file) to use the quadratic formula to solve for the roots of the equation
a x^2 + b x + c = 0
Recall there are three cases, depending on the value of the discriminant
disc = b^2 - 4 a c
Set up your program to allow you to enter numerical values for the coefficients a, b and c.
Use the elseif selection statement to display one of the three messages below, followed by the solution for the roots:
Two real roots exist.
Double equal root exists.
Two complex conjugate roots exist.
Note that if the roots are complex, MATLAB will automatically display them in complex form (using "i", the unit imaginary quantity).
A sample output is shown below:
Enter quadratic equation coefficient a: 2
Enter quadratic equation coefficient b: 5
Enter quadratic equation coefficient c: 4
Two complex conjugate roots exist.
The roots are:
x1 =
-1.2500 + 0.6614i
x2 =
-1.2500 - 0.6614i
Run your program for the three cases shown:
(1) a = 1, b = 4, c = 2
(2) a = 1, b = 4, c = 4
(3) a = 1, b = 4, c = 8