Imaginary numbers in a quadratic using matlab

조회 수: 12 (최근 30일)
STUDENT
STUDENT 2019년 12월 2일
답변: Raunak Gupta 2019년 12월 5일
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