Complex Roots of a quadratic function
Show older comments
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)
Dyuman Joshi
on 16 Sep 2023
Edited: Dyuman Joshi
on 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);
%Alternatively you can directly call a function, like this
[X1,X2] = Quadratic(1,2,1);
%Calling a function directly
[x_1,x_2] = Quadratic(2,6,4);
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
Jason
on 16 Sep 2023
Dyuman Joshi
on 16 Sep 2023
Have you saved the function as a 'Quadratic.m'?
If yes, then have is the file in the current directory?
Jason
on 16 Sep 2023
Walter Roberson
on 16 Sep 2023
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 ?
Dyuman Joshi
on 16 Sep 2023
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.
Jason
on 16 Sep 2023
Walter Roberson
on 16 Sep 2023
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.
Dyuman Joshi
on 16 Sep 2023
"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.
Jason
on 16 Sep 2023
Dyuman Joshi
on 16 Sep 2023
Edited: Dyuman Joshi
on 16 Sep 2023
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)
Jason
on 16 Sep 2023
Jason
on 16 Sep 2023
Jason
on 16 Sep 2023
Dyuman Joshi
on 16 Sep 2023
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.
Jason
on 16 Sep 2023
Dyuman Joshi
on 16 Sep 2023
Can you attach a screenshot of your MATLAB app, like I did?
Walter Roberson
on 16 Sep 2023
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.
Jason
on 16 Sep 2023
Walter Roberson
on 16 Sep 2023
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.
Jason
on 16 Sep 2023
Jason
on 16 Sep 2023
Dyuman Joshi
on 17 Sep 2023
Edited: Dyuman Joshi
on 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.
help roots
Categories
Find more on Naming Conventions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!