Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is my codes, i dont know what wrong for these function [x1, x2, s] = SolveQ

ID: 3732369 • Letter: H

Question

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 < 0

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)

Explanation / Answer

Given below is the fixed code for the question. For complex root, there was extra space when assigning string to s. For case a ==0, x1 should be assigned -c/b where as you have written -c/b


Please do rate the answer if it was helpful. Thank you

function [x1, x2, s] = SolveQuadratic(a, b, c)
D = b^2 - 4*a*c;

if a==0 && b==0 && c==0
s = 'indeterminate';
elseif a==0 && b==0
s = 'none';
elseif a == 0
s = 'linear';
x1 = -c/b;
elseif D == 0
s = 'one real root';
x1 = -b/2*a;
elseif D < 0
s= 'complex roots';
x1 =(-b + sqrt(D))/(2*a);
x2 =(-b - sqrt(D))/(2*a);
else
s= 'real roots';
x1 =(-b + sqrt(D))/(2*a);
x2 =(-b - sqrt(D))/(2*a);
end

end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote