Write a quadratic equation function

Hi all,
I've been writing a function to solve a quadratic equation of the form ax^2 + bx + c. It works for quadratics where there are two real roots, but I want it to display NA for one of the roots when there is a repeated root or NA for both when there are no real roots. Not sure if I have to use an if statement or an if else statement?
Thanks for any help!

 採用された回答

Honglei Chen
Honglei Chen 2014 年 11 月 13 日

0 投票

Isn't this just depending on the value of b^2-4*a*c, something like
val = b^2-4*a*c;
if val > 0
% two roots
elseif val == 0
% one root, one NA
else
% NA
end

2 件のコメント

Sam
Sam 2014 年 11 月 13 日
Yeah, I'm just having trouble getting NA displayed in the command window when I run it from there. Eg for x^2 - 6x + 9,
x1 = 3, x2 = NA
Thank you
Sam
Sam 2014 年 11 月 13 日
I've got it now, Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeQuadratic Programming and Cone Programming についてさらに検索

タグ

質問済み:

Sam
2014 年 11 月 13 日

コメント済み:

Sam
2014 年 11 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by