• No results found

3.9.1

The Newton-Raphson method

In engineering we can encounter equations that cannot be reduced to poly- nomials. To solve them we can start with a guess solution, say x0, that we substitute in the equation. In general the result will be not zero, but a num- ber; let it be δ0. We can assume a rule for correcting the initial guess as a function of δ and substitute the corrected value, x1, in the equation. Again the result may be different from zero, say δ1. We apply again the correction rule and continue so until some stopping rule is fulfilled. For example, we can stop the procedure when δi is smaller than some given error limit, or when two successive solutions, xi, xi+1, differ by a number that is less than a given tolerance, ε. We call such a procedure iterative and say that each repetition of the calculation is an iteration, a term that comes from the Latin iter- are. Iterative procedures can be easily programmed in MATLAB by using the scheme

while some condition statements end 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.2 −0.04 −0.02 0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 φ φ − sin φ − 2A/r 2 x0 y 0 x1 y 1

FIGURE 3.16: Solution of Equation 3.25 by the Newton-Raphson procedure

In this subsection we describe the Newton-Raphson procedure, probably the most popular iterative method. To explain it we refer to an example given in Biran and Breiner (2002), Section 7.5. There we calculate the angle, φ, of a segment of a circle that has a given area, A. The equation of the area as a function of the angle is

A = r

2

2(φ− sin φ) (3.25)

We want to find the angle x for which y = x− sin x −2A

r2 = 0 (3.26)

Let us assume the values A = 0.02, r = 1. In Figure 3.16 we plot Equa- tion 3.26 in a guess interval. Assuming a first guess x0 = π/4, we calculate the corresponding value, y0, of the function given by Equation 3.26. It is

not zero. Therefore, we seek a better value by descending (in other cases ascending) along the tangent in y−) to the given curve. The new value is

x1= x0 y0 ˙

y(x0) (3.27)

Equation 3.26 yields for this a new value, y1. As this value is not zero, we repeat the procedure. We end the iterations when we receive an approximation consistent with our needs. The function Newton shown below implements this procedure. It receives four arguments:

1. the handle of the function whose zero we seek; 2. the handle of the derivative of the above function; 3. a guess solution,

4. the tolerance admitted for the stopping rule. When the difference be- tween two successive iterations is less than the given tolerance, the it- erations stop. This argument is optional; when it is not supplied, the function assumes a built-in tolerance, eps.

function x = newton(h, hdot, x0, tol) %NEWTON Newton-Raphson procedure % Input arguments:

% h, handle of given equation defined as

% anonymous function

% hdot, handle of derivative of h, defined as

% anonymous function

% x0, initial guess

% tol, difference between two successive solutions,

% optional

% Output argument: x, solution within approximation tol if nargin == 3

tol = 10^(-6); end

xi = x0; eps = 1;

while eps > tol

y = feval(h, xi);

ydot = feval(hdot, xi); xi1 = xi - y/ydot; eps = abs(xi1 - xi);

xi = xi1;

end x = xi;

In the above function we used nargin, a command that returns the number of input arguments. In our case, if the argument tol is given, the calculations end when the difference between the results of two successive iterations is smaller than tol. If the fourth input argument is not given, the number of input arguments equals 3 and the conditional statement is executed setting tol to 10−6.

As an example, let us assume that the two coordinates of a point, P1, are x1, y1. It is required to find the angle of rotation around the origin, α, that will bring the point to the x−coordinate x2. From Subsection 2.5.2 we know that the relationship between the initial and the rotated x−coordinate is

x2= x1cos α + y1sin α (3.28)

The unknown in our problem is the angle α. Equation 3.28 can be rewritten as an equation in either sin α or cos α and solved as a second-degree equation in the chosen trigonometric function. This requires some algebraic manipulation. To avoid this treatment, we can directly solve the equation in α by some iterative procedure, for example the Newton-Raphson procedure. We begin by entering the data of the problem and by plotting the given point and the circle on which the given point will move during rotation:

 x1 = 3;  y1 = 2;  r = norm([ x1; y1 ])  x2 = 2;  t = 0: 2.5: 90;  xc = r*cosd(t);  yc = r*sind(t);

 hp = plot(xc, yc, ’k--’), grid;  set(hp, ’LineWidth’, 1.3)  axis equal  ht = xlabel(’x’);  set(ht, ’FontSize’, 16)  ht = ylabel(’y’);  set(ht, ’FontSize’, 16)  hold on  point([ x1; y1 ], 0.03)  ht = text(1.01*x1, 1.05*y1, ’P 1’);  set(ht, ’FontSize’, 16)

Next, we define our equation as an anonymous function with the handle h1, and the derivative of the given equation as another anonymous function with the handle h2. Now we call our function newton with the output argument alpha and use the result to plot the new position of the rotated point:

0 0.5 1 1.5 2 2.5 3 3.5 4 0 0.5 1 1.5 2 2.5 3 3.5 x y P 1 P 2

FIGURE 3.17: Rotating the point P1 around origin

 h1 = @(alpha) x1*cosd(alpha) - y1*sind(alpha) - x2;  h2 = @(alpha) -x1*sind(alpha) - y1*cosd(alpha);  alpha = newton(h1, h2, 1) alpha = 22.6198  y2 = -x1*sind(alpha) + y1*cosd(alpha);  point([ x2; y2 ], 0.03)  ht = text(1.01*x2, 1.05*y2, ’P 2’);  set(ht, ’FontSize’, 16)  hold off

As shown in textbooks on numerical methods, the Newton-Raphson proce- dure can fail for certain initial guesses. An example can be found in Exer- cise 3.4.

3.9.2

Solving an equation with the command fzero

MATLAB provides the command fzero for finding one zero of a continuous function of one variable. This command can be called with two arguments, the handle of the function, and an initial guess. Let us return to the example

in the previous section and try to solve it with fzero:  x1 = 3;

y1 = 2; x2 = 2;

h = @(alpha) x1*cosd(alpha) - y1*sind(alpha) - x2; alpha1 = fzero(h, -10)

alpha1 = 22.6199

Alternatively we can call fzero taking as the second argument a guess interval:

 alpha2 = fzero(h, [ -10 -30 ]) alpha2 =

-22.6199

If the given function does not change sign in the guess interval, fzero fails and issues an error message. Try for yourself fzero(h, [ 10 30 ]). While the command roots shows how many zeros a given equation has, the command fzero finds one zero at most. This command can fail or give no results even in apparently simple cases. An example can be possibly found in Exercise 3.4.