quadratic equation function help
3 views (last 30 days)
Show older comments
I can't figure out how to make it so the root type will display either real roots, complex roots or equal roots.
Rightnow when i type disp(rootType) in the command window it says 'Unrecognized function or variable 'rootType'.
if you know what im doing wrong or see any other things wrong Please let me know.
function [x1, x2, rootType]= quadFunc(a,b,c)
%% quadFunc.m
% The inputs to the function are the equation coefficients (a, b, and c) in
% that order
% The function solves the quadratic formula based on these coefficients.
% The function evaluates the value of the discriminant ("2 − 4!#) to
% determine if there are two real roots, two equal roots or complex roots.
% The outputs of the function should be (x1, x2, rootType) in that order
% The value of the roots (x1, x2)
% A string (rootType) that describes the roots
% (“Real roots”, “Complex roots” or “Equal roots”)
% 10/4/20
a=3;
b=2;
c=5;
x1 = (-b+sqrt((b^(2)-4*a*c)))/2*a;
x2 = (-b-sqrt((b^(2)-4*a*c)))/2*a;
det = sqrt((b^(2)-4*a*c));
rootType = 'undetermined';
if det > 0
rootType = 'there are two real roots'
elseif det < 0
rootType = 'There are complex roots'
else
rootType = 'there are equal roots'
end
0 Comments
Answers (1)
Star Strider
on 5 Oct 2020
Your function file must be saved somewhere on your MATLAB user path as: quadFunc.m so you can use it.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!