Complex Roots of a quadratic function

24 Ansichten (letzte 30 Tage)
Jason
Jason am 16 Sep. 2023
Bearbeitet: Dyuman Joshi am 19 Sep. 2023
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

Antworten (2)

Dyuman Joshi
Dyuman Joshi am 16 Sep. 2023
Bearbeitet: Dyuman Joshi am 16 Sep. 2023
"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 Kommentare
Jason
Jason am 16 Sep. 2023
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 am 17 Sep. 2023
Bearbeitet: Dyuman Joshi am 19 Sep. 2023
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.

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 16 Sep. 2023
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 Kommentare
Walter Roberson
Walter Roberson am 16 Sep. 2023
... because it is homework
Jason
Jason am 16 Sep. 2023
fair, but this is still helpful

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by