Sie sind auf Seite 1von 35

Numerical Solution of Equations of

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:

Solved analytically to get the solution:

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


• Many equations cannot be solved analytically, then seek the solution numerically.
For example:

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


Numerical Solution of Equations

(False Position Method)

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB
Bisection Method
• It is assumed f(x) is continuous on an interval [a, b] and has a root, so that f(a) and f(b) have
theopposite signs, so f(a)*f(b) < 0.

• 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

• Similar to 1stiteration step, look for opposite signs

• N iteration…..

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB
• The absolute error associated withnth (n=number of intervals, start from [a,b]) iteration satisfies

• To what extend is the number of iteration? Set the tolerance ɛ, then the number of iteration
needed is

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


f = @(x)(x*cos(x)+1);

ezplot(f,[-2,4])

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


• Define
f(-2) = 1.823

f(4) =−1.6146

• 1stiteration:
• midpoint , f(1) = 1.5403;

• since then , the root must be in [1, 4]

• 2nditeration:
• midpoint , f(2.5) = −1.0029

• since then b = c2= 2.5,the root must be in [1,2.5]

• 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 1e­4),
% c is the approximate root of f(x) = 0.
ifnargin< 5 ||isempty(tol),tol= 1e­4; end
ifnargin< 4 ||isempty(kmax),kmax= 20; end
if f(a)*f(b) > 0
c = 'failure';
return
end
disp(' k a b c (b­a)/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,(b­a)/2)
if (b­a)/2 <tol, % Stop if tolerance is met
return

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


f = @(x)(x*cos(x)+1);
ezplot(f,[-2,4])
c=Bisection(f, ­2, 4, [], 1e­2)

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


• MATLAB:fzero
• Function, uses a combination of thebisection,secant, andinverse quadraticinterpolation
methods.
• >>fzero(f,1)
ans=
2.0739
• >>fzero(f,[1,3])
ans=
2.0739
• The user-definedBisectionfunction on the example can be improved by choosing smaller
tolerance.tol= 1e­8results a root estimate of2.0739which requires 30 iterations.

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


Regula Falsi Method (Method of False Position)

• 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)

• Bracketing method to find a root of f(x) = 0


• Concept: connect 2 points by a line equation

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


• Line connecting point A to point B

• Find x-intercept, by setting y = 0, solve x = c1

• Generalizing the result,the sequence of points that


converges to the rootis generatedvia

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


function[r, k]=RegulaFalsi(f, a, b,kmax,tol)

%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 1e­4),

%r is the approximate root of f(x) = 0,

%k is the number of iterations needed forconvergence.

ifnargin< 5 ||isempty(tol),tol= 1e­4;end

ifnargin< 4 ||isempty(kmax),kmax= 20;end

c= zeros(1,kmax); %Pre­allocate

iff(a)*f(b) >0

r= 'failure';

return

end

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


disp(' k a b')
for k = 1:kmax,
c(k) = (a*f(b)­b*f(a))/(f(b)­f(a));% Find thex­intercept
iff(c(k)) == 0% Stop if a root has beenfound
return
end
fprintf('%2i %11.6f%11.6f\n',k,a,b)
iff(b)*f(c(k)) > 0% Check signchanges
b= c(k);% Adjust the endpoint of interval
elsea = c(k);
end
c(k+1) = (a*f(b)­b*f(a))/(f(b)­f(a));% Find the nextx­intercept
ifabs(c(k+1)­c(k)) <tol,% Stop if tolerance is met
r= c(k+1);
return
end
end

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB
Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB
Modified Regula Falsi Method

• 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.

• One example of modified method, apply instead of

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB
Fixed-Point Method

• Open method

• Rewrite f(x) = 0 as x = g(x); g(x) is called aniteration


function

• A point of intersection of y = g(x) and y = x is called


afixed pointof g(x)

• A fixed point of g(x) is also a root of the original


equation f(x) = 0

• Afixedpoint ofg(x) is found numerically via thefixed-point


iteration [equation 3.4] in the text book]:

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


• Steps:
• Start with initial guess x1near the fixed point

• x2is found byx2= g(x1)

• x3is found byx3=g(x2)

• and so on… until converged, i.e. two successive

points are within prescribed distance, or:

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


• Monotoneconvergence, the elements ofthe generatedsequence converge to thefixedpoint from
oneside

• Oscillatory convergence, the elements bounce from one side of thefixedpoint to the other
astheyapproachit.

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


• Selection of asuitableiteration function:
• It may be morethan one way to rewrite a givenequationf(x) = 0 asx=g(x).
• Insome cases, more than one ofthe possible forms of g(x)can be successfully used. Sometimes, none of the
forms is suitable,which meansthat the rootcannotbe found by thefixed-pointmethod.
• Whenthere aremultiple roots, one possible form may be used tofindone root, while another form leads
toanother root.
• An iteration functiong(x) must be suitably selected so that when used in Equation 3.4, the iterations
converge to the fixed point, by applyingTheorem 3.1: Convergence of Fixed-Point Iteration:

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


• A note on Convergence:
• FollowingTheorem 3.1, if|g′(x)|< 1near afixedpoint ofg(x), convergence isguaranteed.

• Inother words,if in a neighborhood of a root, the curve representing g(x)is less steep than theline

y=x, then thefixed-pointiteration converges to that root. Note that this is

asufficient,andnotnecessary, condition for convergence.

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


function [r, n] =FixedPoint(g, x1,kmax,tol)
%FixedPointuses the fixed­point method to approximate
% afixedpointofg(x).
% [r, n] =FixedPoint(g, x1,kmax,tol),where
%g is an anonymous function representing g(x),
%x1 is the initialpoint,
%kmaxis the maximum number of iterations (default 20),
%tolis the scalar tolerance for convergence (default 1e­4),
%r is the approximate fixed point of g(x),
% n is the number of iterations needed for convergence.
ifnargin< 4 ||isempty(tol),tol= 1e­4; end
ifnargin< 3 ||isempty(kmax),kmax= 20; end
x = zeros(1,kmax);
x(1) = x1;
for n = 1:kmax,
x(n+1) = g(x(n));
ifabs(x(n+1) ­ x(n)) <tol
r= x(n+1);
return
end
end
r = 'failure';% Failure to converge afterkmaxiterations

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB
Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB
Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB
• Is it convergence?

Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB


Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB
Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB
Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB
Esfandiari, Numerical Methods for Engineers and Scientists Using MATLAB

Das könnte Ihnen auch gefallen