冲哇咔咔
2015-12-23, 10:08
使用斯蒂文森迭代求解非线性方程,斯蒂文森迭代公式如下:
%Steffensen迭代解非线性方程
function[r,n]=Steffensen(x0,eps)
%if nargin==2
% eps=1.0e-10; %默认迭代精度
%end
y=myf(x0);
z=myf(y);
r=x0-(myf(x0)-x0)^2/(myf(y)-2*myf(x0)+x0);
n=1;
err=0.1;
while err>eps
x0=r;
r=x0-(myf(x0)-x0)^2/(myf(y)-2*myf(x0)+x0);
err=abs(r-x0);
n=n+1;
if(n>100000)
disp('迭代步数太多,可能不收敛')
return;
end
end
end
function f=myf(x)
%f=3*x.^2-exp(x);
f=2*log(x)+log(3);
end %end f
在命令窗口输入[r,n]=Steffensen(3.5,1.0e-4)时能够输出正常的实数值,但当提高精度后例如输入[r,n]=Steffensen(3.5,1.0e-5)就开始输出0.9144+0.0000i,同是输入format long后再次运行[r,n]=Steffensen(3.5,1.0e-5),输出为0.914378007984216+0.000010915415698i,正常的迭代后解在3.7附近,求大神指教啊!
%Steffensen迭代解非线性方程
function[r,n]=Steffensen(x0,eps)
%if nargin==2
% eps=1.0e-10; %默认迭代精度
%end
y=myf(x0);
z=myf(y);
r=x0-(myf(x0)-x0)^2/(myf(y)-2*myf(x0)+x0);
n=1;
err=0.1;
while err>eps
x0=r;
r=x0-(myf(x0)-x0)^2/(myf(y)-2*myf(x0)+x0);
err=abs(r-x0);
n=n+1;
if(n>100000)
disp('迭代步数太多,可能不收敛')
return;
end
end
end
function f=myf(x)
%f=3*x.^2-exp(x);
f=2*log(x)+log(3);
end %end f
在命令窗口输入[r,n]=Steffensen(3.5,1.0e-4)时能够输出正常的实数值,但当提高精度后例如输入[r,n]=Steffensen(3.5,1.0e-5)就开始输出0.9144+0.0000i,同是输入format long后再次运行[r,n]=Steffensen(3.5,1.0e-5),输出为0.914378007984216+0.000010915415698i,正常的迭代后解在3.7附近,求大神指教啊!