Your function file must be saved somewhere on your MATLAB user path as: quadFunc.m so you can use it.
quadratic equation function help
1 次查看(过去 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
0 个评论
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Simulation Setup 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!