这是使用Graeffe方法进行根寻找的问题,其中给定的多项式是:
p(x) = x^4 - x^3 + 3*x^2 + x - 4 = 0
现在我已经完成了所有的计算,但是如果我不使用'if'语句,那么代码非常棒,输出看起来像下图所示。
正如您所看到的, a2 正在第二次和第三次次迭代之后改变符号,再次在第七次和第八次< / strong>迭代,因此根与复数相关。所以,这些我想在代码中写的它正在改变符号(即使在所有迭代中也是一次),导致复杂的解决方案和
Root are related to a1 and a3 are r1 and r2 roots as (a2/a1)^1/n and (a3/a2)^1/n,
therefore the roots are complex conjugate to each other ( r1 and r2 are
the magnitude of the complex number).
Now i want to code that if any of a1, a2, ...., a4 changes the sign it just notifies
that there is a complex root.
Further, I want that as in solution after the 9th iteration the value of a1,..a4 and
r1,...r4 become either **NaN** or **Inf**. I need that the code stop before the value
become either **NaN** or **Inf**.(for ex. in this particluar polynomial code
should stop at 8th iteration which show all the values to be finite.)
Now pick up the value of 'r' for 'a' are related (where the sign changes) and assign it to some parameter.
Further i can do the calculations.
Graeffes函数定义为
function b = graeffe(a)
% a = a(1)*x^4 + ... + a(4) is a cubic.
% b = graeffe(a) is a order 4 whose roots are the squares of the roots of a.
b = [ a(1)^2 a(2)^2-2*a(1)*a(3) a(3)^2-2*a(2)*a(4)+2*a(1)*a(5) ...
a(4)^2-2*a(3)*a(5) a(5)^2 ];
这是我的代码与for循环,我想要与上面相同的结果另外我需要'打印'如果符号更改,根是复杂的
clear all
close all
clc
a = [1 -1 3 1 -4];
t = '\t';
k = 0;
i = 1;
sprintf([' Iter n a1 a2 a3 a4 ', ...
' a5 r1 r2 r3 r4'])
while all(isfinite(a))
k = k+1;
a = graeffe(a);
n = 2^k;
r1 = (a(2)/a(1))^(1/n);
r2 = (a(3)/a(2))^(1/n);
r3 = (a(4)/a(3))^(1/n);
r4 = (a(5)/a(4))^(1/n);
fprintf(['%5.0f %4.0f %5.0f %8.3e | %8.3e | %8.3e | %8.3e |', ...
'%8.6e | %8.6e | %8.6e | %8.6e \n'],k, n, a, r1, r2, r3,r4)
if a(i)=-a(i);
sprintf('roots are complex onjugate ')
end
end