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?
Afficher commentaires plus anciens
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 commentaire
zhanel zhumagulova
le 23 Jan 2020
Trying to do the same question
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Mathematics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!