MATLAB Answers

Imaginary numbers in a quadratic using matlab

2 views (last 30 days)
Estela Barrios Lopez
Estela Barrios Lopez on 2 Dec 2019
Answered: Raunak Gupta on 5 Dec 2019
I am trying to graph a quadratic using the 2 roots found
but wont graph . this is the code. what am i dong wrong. keeps on giving me imaginary values for my x values
function [x1,x2]=quad(a,b,c)
z=sqrt(b^2 -4*a*c);
w=2*a;
x1=(-b+z)/w;
x2=(-b-z)/w;
end
disp('a cannot be 0')
a=input('Enter a Value for a: ');
b=input('\nEnter a value for b: ');
c=input('\nEnter a vlaue for c: ');
[x1,x2]= quad(a,b,c)
z=sqrt(b^2 -4*a*c);
w=2*a;
g1=(a*x1^2+b*x1+c)
g2=(a*x2^2+b*x2+c)
if z<0
fprintf('There are no real values')
elseif z==0
fprintf('There is only one real root')
else
fprintf('There is two real roots')
plot(g1,g2)
end

  0 Comments

Sign in to comment.

Answers (1)

Raunak Gupta
Raunak Gupta on 5 Dec 2019
Hi,
The roots of quadratic equation are dependent on the coefficients a,b,c and according to code that is mentioned the roots are plotted only when they are real. You may plot imaginary roots with real part on the x axis and imaginary part on the y axis as mentioned here. Also, at the end of the code g1,g2 are plotted which are not roots but evaluation of quadratic equation at the roots. You may plot roots by
plot([x1,x2],'o');

  0 Comments

Sign in to comment.

Sign in to answer this question.

Products


Release

R2019a

Translated by