大家好,我在编写算法的编程代码时遇到了一个问题,如下所示
当定义为(当前近似值-先前近似值)/当前近似值的近似误差小于 0.01 时,该程序将终止。它可以简化为 (f(xr)i+1 - f(xr)i)/f(xr)i+1。下面是我编写的代码,我真的很想知道如何编写一个迭代,当遇到上述情况时该迭代将停止。
xl = input('Enter lower limit : ');
xu = input('Enter upper limit : ');
xr = (xl+xu)/2;
R = 3; V = 30;
fl = (pi*R*xl^2)-(pi*(xl^3)/3)-V; % between is there anyway can call these functions
fu = (pi*R*xu^2)-(pi*(xu^3)/3)-V; other than typing 3 times
fh = (pi*R*xr^2)-(pi*(xr^3)/3)-V;
while relative error is less than 0.01 then display value of xr
if fl*fu<0
xu = xr;
elseif fl*fu>0
xl = xr;
end
end