I need help with the secant method. Need to find the root of Wilkinson polynomial
1 回表示 (過去 30 日間)
古いコメントを表示
Im trying to use the secant method to prove that the root lies [20,22]. The equation that I'm trying to solve is
p(t) = (t-1)*(t-2)*...*(t-20) - (10^-8)*t^19
my code is as follows:
format long
my_tol = 1e-16
w =@(t,n) prod(t-n)-(10.^-8)*t.^19
t(1) = input('Enter first guess: ')
t(2) = input('Enter second guess: ')
iter = 0;
n = 1;
for i = 3:23
t(i) = t(i-1)-(w(t(i-1),n))*((t(i-1)-t(i-2))/(w(t(i-1),n))-w(t(i-2)));
iter = iter + 1;
n = n + 1;
if abs(t(i)-t(i-1))/t(i) < my_tol
wilkroot = t(i)
break
end
end
I'm not going to lie, i am terrible at this. This code looks like it should work but i get this error
*Error using @(t,n)prod(t-n)-(10.^-8)*t.^19
Not enough input arguments.
Error in test (line 9)
t(i) = t(i-1)-(w(t(i-1),n))*((t(i-1)-t(i-2))/(w(t(i-1),n))-w(t(i-2)));*
Can anyone help me with this?? I'm losing my f****** mind
1 件のコメント
John D'Errico
2014 年 9 月 28 日
編集済み: John D'Errico
2014 年 9 月 28 日
It is much easier to read IF you flag as code those parts of your post that are code. In fact, simply preceding those lines with a double space will suffice.
回答 (2 件)
Guillaume
2014 年 9 月 28 日
You've defined w as a function of two variables, but the last part of your t calculation on line 9 calls w with only one argument, t(i-2). Maybe you meant:
t(i) = t(i-1)-(w(t(i-1),n))*((t(i-1)-t(i-2))/(w(t(i-1),n))-w(t(i-2), n));
0 件のコメント
John D'Errico
2014 年 9 月 28 日
編集済み: John D'Errico
2014 年 9 月 28 日
Anyway, why do you think that your function w(t,n) evaluates that polynomial?
t is a vector of length 2, end points of your interval apparently. n is a scalar, that seems to change with iteration?
I think you need to first figure out how to write a function that will CORRECTLY evaluate that polynomial.
Your polynomial has a fixed order, so why make it a function of n at all? Next, you need to be careful trying to use tools like prod when t is a vector. How does MATLAB know what to take the product over? READ THE HELP FOR PROD.
For example, IF you know that t will always be a row vector, then you MIGHT do something like this:
w = @(t) prod(bsxfun(@minus,t,(1:20)'),1) - (10^-8)*t.^19;
Note how I've changed w. See that the last term uses the .^ operator, instead of the ^ operator. There is a difference, and it is an important one when t is a vector.
I've also made w not a function of n. Why would you have it that way anyway?
And, if you wanted to make w insensitive to whether t is a row or column vector, this would work.
w = @(t) prod(bsxfun(@minus,t(:).',(1:20)'),1) - (10^-8)*t(:).'.^19;
Having done that, what does it take to prove a root lies in an interval? So if your interval of interest is [20,22], what does w([20 22]) tell you? Do you really need to do much more than that if your goal is to show at least one root lies in an interval?
2 件のコメント
John D'Errico
2014 年 9 月 28 日
The polynomial is not a function of n. Somehow maybe you were confusing n with the numbers 1:20 in the polynomial. So rewrite it. Solve the small problem of determining there is a root in that interval.
The solve the secant method iteration. By the way, I'm not sure why you were using a var named n and iter, then incrementing both.
参考
カテゴリ
Help Center および File Exchange で Polynomials についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!