Question
P10-04: Solving quadratic equations Recall that quadratic equations are of the form Typically the solution is two real roots x1 and x2 . Example :
P10-04: Solving quadratic equations
Recall that quadratic equations are of the form
Typically the solution is two real roots x1 and x2. Example: given 1, 3, and 2 for a, b, and c, solving for x yields -1 and -2.
Write a function SolveQuadratic that takes three coefficients a, b, and c, and solves for x. However, as you probably remember, in some cases there is only one solution for x, and in other cases there are no solutions for x. And when the discriminant is negative, solving for x yields complex roots --- which MATLAB supports. [ You can test this in MATLAB, Octave, or using the script window here in zybooks --- compute the square root of -1 and see what you get. Type this command: sqrt(-1) ]
In this exercise your function will have 3 output variables:
function [x1, x2, s] = SolveQuadratic(a, b, c)
...
end
The output variables x1 and x2 denote the solution, while s is an information string about the solution. Here's the algorithm to follow for your function:
if a, b, and c are all 0
s = 'indeterminate';
elseif a and b are 0
s = 'none';
elseif a is 0
s = 'linear';
x1 = ...;
elseif the discriminant is 0
s = 'one real root';
x1 = ...;
elseif the discriminant is less than 0
s = 'complex roots';
x1 = ...; %% x1 = -b + ...
x2 = ...; %% x2 = -b - ...
else %% discriminant is positive:
s = 'real roots';
x1 = ...; %% x1 = -b + ...
x2 = ...; %% x2 = -b - ...
end
Recall that the discriminant is defined as b^2 - 4ac. To compute the square root of a value, use the MATLAB function sqrt. The syntax for if-then-else statements is accurately shown above, but you'll want to read about if statements, relational operators, and logical operators by skimming chapter 28 (CS 109) or chapter 11 (CS 110). You have unlimited submissions.
Because the function has 3 output variables, there's a special syntax necessary to call the function and capture all 3 results:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started