Complex Roots of a quadratic function

조회 수: 24 (최근 30일)
Jason
Jason 2023년 9월 16일
편집: Dyuman Joshi 2023년 9월 19일
Hello,
I have this code that take inputs from a user to solve for the roots of a quadratic equation. I am focused on getting the complex roots of this and am having trouble getting anything to display. I am using a function where it takes 3 inputs of a,b,c and the code should process it to output the roots of x1 and x2. Right now, I am not getting anything to display after doing the inputs. I am very confused and any help would be much appreciated.
function [x1,x2] = Quadratic(a,b,c)
%ax^2+ bx+ c = 0
if b^2 < 4*a*c
x1 = (-b +sqrt(b^2 - 4*a*c))/(2*a);
x2 = (-b -sqrt(b^2 - 4*a*c))/(2*a);
disp(' x1 and x2 are complex roots')
disp(' x1',x1)
disp(' x2',x2)
elseif b^2 == 4*a*c
x1 = -b/(2*a);
x2=x1;
disp('equal roots')
disp(' x1',x1)
disp(' x2',x2)
else
x1 = (-b + sqrt(b^2 - 4*a*c))/(2*a);
x2 = (-b - sqrt(b^2 - 4*a*c))/(2*a);
disp(' x1',x1)
disp(' x2',x2)
end
end

답변 (2개)

Dyuman Joshi
Dyuman Joshi 2023년 9월 16일
편집: Dyuman Joshi 2023년 9월 16일
"Right now, I am not getting anything to display after doing the inputs."
It is because you have not called the function. To get output(s) from a function, you have call the function with input(s) (if there are any required).
Additionally, when a quantity is used multiple times in a code, it is better to store it in a variable rather than evaluating the quantity repeatedly. In your case, it would be the discriminant and the roots.
%Random values for example, as input is not supported in live editor
a = 3; %input('Enter value for a: ')
b = 5; %input('Enter value for b: ')
c = 9; %input('Enter value for c: ')
%Calling the function
[x1,x2] = Quadratic(a,b,c);
x1 and x2 are complex roots x1 = -0.8333 + 1.5184i x2 = -0.8333 - 1.5184i
%Alternatively you can directly call a function, like this
[X1,X2] = Quadratic(1,2,1);
equal roots x1 = -1 x2 = -1
%Calling a function directly
[x_1,x_2] = Quadratic(2,6,4);
real and unequal roots x1 = -1 x2 = -2
function [x1,x2] = Quadratic(a,b,c)
%ax^2+ bx+ c = 0
%Computing and storing the discriminant as a variable
%to use for comparison multiple times
D = b^2 - 4*a*c;
%Doing the same for roots
x1 = (-b + sqrt(D))/(2*a);
x2 = (-b - sqrt(D))/(2*a);
if D<0
disp('x1 and x2 are complex roots')
elseif D==0
disp('equal roots')
else
%Added statement
disp('real and unequal roots')
end
disp('x1 = ')
disp(x1)
disp('x2 = ')
disp(x2)
end
  댓글 수: 23
Jason
Jason 2023년 9월 16일
Disregard, I finally got it running. I apologize for having you guys take the time to explain this to me on a Saturday lol. Thank you guys so much for all your help
Dyuman Joshi
Dyuman Joshi 2023년 9월 17일
편집: Dyuman Joshi 2023년 9월 19일
Looking at the screenshot you have attached - remove any (non-empty) lines before
function [x1,x2] = Quadratic(a,b,c)
And your code should work properly.
@Ant, if my answer helped solve your problem, please consider accepting it.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2023년 9월 16일
Why not call the built-in roots function?
help roots
ROOTS Find polynomial roots. ROOTS(C) computes the roots of the polynomial whose coefficients are the elements of the vector C. If C has N+1 components, the polynomial is C(1)*X^N + ... + C(N)*X + C(N+1). Note: Leading zeros in C are discarded first. Then, leading relative zeros are removed as well. That is, if division by the leading coefficient results in overflow, all coefficients up to the first coefficient where overflow occurred are also discarded. This process is repeated until the leading coefficient is not a relative zero. Class support for input c: float: double, single See also POLY, RESIDUE, FZERO. Documentation for roots doc roots Other uses of roots gpuArray/roots sym/roots
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 9월 16일
... because it is homework
Jason
Jason 2023년 9월 16일
fair, but this is still helpful

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by