Quadratic equation & Minus B

How do I solve the quadratic equation ax^2+bx+c=0 using the minus b formula, x=-b+- sqrt b^2-4ac/2a on MATLAB
Thank you

3 comentarios

Geoff Hayes
Geoff Hayes el 15 de Mayo de 2019
Fergal - are a, b, and c known? If so, wouldn't the code just be
x1 = (-b + sqrt(b^2 - (4 * a * c))) / (2 * a);
x2 = (-b - sqrt(b^2 - (4 * a * c))) / (2 * a);
assuming a is not zero...
Fergal Ahern
Fergal Ahern el 15 de Mayo de 2019
Editada: Fergal Ahern el 15 de Mayo de 2019
Geoff - The program must read in coefficients a,b and c from the keyboard and then calculate the roots of the equation. Do you know how I would verify that real roots are generated?
James Tursa
James Tursa el 15 de Mayo de 2019
To read coefficients from the keyboard, you could use the input function
To see if a number is real, you could use the isreal function

Iniciar sesión para comentar.

 Respuesta aceptada

kirti singh
kirti singh el 16 de Mayo de 2019

1 voto

The following example code snippet works for accepting the coefficients a,b and c and calculating the roots and finding if they are real or not:
a1 = 'Enter value of a ';
a = input(a1)
b1= 'Enter value of b ';
b = input(b1)
c1= 'Enter value of c ';
c = input(c1)
x1 = (-b + sqrt(b^2 - (4 * a * c))) / (2 * a);
x2 = (-b - sqrt(b^2 - (4 * a * c))) / (2 * a);
t1=isreal(x1);
t2=isreal(x2);

1 comentario

James Tursa
James Tursa el 16 de Mayo de 2019
Please don't post complete solutions to homework problems.

Iniciar sesión para comentar.

Más respuestas (1)

Pavi thra
Pavi thra el 28 de Oct. de 2020

0 votos

a1 = 'Enter value of a ';
a = input(a1)
b1= 'Enter value of b ';
b = input(b1)
c1= 'Enter value of c ';
c = input(c1)
x1 = (-b + sqrt(b^2 - (4 * a * c))) / (2 * a);
x2 = (-b - sqrt(b^2 - (4 * a * c))) / (2 * a);
t1=isreal(x1);
t2=isreal(x2);

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 15 de Mayo de 2019

Respondida:

el 28 de Oct. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by