필터 지우기
필터 지우기

quadratic equation function help

조회 수: 5 (최근 30일)
Kelsey Pettrone
Kelsey Pettrone 2020년 10월 5일
답변: Star Strider 2020년 10월 5일
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에서 Simulation Setup에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by