⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 falspos.m

📁 matlab code for new user to design any project
💻 M
字号:
% Demonstration of Root Finding using False Position
%
% The function is defined in the file 'fcn.m'
%      function y = fcn(x)
%          y = 9.8 * 68.1 * (1-exp(-10*x/68.1))/x - 40;

clear;

      xl = input ('Enter lower bound of root bracket:  ');
      xu = input ('Enter upper bound of root bracket:  ');
maxerror = input ('Enter maximum error (percent):      ');
   maxit = input ('Enter maximum number of iterations: ');

fplot('fcn',[xl xu]); grid on; hold on;

count = 0;          % iteration counter
rel_error = 1;      % to force entry into while loop
xr = xu;            % require starting value in loop below

fprintf('\n  #         xl         xu         xr      error\n\n');

% main loop until change between iterations small enough

while (rel_error > maxerror) & (count < maxit) 
  count = count + 1;
  xrold = xr;

  xr = xu - fcn(xu)*(xu-xl)/(fcn(xu)-fcn(xl));
  plot(xr,fcn(xr),'ro');

  if xr ~= 0
    rel_error = abs((xr - xrold)/xr) * 100;
  end
  fprintf('%3g %10g %10g %10g %10.4f\n',count,xl,xu,xr,rel_error)

  test = fcn(xl) * fcn(xr);  % form test product

  if test == 0
    rel_error = 0;  % root is at xr
  elseif test < 0
    xu = xr;        % root is below xr
  else
    xl = xr;        % root is above xr
  end

end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -