Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Here is my codes, i dont know what wrong for these function [x1, x2, s] = SolveQuadratic(a, b, c) discriminant= b^2 - 4*a*c; D=discriminant; if
Here is my codes, i dont know what wrong for these
function [x1, x2, s] = SolveQuadratic(a, b, c)
discriminant= b^2 - 4*a*c;
D=discriminant;
if a==0 && b==0 && c==0
s = 'indeterminate';
elseif a==0 && b==0
s = 'none';
elseif a == 0
x1=-c/a;
x2=x1;
s = 'linear';
elseif D> 0
x1 =(-b+ sqrt(b^2-4 *a*c))/(2*a);
x2 = (-b- sqrt (b^2-4 *a*c))/(2*a);
s= ('real roots')
elseif D == 0
x1 = -b/2*a;
x2 = x1;
s=('one real root')
else D
x1 =(-b+ sqrt(b^2-4 *a*c))/(2*a);
x2 = (-b- sqrt (b^2-4 *a*c))/(2*a);
s= (' complex roots')
end
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 elseif a and b are 0 elseif a is elseif the discriminant is 0 sindeterminate' s none ; s'linear; sone real root'; ' ' elseif the discriminant is less than complex roots' else %% discriminant is positive : sreal roots' 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 [x1, x2, s SolveQuadratic(1, 3, 2)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