from
writing fortran mex file
by Alain Barraud
fortran mex file which call user matlab function with varargin argument.
|
| fzdm(f,a,b,tol,varargin)
|
function [x,k]= fzdm(f,a,b,tol,varargin)
% find zero of f(x) by dichotomy in the interval [a b]
% f(a)*f(b)<0 by hypothesis
%
% tol is the stopping criterion
% varargin is additionnal user parameters of f(x)
%
% on return
% k is the number of calls of f(x)
% x is the zero of f(x)
%
% the function f is as follows
% function fx=f(x,varargin)
%
% author Alain barraud mars 2009
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x=(a+b)/2;k=0;
while abs(b-a)>tol*abs(x)+1.e-1*tol
fx=f(x,varargin{:});k=k+1;
if fx>0
b=x;
elseif fx<0
a=x;
else
a=x;b=x;
end
disp(['k, x, fx : ',num2str(k),' ',num2str([x,fx],'%25.15e')]);
x=(a+b)/2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
|
|
Contact us at files@mathworks.com