how do i make this function accept vector values ?

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

回答(1 个)

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 个评论

yes , it is a +1 or -1 input in order to calculate either the negative or positive root

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by