0001 function [ x,k ] = Bus_Dekker( f,a,b,tol,max )
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 if a>=b;
0015 error('specifed interval invalid');
0016 elseif tol<=0;
0017 error('tolerance must be positive');
0018 elseif max<=0;
0019 error('maximum number of iterations must be positive');
0020 elseif isa(f,'function_handle');
0021
0022 elseif isa(f,'double');
0023 [n,m]=size(f);
0024 if n~=1 && m~=1;
0025 error(['input must be a function handle or vector holding ',...
0026 'polynomial coefficients']);
0027 else
0028 v=poly2sym(f);
0029
0030 f=matlabFunction(v);
0031
0032 end
0033 else
0034 error(['input must be a function handle or vector holding ',...
0035 'polynomial coefficients']);
0036 end
0037
0038
0039 fa=feval(f,a);
0040 fb=feval(f,b);
0041
0042 if fa==0;
0043 x=a; return;
0044 elseif fb==0;
0045 x=b; return;
0046 elseif fa*fb>0;
0047 error('function values at endpoints have the same sign');
0048 end
0049
0050 c=a;
0051
0052 fc=fa;
0053 k=0;
0054
0055 while abs(b-a)>tol && k<=max;
0056
0057 m=(a+b)/2;
0058 s=b-((b-c)*fb)/(fb-fc);
0059
0060 temp=b;
0061
0062 if or((s>b & s<m),(s<b & s>m));
0063 b=s;
0064 else
0065 b=m;
0066 end
0067 fb=feval(f,b);
0068
0069 if fa==0;
0070 x=a; return;
0071 elseif fb==0;
0072 x=b; return;
0073 elseif fa*fb>0;
0074 a=c;
0075 fa=feval(f,a);
0076 end
0077
0078 c=temp;
0079 fc=feval(f,c);
0080
0081
0082 if abs(fa)<abs(fb);
0083
0084 swap=a;
0085 a=b; b=swap;
0086 fa=feval(f,a);
0087 fb=feval(f,b);
0088 end
0089
0090 k=k+1;
0091 end
0092
0093 x=b;
0094
0095 if k==max+1;
0096 disp('maximum number of iterations reached');
0097 end
0098
0099 end
0100
0101
0102
0103
0104