Matlab 编制一个迭代程序,当满足上述情况时,该迭代将停止
大家好,我在为如下所示的算法编写编程代码时遇到了一个问题 当定义为(当前近似值先前近似值)/当前近似值的近似误差小于0.01时,该程序将终止。它可以简化为(f(xr)i+1-f(xr)i)/f(xr)i+1。下面是我编写的代码,我真的很想知道如何编写一个迭代,当遇到上述情况时,迭代将停止Matlab 编制一个迭代程序,当满足上述情况时,该迭代将停止,matlab,Matlab,大家好,我在为如下所示的算法编写编程代码时遇到了一个问题 当定义为(当前近似值先前近似值)/当前近似值的近似误差小于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;
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
xl=input('输入下限:');
xu=输入('输入上限:');
xr=(xl+xu)/2;
R=3;V=30;
fl=(π*R*xl^2)-(π*(xl^3)/3)-V;%在这两者之间是否存在可以调用这些函数的方法
傅=(π*R*xu^2)-(π*(xu^3)/3)-V;除了打字3次以外
fh=(pi*R*xr^2)-(pi*(xr^3)/3)-V;
当相对误差小于0.01时,则显示xr的值
如果fl*fu0
xl=xr;
结束
结束
您忘记执行步骤3(c)
在步骤3(a)和3(b)中,您也没有按照说明状态“返回步骤2”。为此,您需要创建一个while
循环,如前所述;在while循环中加入保持循环的条件。如果该条件评估为false,则应根据步骤3(c)退出循环
继续执行步骤3(a)和3(b)中的“返回步骤2”部分;这会将执行移回循环的顶部。另见
祝你好运。你可以将计算放在一个函数中:
function f = some_function(x)
R = 3;
V = 30;
f = (pi*R*x^2)-(pi*(x^3)/3)-V;
您可以尝试100次传球(为了安全):
i=1:100时的
xr_old=xr
fr_old=fr
xr=(xl+xu)/2;
fr=某些函数(xr);
如果abs((xr-xr\u old)/xr)0:
xl=xr
fl=fr
结束
结束
我现在更新了代码,可以运行了。我用f(x)=x^2-2测试它。它在6次迭代中收敛到1.4141。我建议您将该代码与您必须了解的代码进行比较,了解哪些代码以前不适用于您。这将是一次很好的学习经历
>> example(1,2);
Crossing found after 6 iterations: 1.414062
其中,example.m如下所示:
function xr = root(xl,xu)
MAX_NUMBER_ITERATIONS = 1000;
MAX_DELTA=.01;
numberIterations=0;
xr_old=xu;
xr = (xl+xu)/2;
while ((numberIterations<MAX_NUMBER_ITERATIONS) & (abs(xr_old-xr)>=MAX_DELTA))
numberIterations=numberIterations+1;
xr_old = xr;;
product=f(xl)*f(xr);
if product<0
xu = xr;
xr = (xl+xu)/2;
continue;
elseif product>0
xl = xr;
xr = (xl+xu)/2;
continue;
else
break;
end
end
fprintf('Crossing found after %d iterations: %f\n',numberIterations,xr)
end
function y = f(x)
y=x^2-2;
end
函数xr=root(xl,xu)
最大迭代次数=1000;
MAX_DELTA=.01;
numberIterations=0;
xr_old=xu;
xr=(xl+xu)/2;
而((numberIterations=MAX_DELTA))
numberIterations=numberIterations+1;
xr_old=xr;;
产品=f(xl)*f(xr);
如果产品0
xl=xr;
xr=(xl+xu)/2;
继续;
其他的
打破
结束
结束
fprintf('在%d次迭代后发现交叉:%f\n',numberIterations,xr)
结束
函数y=f(x)
y=x^2-2;
结束
您忘记执行步骤3(c)。您也没有在步骤3(a)和3(b)中“返回步骤2”如说明书所述。嗯……我认为这一条毕竟需要添加一个if语句……但我现在遇到的问题是关于迭代和循环条件……谢谢你提醒是的……顺便问一下,你能帮我解决这个问题吗……我将非常感谢。谢谢!!嗯,但是当相对误差小于0.01,也就是(当前xr上一个xr)时,我停止程序怎么样/当前xr hmm,但问题是我不知道如何在中定义上一个xr和当前xrmatlab@RobertHarvey你能给我一个电话吗help@green:while中的条件必须保持循环,直到满足退出条件。@RobertHarvey:是的。。但现在的关键问题是我想写一些类似while abs的东西((current xr previous xr)/current xr)我建议为循环设置一个固定通过次数的,而不是while
,至少在调试过程中是这样。我的代码通常会出问题,并且对于无限循环,while
条件保持为真。
function xr = root(xl,xu)
MAX_NUMBER_ITERATIONS = 1000;
MAX_DELTA=.01;
numberIterations=0;
xr_old=xu;
xr = (xl+xu)/2;
while ((numberIterations<MAX_NUMBER_ITERATIONS) & (abs(xr_old-xr)>=MAX_DELTA))
numberIterations=numberIterations+1;
xr_old = xr;;
product=f(xl)*f(xr);
if product<0
xu = xr;
xr = (xl+xu)/2;
continue;
elseif product>0
xl = xr;
xr = (xl+xu)/2;
continue;
else
break;
end
end
fprintf('Crossing found after %d iterations: %f\n',numberIterations,xr)
end
function y = f(x)
y=x^2-2;
end