Write a function that takes three inputs: a, b and c of the standard polynomial
ID: 3793424 • Letter: W
Question
Write a function that takes three inputs: a, b and c of the standard polynomial form ax^2 + bx + c = 0 And returns two outputs, the two solutions of the quadratic equation. You need not worry about negative numbers inside of radicals (like sqrt (-1)), as MATLAB can handle complex and imaginary numbers! a = 1; b = 6; c = 5; correct1 = -1; correct2 = -5; [x1 x2] = quadratic (a, b, c); assert ((isequal (x1, correct1)||isequal (x1, correct2))&& ((isequal (x2, correct1)||isequal (x2, correct 2))))Explanation / Answer
function [x1,x2] = quadratic(a,b,c)
%First calculate determinant
d = sqrt(b^2 - 4*a*c);
%Now using determinant to find roots
x1 = (-b+d)/(2*a);
x2 = (-b-d)/(2*a);
end
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.