Beruflich Dokumente
Kultur Dokumente
a Single Variable
Finding Root(s)
• General form of Equation of a Single Variable:
• Solution(s) (or root(s)) of f(x) = 0 refers to the point(s) of intersection(s) of f(x) and the x axis.
• Depending on the f(x), it may have a unique solution, multiple solutions, or no solution.
• A root ofan equationcan sometimes be determined analytically resulting in an exactsolution.
For example:
• 1stiteration:
• Calculate midpoint: c1= (a+b)/2
• If f(a) is positive, f(b) is negative, f(c1) is negative, then a is retained and b is replaced by c1[f(a) andf(c1)
have opposite signs]
• If f(a) ispositive,f(b) isnegative,f(c1) ispositive,thenbis retained andais replaced byc1[f(b)and f(c1) have
oppositesigns]
• 2nditeration:
• Calculate midpoint c2= (a + c1)/2or(c1+b)/2= (a+b)/4
• N iteration…..
• To what extend is the number of iteration? Set the tolerance ɛ, then the number of iteration
needed is
ezplot(f,[-2,4])
f(4) =−1.6146
• 1stiteration:
• midpoint , f(1) = 1.5403;
• 2nditeration:
• midpoint , f(2.5) = −1.0029
• 3rditeration:
• Similar to the abovesteps…
Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB
function c = Bisection(f, a, b,kmax,tol)
% Bisection uses the bisection method to find a root of f(x) = 0
% in the interval [a,b].
% c = Bisection(f, a, b,kmax,tol), where
% f is an anonymous function representing f(x),
% a and b are the endpoints of interval [a,b],
%kmaxis the maximum number of iterations (default 20),
%tolis the scalar tolerance for convergence (default 1e4),
% c is the approximate root of f(x) = 0.
ifnargin< 5 ||isempty(tol),tol= 1e4; end
ifnargin< 4 ||isempty(kmax),kmax= 20; end
if f(a)*f(b) > 0
c = 'failure';
return
end
disp(' k a b c (ba)/2')
for k = 1:kmax,
c = (a+b)/2; % Find the first midpoint
if f(c) == 0, % Stop if a root has been found
return
end
fprintf('%3i %11.6f%11.6f%11.6f%11.6f\n',k,a,b,c,(ba)/2)
if (ba)/2 <tol, % Stop if tolerance is met
return
• Assumedf(x) is continuous on an interval [a, b] and has a root, so that f(a) and f(b) have opposite
signs, so f(a)*f(b) < 0. (same assumption as bisection method)
%RegulaFalsiuses theregulafalsimethod to approximate a root of% f(x)=0in the interval [a,b].
%[r, k] =RegulaFalsi(f, a, b,kmax,tol),where
%f is an anonymous function representing f(x),
%a and b are the limits of interval [a,b],
%kmaxis the maximum number of iterations (default 20),
%tolis the scalar tolerance for convergence (default 1e4),
%r is the approximate root of f(x) = 0,
%k is the number of iterations needed forconvergence.
ifnargin< 5 ||isempty(tol),tol= 1e4;end
ifnargin< 4 ||isempty(kmax),kmax= 20;end
c= zeros(1,kmax); %Preallocate
iff(a)*f(b) >0
r= 'failure';
return
end
• In normalRegulaFalsi method, one of the endpoints of the interval remains the same through all
iterations, while the other endpoint advances in the direction of the root.
• Theregulafalsimethod canbe modifiedsuch that both ends of the interval move toward the root,
thus improving therateofconvergence.
• Open method
• Oscillatory convergence, the elements bounce from one side of thefixedpoint to the other
astheyapproachit.
• Inother words,if in a neighborhood of a root, the curve representing g(x)is less steep than theline