首页 > 解决方案 > 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

标签: octave

解决方案


推荐阅读