octave - Octave中的错误位置方法
问题描述
我编写了一个代码来使用错误位置方法查找根。它给出了正确的答案,但它总是需要尽可能多的迭代。我注意到我的上限永远不会改变,这意味着我的错误将永远保持在高位。任何想法为什么?
fprintf('Be sure to mark all multiplication with a *\n\n\n');
str=input('Give an equation in x with right hand side=0: ', 's');
%User types in their equation
f=inline(str);
%Converts from string to function
xl=input('Choose the lower bound for the guess interval: ');
xh=input('Choose the upper bound for the guess interval: ');
%Input the upper and lower bounds to initial guess
while(f(xl)*f(xh)) > 0
fprintf('There is no root in this interval\n')
%Prompt user the interval is incorrect
xl=input('Choose the lower bound for the guess interval: ');
xh=input('Choose the upper bound for the guess interval: ');
%Input a new upper and lower bound for the initial guess
endwhile
if f(xl) == 0
fprintf('There is a root at the lower bound\n')
%Inform the user their lower bound is located on a root
elseif f(xh) == 0
fprintf('There is a root at the upper bound\n')
%Inform the user their upper bound is located on a root
endif
tol=input('What would you like your lowest error to be: ');
%User inputs their desired error value
for i=1:1000 %Limit total iterations to 1000
xm=((xl*f(xh)-xh*f(xl))/(f(xh)-f(xl))) %Define center value based on method
if f(xl)*f(xm) < 0
xh=xm
else
xl=xm
endif
%Check which half the root is located in
if abs((xh-xl)/2) < tol
break
endif
%Stop loop once error is acceptable
endfor
fprintf('The root: %f\nThe number of iterations: %d\n',xl,i);
%Display the root and number of iterations
解决方案
推荐阅读
- python - 安装后可以从应用程序中删除 Python 文件夹吗?
- javascript - js中的多个if条件没有给出所需的输出
- java - Mapstruct 与 maven-pom 中的其他注释处理器一起
- ruby - 在使用 Ruby2.6 的 Apache Netbeans 11 中,Ruby Gems 获取失败,而它与 Ruby2.4 一起使用
- nginx - 使用 Nginx 作为代理密码时的 Apache Openmeetings 5.0.0-M2 CSRF 攻击
- hybris - Hybris - PromotionSourceRule - 规则条件限定类别总是回退到其他目录
- javascript - 错误:超时 - 5000 毫秒内未调用异步回调(由 jasmine.DEFAULT_TIMEOUT_INTERVAL 设置)
- javascript - Promise.all 不等待承诺
- javascript - 当代码需要设备宽度时,ReactDOMServer 的行为如何?
- ios - 如何在 iOS 中更改导航栏的高度