quadratic equation function help

3 次查看(过去 30 天)
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

回答(1 个)

Star Strider
Star Strider 2020-10-5
Your function file must be saved somewhere on your MATLAB user path as: quadFunc.m so you can use it.
If you are unfamiliar with it, see the documentation section on: What Is the MATLAB Search Path?

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by