Complex Roots of a quadratic function

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

Answers (2)

"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 Comments

I really appreciate your help here! This is very helpful but I guess I am confused when trying to call the function "Quadratic". In the command line, I keep getting an error
[x_1,x_2] = Quadratic(2,6,4);
Unrecognized function or variable 'Quadratic'.
Have you saved the function as a 'Quadratic.m'?
If yes, then have is the file in the current directory?
Thanks for the feedback. I guess I am confused on what you mean y saving the function as a 'Quadratic.m'. Sorry, I am new to this. I thought declaring it as this would be doing that?
function [x1,x2] = Quadratic(a,b,c)
function declarations cannot be used on the command line. You would have had to store that code in a file. Which file did you store it in? Is that file in your current directory ?
No, it does not.
In order to run a function, it needs to be saved as a file with the file name being the same as the function name. Since the function you have is named 'Quadratic', the file name must be 'Quadratic.m'.
Make sure you save the file in the current directory, or you will have to copy/move the file to the current directory.
After you saved the function file, you can call the function in the command line.
This is all saved as a .m file when I run it and I am in the same directory.
In order to run a function, it needs to be saved as a file with the file name being the same as the function name
There is a quirk of MATLAB that leads to an exception to that.
If you have a function file (a file whose first executable token is function ) then the name of the first function given on the function line will be ignored, and the name of the file will be used instead, as-if it the file name had been coded.
So you could store function [x1,x2] = Quadratic(a,b,c) at the beginning of (say) Quoth_The_Raven and then invoke it as
[never, more] = Quoth_The_Raven(2,6,4)
and it would execute even though the function line is still Quadratic
This only applies to the very first function in a function file.
"This is all saved as a .m file when I run it and I am in the same directory."
Then the function call in the command line should work.
I ran the code you provided in the Editor. Then did this in the command line:
>> quad_solve_real_Kefalos %ran the file
>> Quadratic(1,2,3) %called the function with 1,2,3 for the varibles
Unrecognized function or variable 'Quadratic'. %Got this as my error
Why did you save the file (as a script) with this name "quad_solve_real_Kefalos"?
As I mentioned above, the file must be saved with the same name as the function. Your file should be saved as 'Quadratic.m'.
I'll clarify once again -
The section from this line
function [x1,x2] = Quadratic(a,b,c)
to the last line
end
must be written in editor and then saved as 'Quadratic.m'.
After saving, call the function in the command line -
[out1,out2]=Quadratic(1,2,3)
I then get this as my error:
Error: File: Quadratic.m Line: 6 Column: 20
Local function name must be different from the script name.
I get "unable to define local function 'Quadratic' because it has the same name as the file"
>> [out1,out2]=Quadratic(1,2,3)
Error: File: Quadratic.m Line: 6 Column: 20
Local function name must be different from the script name.
I guess you have stored the file in a similar style to my code above.
Things are different in Live Editor (here) compared to the MATLAB app.
I am attaching a screenshot as to how it should be. Hope this helps.
I have an underline under the Quadratic function in the Editor. It says function may be unused?
Can you attach a screenshot of your MATLAB app, like I did?
MATLAB defines three different kinds of files whose extension ends in .m :
  • files whose first executable word is function . There can be comments and whitespace before that but no other statements -- no clear all for example. Such files are referred to as "function files". Function files cannot define classes within them.
  • files whose first executable word is classdef . There can be comments and whitespace before that but no other statements -- no clear all for example. Such files are referred to as "class definitions"
  • another other .m file is not considered to be a function file or class definition file, and is referred to as a "script" file. This includes files that contain only whitespace and comments. Before R2015b, script files could not have function statements in them. As of R2015b, script files can have function statements in them. function statements, by definition, cannot be the first executable thing in script files -- if they were were first executable thing then it would be a function file. function statements inside script files must appear after the script. You cannot use a coding pattern such as creating part of the script, then defining a function, and then continuing the script: the function lines must be after all script lines. And the name of function defined in script files cannot be the same as the name of the script file.
So you cannot have a script Quadratic.m that also defines a function Quadartic.m .
The most common reason to receive that error message is by attempting to put code such as
clear all
close all
at the time of a function file. Those are executable statements and if they appear before a function statement then the file is not a function file and is instead a script file.
As a general rule, at least until you have a lot more experience with MATLAB, you should not have clear all coded into any of your .m files.
Here is a screenshot of my matlab
The
clear;clc
syms a b c
lines appear before the function statement, so as I explained above, that makes your file a script file instead of a function file.
You could rename the file to something other than Quadratic.m to make it into a technically valid file. However, the script part -- the clearing and the symbolic definitions -- do not call the Quadratic function stored in the file, so there would not seem to be any point in bothering to define the Quadratic function at all.
When you create a script file that defines a function inside of it, the only way that any code outside of the script can invoke the function stored in the file, would be if the script created a handle to the function and made it available to the outside world. For example, you could have a function such as Useless_Container_For_Quadratic.m that had the contents
clear;clc
syms a b c
QF = @Quadratic
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
and if you did that, then after executing Useless_Container_For_Quadratic the containing workspace would have a function handle variable named QF that could be used to call the function, such as
QF(11,3,9)
Question for you:
Why are you defining symbolic variables a b c ? You are not using them inside your function -- your function has named parameters a b c and named parameters always override anything in the calling environment, so the syms a b c that you define will be ignored inside the function.
You cannot be expecting to pass symbolic variables a b c to the function at some point: if you pass symbolic a or b or c to the function then D and x1 and x2 are going to be symbolic expressions involving the symbolic variables you pass in -- and with D being symbolic involving an unresolved variable, you would not be able to compare D<0 as is needed for your function. Your function can only work if you pass in numeric values, not symbolic values. Well, except for possibilities such as passing in (a, 2*a, a) in which case D would come out as 0 even though symbolic variables were being used.
Thank you for that input. That was left over from a previous file I was working on and my heading comments were copied over
Removing those lines, I am now getting this error. Seems like the function is not initializing? I have no idea what is the issue when you guys are able to execute this... I am sorry if this is basic stuff, I am just confused
>> quad_solve_real %% just running the file from Editor
Not enough input arguments.
Error in quad_solve_real (line 5)
X = b^2 - 4*a*c;
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
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.

Sign in to comment.

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 Comments

... because it is homework
fair, but this is still helpful

Sign in to comment.

Asked:

on 16 Sep 2023

Edited:

on 19 Sep 2023

Community Treasure Hunt

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

Start Hunting!