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

Risposte (1)

James Tursa
James Tursa il 15 Feb 2017
Modificato: James Tursa il 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 Commento

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

Accedi per commentare.

Categorie

Scopri di più su Programming in Centro assistenza e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by