Creating a code to solve quadratic equation

13 ビュー (過去 30 日間)
Michael Eugene Carter
Michael Eugene Carter 2022 年 2 月 2 日
編集済み: Voss 2022 年 2 月 2 日
I am trying to solve a quadratic equation in matlab, but I need to figure out how to do it without syms() or roots() or anyting like that. I have been trying to use a for loop or a while loop, but I can't figure out how to do this, and I don't really know if that is the right approach to something like this. The equation is just a*t^2+b*t+c, I already have a b and c defined and the rest of the code seems to work properly, but I have to have an exact answer for the roots of t so I keep getting an error.
I already tried running the code with syms('x') and then used solve() to find the roots of the equation, but my professor isn't allowing us to use syms() for this and so I need to find a way to solve for the roots without using those and I am stuck on how we are supposed to do this.

採用された回答

Voss
Voss 2022 年 2 月 2 日
編集済み: Voss 2022 年 2 月 2 日
How about applying the quadratic formula? Is that allowed?
% a*t^2+b*t+c = 0
d = sqrt(b^2-4*a*c);
t1 = (-b+d)/(2*a);
t2 = (-b-d)/(2*a);
Or better:
% a*t^2+b*t+c = 0
t0 = (-b+sqrt(b^2-4*a*c)*[1 -1])/(2*a);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumber Theory についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by