how to solve quadratic equation?

Hai,
How could I solve a quadratic equation in matlab? Looking for your reply.
BSD

 Réponse acceptée

Walter Roberson
Walter Roberson le 8 Nov 2011
Modifié(e) : Walter Roberson le 15 Oct 2025

0 votes

roots(). Or if you have the symbolic toolbox, solve()

4 commentaires

Naz
Naz le 8 Nov 2011
More explicit: x^2-2x+1. Coefficients are 1 -2 1, therefore:
roots([1 -2 1])
Walter Roberson
Walter Roberson le 8 Nov 2011
And note that any root that is output might be complex. If you want only the real roots, filter to TheRoots(imag(TheRoots)==0)
bsd
bsd le 9 Nov 2011
why is that == 0 used?
BSD
Walter Roberson
Walter Roberson le 9 Nov 2011
You can tell whether a number has a complex part or not by testing to see if the imaginary part is 0. imag(x) gives you the imaginary part of x, so imag(x)==0 tests whether the imaginary part is 0. TheRoots(imag(TheRoots)==0) thus selects only the roots which are real-valued with no imaginary component.
Of course for a quadratic function over real coefficients, either _neither_ root is complex or _both_ roots are complex...

Connectez-vous pour commenter.

Plus de réponses (4)

Reina Young
Reina Young le 27 Mai 2020

3 votes

This code works for real and imaginary roots.
%%QUADRATIC FORMULA%%
% A(x^2)+B(x)+C=0
begin_prompt = 'Quadratic Formaula (Yes/No)(1/0)';
d = input(begin_prompt)
if d == 1
prompt = 'Ax^2+Bx+C=0...A=';
A = input(prompt)
prompt2 = 'Ax^2+Bx+C=0...B=';
B = input(prompt2)
prompt3 = 'Ax^2+Bx+C=0...C=';
C = input(prompt3)
Answer1= ((-B)+((B^2-4*A*C))^0.5)/(2*A)
Answer2= ((-B)-((B^2-4*A*C))^0.5)/(2*A)
disp(Answer1)
disp(Answer2)
else
disp( 'Error in Start Prompt Input, Please Pick Yes (1) or this code will not work');
end
Emre Komur
Emre Komur le 10 Avr 2021
Modifié(e) : Emre Komur le 10 Avr 2021

3 votes

Solution Method-1
1- You can create function as below
function quadraticEquation
a = input('please enter a value :');
b = input('please enter b value :');
c = input('please enter a value :');
delta = (b.^2)-(4*a*c);
if(delta < 0)
disp("Delta < 0 The equation does not have a real root");
elseif (delta == 0)
disp('The equation has only one real roots');
disp(-b./(2*a));
else
disp('The equation has two real roots');
disp((-b+sqrt(delta))./(2*a));
disp((-b-sqrt(delta))./(2*a));
end
end
2- You should call the function as below
quadraticEquation
Rick Rosson
Rick Rosson le 8 Nov 2011

1 vote

Please try:
x = zeros(2,1);
d = sqrt(b^2 - 4*a*c);
x(1) = ( -b + d ) / (2*a);
x(2) = ( -b - d ) / (2*a);
HTH.
Rick

4 commentaires

Walter Roberson
Walter Roberson le 9 Nov 2011
Which is equivalent to x = roots([a,b,c]) except that roots() does not promise any particular order.
Walter Roberson
Walter Roberson le 9 Nov 2011
Beware a = 0 !
Rick Rosson
Rick Rosson le 9 Nov 2011
If a = 0, then it is not (strictly speaking) a quadratic equation. It's a linear equation, and the solution in that case is trivial to compute.
Walter Roberson
Walter Roberson le 9 Nov 2011
Yes, but it is not an uncommon problem for people to calculate or randomly generate the coefficients and forget to double-check that the system is still of the same order.

Connectez-vous pour commenter.

Temidayo Daniel
Temidayo Daniel le 18 Oct 2022

0 votes

a = input ('Enter the value of the coefficient of x square ');
b = input ('Enter the value of the coefficient of x ');
c = input ('Enter the third value ');
t = sqrt ((b^2) - (4*a*c));
u = (-b+t)/(2*a);
v = (-b-t)/(2*a);
x1 = u
x2 = v

Community Treasure Hunt

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

Start Hunting!

Translated by