Imaginary numbers in a quadratic using matlab

10 次查看(过去 30 天)
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

回答(1 个)

Raunak Gupta
Raunak Gupta 2019-12-5
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');

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by