Write a program in a script file that determines the real roots of a quadratic equation
Afficher commentaires plus anciens
Howdy, I am new to math lab and need a little help The question asks: "Write a program in a script file that determines the real roots of a quadratic equation ax^2+bx+c=0. Name the file quadroots. When the file runs, it asks the user to input values of the constants a,b, and c. To calculate the roots of the equation the program calculates the discriminant D, given by D=b^2-4ac."
Then it goes on to give me specific equations and what Matlab is actually doing
I don't know what commands to use, and any help would be appreciated, thanks
1 commentaire
Seth
le 2 Avr 2012
Réponse acceptée
Plus de réponses (5)
Sean de Wolski
le 2 Avr 2012
doc roots
and, specifically:
roots([a b c]);
Fikru Dikumbab
le 16 Sep 2014
Modifié(e) : Fikru Dikumbab
le 16 Sep 2014
% ax^2+bx+c =0 is a quadratic equation
function qadr(a,b,c)
if b^2-4*a*c > 0
r1=(-b+sqrt(b^2-4*a*c))/2*a
r2=(-b-sqrt(b^2-4*a*c))/2*a
else if b^2-4*a*c == 0
r=-b/2*a
else
disp(' No real solution')
end
end
end
Md. Intesar Farhan Labib
le 6 Avr 2021
Modifié(e) : Md. Intesar Farhan Labib
le 6 Avr 2021
clc
clear all
close all
a= input("Co-efficient of x^2: ");
b= input("Co-efficient of x: ");
c=input("Contant term: ");
d= b^2-4*a*c;
fx=[a b c];
x=roots(fx);
if d>0
disp(['The equation has two roots ', num2str(x(1)),' ,',num2str(x(2))]);
elseif d==0
disp(['The equation has one root ', num2str(x(1)),',',num2str(x(2))]);
else
disp("The equation has no real roots.");
end
5 commentaires
Chandrachud Shrivastava
le 15 Mar 2022
Modifié(e) : Walter Roberson
le 15 Mar 2022
```
clc
clear all
close all
a= input("Co-efficient of x^2: ");
b= input("Co-efficient of x: ");
c=input("Contant term: ");
d= b^2-4*a*c;
fx=[a b c];
x=roots(fx);
if d>0
fprintf(['The equation has two roots ', num2str(x(1)),' ,',num2str(x(2))]);
elseif d==0
fprintf(['The equation has one root ', num2str(x(1)),',',num2str(x(2))]);
else
x1 = (-b + sqrt (d)) / (2*a);
x2 = (-b - sqrt (d)) / (2*a);
fprintf('Complex Zeros: %g + i*%g, %g + i*%g\n',real(x1),imag(x1),real(x2),imag(x2))
end
```
Shailendra Shankar
le 24 Avr 2023
How can I modify your programs for the below conditions:
- IF a = 0 and b ≠ 0, the equation will be linear, and there will be one root, x = - c/b and the same should be printed in the command window.
- IF both a and b are equal to 0, then the equation is invalid and the same should be printed in the command window.
below is the error I get:
Index exceeds the number of array elements. Index must not exceed 1.
Error in QUAD (line 11)
disp(['The equation has two roots ', num2str(x(1)),',',num2str(x(2))]);
Md. Intesar Farhan Labib
le 24 Avr 2023
You can add additional command before checking D. First check whether a= 0 or not. If a=0 and b=0 Then display that the input is invalid. elseif a=0 Then simply show the result as -c/b Else The rest of the programme should be written here.
Thanks
Shailendra Shankar
le 26 Avr 2023
clc
clear all
close all
a= input("Co-efficient of x^2: ");
b= input("Co-efficient of x: ");
c= input("Contant term: ");
d= b^2-4*a*c;
if a==0&&b==0;
disp ('The equation is invalid')
elseif a==0
x=-c/b;
disp('The equation is now linear and has one root',x);
end
fx=[a b c];
x=roots(fx);
if d>0
disp('The equation has two roots ', num2str(x(1)),',',num2str(x(2)));
elseif d==0
disp('The equation has one root ', num2str(x(1)));
else
disp('The equation has no real roots.');
end
Can you correct this code for me please.
ERROR MSG:
Co-efficient of x^2: 0
Co-efficient of x: 4
Contant term: -16
Error using disp
Too many input arguments.
Error in QUAD (line 12)
disp('The equation is now linear and has one root',x);
Shailendra Shankar
le 26 Avr 2023
Here are my requirements:
- need to show that if a=b=0, the equation is invalid
- if a=0, the equation is linear and the root is to be displayed
- if d>0, the equation has two roots which have to be displayed
- if d=0, the equation has only one root which has to be displayed
- if d<0, the equation has no real roots
Matt Kindig
le 2 Avr 2012
0 votes
Well let's break down the question step by step, with the specified section of the documentation for each.
- Get input a, b, c from user
- Calculate discriminant D
- Compare D to 0.
- If D < 0, there will be imaginary roots. Since your assignment specifies real roots, throw an error here. If D == 0, there will be one root. If D > 0, there will be two roots.
- Calculate the roots using the quadratic formula.
- Display results to user or output (depending on assignment specification).
For step 1, look at the 'input' function in MATLAB. The rest of the assignment can be done using straightforward arithmetic operations (+,-,*,/). For steps 3-4, use an if-else structure.
This should get you started...
Catégories
En savoir plus sur Large Files and Big Data dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!