falsi方法算法



我写了一个falsi方法代码,我在 fa 函数方面遇到了问题。我的一般函数是间隔[a,b] = [2,12],正如我在主脚本中定义的那样:

 a=2;
 b=12;
 %Function
 syms x real;
 f=1.2*sin(x)+2*log(x+2)-5;

我想知道如何正确地将参数 a 传递到函数 fa ,并且通常, falsi方法函数。我想,它不仅仅是" 2",它必须以某种方式是动态的。谢谢您的提示!:)

 function [ x ] = task1_falsi( f, x, a)
 % Defining functions
 a=2;
 fa=feval(f,a);
 f=inline(f);
 fa=inline(fa);
 err=1;
% Falsi's loop.
% Stops when error is negligible or number of iterations becomes  prohibitive
it=0;
while (it<1000) && (err>10e-5)
% Falsi's algorithm
x1=x-feval(f,x)*(x-a)/fa;
% Calculating error
err=abs(x1-x);
% New zero is previous value
x=x1;
it=it+1;
%Showing steps
fprintf('%dt%f n',it, x); 
end
disp('Root:');
disp(x);
disp('Iterations:');
disp(it);
end
  %Task 1
     % Falsi's method algorithm
 function [ x ] = task1_falsi( f, x)
  err=1;
  y = 7;                  % Starting point
 z = 8; 
 f=inline(f);
  % Ending point
  maxIteration = 1000;     % Maximum number of iterations
epsilon = 0.001;        % Accuracy
it=0;      % Vector with number of iterations
xold = z;               % Variable used for calculations
   solutionRegulaFalsix=zeros(10,1);   % Error vector
for  j=0:20
 while (it <= maxIteration)
     x1 = y - ((feval(f,y)*(z-y))/(feval(f,z)-feval(f,y))); % Calculating the root
    it= it+ 1;
    if (abs(x1-xold)<epsilon*abs(x1)) % Accuracy condition
        break;
    else
        xold = x1;
        if feval(f,y)*feval(f,z) > 0
            y = x1;       % Starting point is being changed
        else
            z = x1;       % Ending point is being changed
        end
    end
end
    err = abs(x1-x);     % Calculating the error
   % epsilon = epsilon/10;
   %Showing steps
fprintf('%dt%f n',it, x); 
y=2; 
   z=3;
   disp('Root:');
   disp(x1);
  disp('Iterations:');
   disp(it);
  end
    end

最新更新