dimanche 26 mai 2013

Algorithme de la Méthode de la Position Fausse en Matlab

Algorithme de la Méthode de la Position Fausse en Matlab :

function [x,err,xx] = falsp(f,a,b,TolX,MaxIter)
%bisct.m to solve f(x)=0 by using the false position method.
%input : f = ftn to be given as a string ’f’ if defined in an M-file
% a/b = initial left/right point of the solution interval
% TolX = upperbound of error(max(|x(k)-a|,|b-x(k)|))
% MaxIter = maximum # of iterations
%output: x = point which the algorithm has reached
% err = max(x(last)-a|,|b-x(last)|)
% xx = history of x
TolFun = eps; fa = feval(f,a); fb=feval(f,b);
if fa*fb > 0, error(’We must have f(a)f(b)<0!’); end
for k = 1: MaxIter
xx(k) = (a*fb-b*fa)/(fb-fa); %Eq.(4.3.1)
fx = feval(f,xx(k));
err = max(abs(xx(k) - a),abs(b - xx(k)));
if abs(fx) < TolFun | err<TolX, break;
elseif fx*fa > 0, a = xx(k); fa = fx;
else b = xx(k); fb = fx;
end
end
x = xx(k);
if k == MaxIter, fprintf(’The best in %d iterations\n’,MaxIter), end

Aucun commentaire:

Enregistrer un commentaire