how do i make this function accept vector values ?
Show older comments
hi , i have this function of the quadratic formula and i have had trouble getting it to accept vector values ?
function [ Root] = Quadratic( a, b ,c , plusOrMinus )
%UNTITLED6 Summary of this function goes here
% inputs coeffecients
if ((b^2)-4.*(a).*(c)) > 0
Num = -b +(plusOrMinus)* sqrt((b.^2)-(4.* a.* c));
denom = 2.*(a);
Root = Num/denom;
else
error(' the coeffecients yield a complex root')
end
Answers (1)
James Tursa
on 15 Feb 2017
Edited: James Tursa
on 15 Feb 2017
Assuming you want to yield an error if any of the values yield complex roots, e.g.,
function [ Root] = Quadratic( a, b ,c , plusOrMinus )
%UNTITLED6 Summary of this function goes here
% inputs coeffecients
% Khaled Almutairi, u1055414, ME EN 1010, HW2
if all( (b.^2)-4.*(a).*(c)) >= 0 )
Num = -b +(plusOrMinus).* sqrt((b.^2)-(4.* a.* c));
denom = 2.*(a);
Root = Num./denom;
else
error(' the coeffecients yield a complex root')
end
However, it is not clear to me what plusOrMinus is and whether that will work with vectors or not. Maybe you could clarify what this is. Simply a +1 or -1 input?
1 Comment
khaled almutairi
on 15 Feb 2017
Categories
Find more on Programming 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!