Why is the polyval command giving two different answers?

조회 수: 2 (최근 30일)
Baldemy
Baldemy 2014년 9월 22일
댓글: Matt J 2014년 9월 23일
Why does the polyval operator not work as expected. Is the ans variable not stored as a column vector? Why aren't the second, fifth, and sixth results equal?
>> roots([1,-8,17,2,-24])
ans =
4.0000
3.0000
2.0000
-1.0000
>> polyval([1.-8,17,2,-24],ans)
ans =
-192.0000
-54.0000
-8.0000
-2.0000
>> roots([1,-8,17,2,-24])
ans =
4.0000
3.0000
2.0000
-1.0000
>> x=ans
x =
4.0000
3.0000
2.0000
-1.0000
>> polyval([1,-8,17,2,-24],x)
ans =
1.0e-13 *
0.8882
0.3197
0.0355
0.1421
>> polyval([1,-8,17,2,-24],[2.0000;3.0000;-1.0000;3])
ans =
0
0
0
0

채택된 답변

Alberto
Alberto 2014년 9월 22일
Instruction roots uses an iterative numeric method to approximate the solution in float arithmetic. What you get is an excellent approximation.
If you need the exact solution you should try a symbolic method:
g = x^4-8*x^3 + 17*x^2 +2*x -24
g =
x^4 - 8*x^3 + 17*x^2 + 2*x - 24
>> sol=solve(g==0)
sol =
2
3
4
-1
  댓글 수: 1
Matt J
Matt J 2014년 9월 23일
You also may need a symbolic version of polyval, even when you have the exact roots:
>> polyval([1,-8,17,2,-24]/3,[4 3 2 -1])
ans =
1.0e-14 *
0.8882 0.1776 0.1776 0.1776

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2014년 9월 22일
편집: Matt J 2014년 9월 22일
Because you have a typo in your call to polyval: a period appears where a comma should be.
  댓글 수: 2
Baldemy
Baldemy 2014년 9월 22일
편집: Matt J 2014년 9월 22일
ok then this happens instead
>> roots([1,-8,17,2,-24])
ans =
4.000000000000009
2.999999999999992
2.000000000000001
-1.000000000000000
>> polyval([1,-8,17,2,-24],ans)
ans =
1.0e-13 *
0.888178419700125
0.319744231092045
0.035527136788005
0.142108547152020
Matt J
Matt J 2014년 9월 22일
편집: Matt J 2014년 9월 22일
And what don't you like about it? The result is quite close to zero, as one would expect.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by