Imaginary numbers in a quadratic using matlab
조회 수: 12 (최근 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
댓글 수: 0
답변 (1개)
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');
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!