"roots" call is not providing the correct answer

2 ビュー (過去 30 日間)
Jenna Broders
Jenna Broders 2023 年 5 月 24 日
編集済み: James Tursa 2023 年 5 月 25 日
So this has been happening for every type of polynomail that I give matlab, and it always provides me with the same answer.
For example, if trying to find the roots for: f(x) = x^2+ 3x+2
the two roots are simply: x = -1 and x = -2
But, when I run the code in my matlab program on my computer I get the following:
p = [1 3 2];
r = roots(p)
>> roottest
fx =
-1.6954e+03
r =
-1.6954e+03
And I get that answer regardless of what coefficients are place within the p variable. I'm just wondering if anyone has encountered a similar probelm ebfore and how to go about fixing it. I have tried clear roots before as well and that did not change anything. I'm just doing a tutorial for a lab session for intro to Matlab and while I will be using a different computer in the lab which I know works, I would just like to know why it is not working for my personal program.
TYIA
  2 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 5 月 24 日
Are you running a script named "roottest"? What are its content?
"And I get that answer regardless of what coefficients are place within the p variable."
Maybe you have saved a user-defined function with the name 'roots'.
What is the output when you run this following line of code?
which roots -all
Jenna Broders
Jenna Broders 2023 年 5 月 24 日
Thanks for your response,
After looking through my files it looks like do have an old file called roots with a preset polynomial which is was printing every time.
So thank you for pointing that out as a possibility!
Is there a way to replace that function with the one that was originally associated with matlab? I'm not sure if you know how to do that so no worries if not.
thanks again!

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

回答 (1 件)

James Tursa
James Tursa 2023 年 5 月 24 日
編集済み: James Tursa 2023 年 5 月 25 日
Rename your old roots file (preferred method) or move it to a directory that is not on the MATLAB path. That way it won't shadow the MATLAB roots function.

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by