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 Center および File Exchange2-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