How do I get this function to accept one input argument, a vector representing the three coefficients of the quadratic in the order a, b, c?
Show older comments
function[quadRoots, disc] = Q1(coeff)
%function solves quadratic equation in the form ax^2 + bx + c = 0
a= coeff(1,1);
b= coeff(2,1);
c= coeff(3,1);
end
%finding the descriminant
disc = (b^2-4*a*c);
%roots
if disc>0
x1= ((-b+sqrt(disc))/(2*a));
x2= ((-b-sqrt(disc))/(2*a));
quadRoots= [x1, x2]
end
%no real roots
if disc<0
quadRoots = nan;
end
%one real root
if disc == 0
x1= ((-b+sqrt(disc))/(2*a));
disc= (b^2-(4*a*c));
quadRoots - x1;
end
end
1 Comment
zhanel zhumagulova
on 23 Jan 2020
Trying to do the same question
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!