0% found this document useful (0 votes)
169 views

Numerical Computing Lab 3

The document discusses solving nonlinear equations graphically and numerically using the bisection method in MATLAB. It begins with an introduction to graphical methods using MATLAB plot commands. It then provides an example of using graphs to find the approximate root of an equation. The document next describes the bisection method, which iteratively narrows down the interval containing the root. Pseudocode is provided for the bisection algorithm. MATLAB code is given for bisection functions with and without graphical output. An example bisection problem and solution is included to demonstrate the method.

Uploaded by

Aamna Imran
Copyright
© Attribution Non-Commercial (BY-NC)
0% found this document useful (0 votes)
169 views

Numerical Computing Lab 3

The document discusses solving nonlinear equations graphically and numerically using the bisection method in MATLAB. It begins with an introduction to graphical methods using MATLAB plot commands. It then provides an example of using graphs to find the approximate root of an equation. The document next describes the bisection method, which iteratively narrows down the interval containing the root. Pseudocode is provided for the bisection algorithm. MATLAB code is given for bisection functions with and without graphical output. An example bisection problem and solution is included to demonstrate the method.

Uploaded by

Aamna Imran
Copyright
© Attribution Non-Commercial (BY-NC)
You are on page 1/ 8

Lab 3: Solution of non-linear equations: Graphical Method and Bisection Method

1|P ag e

OBJECT: Solution of non-linear equations: Graphical Method and Bisection Method Implementation in MATLAB 3.1 INTRODUCTION Solutions of the nonlinear equation, f(x) = 0, whether it is a scalar equation or a set of simultaneous equations, are called zeros or roots of f(x). In this section, we study the methods, such as the graphic, bisection and successive substitution methods, to find the real roots of scalar nonlinear equations. 3.2 GRAPHICAL METHOD Before studying the graphical method, an introduction to graph plotting in MATLAB is necessary The basic plot command Two-dimensional line and symbol plots are created with the plot command. In its simplest form plot takes two arguments >> plot(xdata, ydata) where xdata and ydata are vectors containing the data. Note that xdata and ydata must be the same length and both must be the same type, i.e., both must be either row or column vectors. Additional arguments to the plot command provide other options including the ability to plot multiple data sets, and a choice of colors, symbols and line types for the data. A simple line plot Here are the MATLAB commands to create a simple plot of y = sin(3*pi*x) from 0 to 2*pi. >> x = 0:pi/30:2*pi; % x vector, 0 < = x < = 2*pi, increments of pi/30 >> y = sin(3*x); % vector of y values >> plot(x, y) % create the plot >> xlabel('x (radians)'); % label the x-axis >> ylabel('sine function'); % label the y-axis >> title('sin(3*x)'); % put a title on the plot

Numerical Computing

Prepared by: Engr. Aamna Khalid

Lab 3: Solution of non-linear equations: Graphical Method and Bisection Method

2|P ag e

For including the grid lines in MATLAB the grid on command can be used. Similarly, for switching on or switching off the axes, axis on and axis off commands can be used respectively. The graphical method can now be understood clearly through an example. Suppose we wish to find a positive root of f(x) = 0 where f(x) = x sin(l/x) - 0.2 exp(-x) (3.2) (3.1)

If you ask a mathematician to find the solution immediately, he will perhaps look at the equation for a minute. After figuring out that as x approaches 0 from the positive side of x, sin(l/x) oscillates with an increasing frequency and becomes singular at x = 0, he would start sketching x sin(l/x) and 0.2exp(-x). After a few trials, a neat graph would be drawn, done by hand on a piece of scratch paper. From the figure, it would be found that there is only one root, approximately 0.4. After this, a computer program may be used to find a more accurate value; however, the most crucial part of the solution has been accomplished by the graphic method. Knowing the behavior of the function, the number of roots, and its approximate value, the rest can be easily done by a computer program, or even with a hand-held calculator. The approach of the mathematician is exactly what we will do with the graphics of MATLAB. Wwe can easily plot y = xsin(l/x) and y = 0.2exp(-x), as illustrated in Figure 3.1. Alternatively, f(x) = xsin(l/x) - 0.2exp(-x) may be directly plotted, as shown in Figure 3.2. The figures show that the root is approximately 0.38. The graphic method can still be used to find a more accurate value by zooming up the plot. It would be more efficient, however, if one of the methods discussed in the following sections is applied in order to calculate much more accurately

Figure 3.1 Example 3.1

Figure 3.2

Numerical Computing

Prepared by: Engr. Aamna Khalid

Lab 3: Solution of non-linear equations: Graphical Method and Bisection Method

3|P ag e

The natural frequencies of vibration of a uniform beam are clamped at one end is the solution of the following equation: cos(x) cosh (x) + 1 = 0 (A) where x = p2L/EI L = length of the beam (m) w = frequency (s-1) El = flexural rigidity(Nm2) p = density of the beam material (kg/m3) Determine approximate values of the lowest three positive roots by the graphic method. Solution We first set f(x) = cos(x) cosh(x) + 1 (B) Because we know little of the function, a graphical analysis at this point is helpful. We first plot without limits of y in 0 < x < 20. The plotted graph is shown in Figure 3.3 (see List 3.1 for the, script to plot). From this figure, we learn that one root is approximately x = 17.5, but other roots may also exist in 0 < x < 15.

Figure 7.3 Plot of y cos(x) cosh(x) + 1. List 3.1


clf;clear x = 0:0.1:20; y = cos(x).*cosh(x) + 1; plot(x, y, x, zeros(x)); xlabel('x'); ylabel('y = cos(x)*cosh(x)+l')

While the figure plotted by the foregoing script is still on the screen, the limits of the graph may be changed by the axis command or manually by zooming. For example, axis([0 20 -10 20]) changes the graph to that shown in Figure 3.4. By reading from Figure 3.4, the three smallest positive roots are found to be x = 1.8, 4.6, and 7.8, approximately.

Numerical Computing

Prepared by: Engr. Aamna Khalid

Lab 3: Solution of non-linear equations: Graphical Method and Bisection Method

4|P ag e

Figure 3.4 Zoomed plot of y = cos(x) cosh(x) + 1. ______________________________________________________________________________ As is obvious from the example just described, the graphic method is not without difficulties. One problem is that a graph may be very poorly depicted in some cases. For example, the quality of plot becomes poor in the vicinity of a singularity. If a graph is carelessly plotted, singular points may look like roots. When a singularity is suspected, zoom up the graph and then determine if the function is really singular. Another example is that if a function is plotted with equispaced intervals, fast oscillation may not be captured, so the plotted curve may significantly misrepresent the true function. It is advisable, therefore, to plot the function several times with a finer grid, different zooming, and focus until the function is well understood. 3.4 BISECTION METHOD The bisection method is a simple but robust numerical method for finding one real root in a given interval where the root is known to exist. Its unique advantage is that it works even for nonanalytic functions; however, the method should be used only after a graphic analysis. Suppose that a root of f(x) = 0 is located in an interval between x = a and x = c denoted by [a,c], or equivalently, a < x < c. The bisection method is based on the fact that the sign of y(x) at x = a and x = c are opposite, namely, f(a)f(c) < 0 (see Figure 3.5). We now bisect the interval [a, c] into two halves, namely, [a, b] and [b, c], where b = (a + c)/2 (see circle 1 in Figure 3.5). By checking the signs of f(a)f(b) and f(b)f(c), the half interval that contains the root is found. Indeed, since f(b)f(c) < 0, the interval [b, c] including x = b and x = c has the root, otherwise, the root would be in the other interval [a, b]. The new interval containing the root is bisected again.

Numerical Computing

Prepared by: Engr. Aamna Khalid

Lab 3: Solution of non-linear equations: Graphical Method and Bisection Method

5|P ag e

Figure 3.5 Bisection Method As this procedure is repeated, the size of the interval containing the root becomes smaller and smaller. In each step, the midpoint of the interval is taken as the most updated approximation for the root. The iteration is stopped when the half interval size is less than a given tolerance. The interval size after n iterations becomes (3.3) Where a0 and c0 are initial values of a and c, so the numerator is the initial interval size. Equation 3.3 equals the maximum possible error when the root is approximated by the nth midpoint. Therefore, the number of iteration steps required is the smallest integer n satisfying (3.4) where is the tolerance. For example, if c0 a0 = 1 and r = 0.0001, the n = 14.

Two functions, bisec_g, and bisec_n, may be used for bisection computations. The former graphically displays the progress c bisection iteration, as illustrated in Figure 3.5. The latter does not plot graph, but is faster. The synopsis for bisec_g is as follows: bisec_g('f_name', a, c, xmin, xmax, n_points) where f _name is the name of the function that defines the equation to be solved, a and c are endpoints of the initial interval, xmin and xmax are minimum and maximum x values of the graph, and n points is the number of points to plot the function. The tolerance is set to = 10-6 by default. Bisection m-file Purpose: To find a root of a function. Synopsis: bisec_n('f_name', a, b) f _name: the name of the function a and b: endpoints of the initial interval Example: bisec_n('eqn_w3', 0, 1.3) bisec_n.m function bisec_n(f_name, a,c) f _name % a, c : endpoints of initial interval % tolerance : tolerance

Numerical Computing

Prepared by: Engr. Aamna Khalid

Lab 3: Solution of non-linear equations: Graphical Method and Bisection Method

6|P ag e

% it_limit : limit of iteration number % Y_a, Y_c : y values of the current end points % fun_f(x) : functional value at x fprintf( 'Bisection Scheme\n\n' ); tolerance = 0.000001; it_limit =30; fprintf( ' It. a b c fa=f(a) '); fprintf( ' fc=f(c) abs(fc-fa) \n' ); it = 0; Y_a = feval(f_name, a ); Y_c = feval(f_name, c ); if ( Y_a*Y_c > 0 ) fprintf('\n \n Stopped because f(a)f(c) > 0 \n' ); else while 1 it = it + 1; b = (a + c)/2; Y_b = feval(f_name, b ); fprintf (%3.0f %10.6f %10.6f, it, a, b ); fprintf(%10.6f %10.6f %10.6f, c, Y_a, Y_c ); fprintf( '%12.3e \n', abs(Y_c - Y_a)); if (abs(c-a)/2<=tolerance ) fprintf( ' Tolerance is satisfied. \n' ); break fprintf( '\n Change a or b and run again.\n' ); end if ( it>it_limit ) fprintf('Iteration limit exceeded.\n' ); break end if( Y_a*Y_b <= 0 ) c = b; Y_c = Y_b; else a = b; Y_a = Y_b; end end fprintf ('Final result: Root = %12.6f \n', b ); end eqn_w3.m function y = eqn_w3(x) y = sqrt(x^2 + 1) tan(x); ______________________________________________________________________________ Example 3.2 Find the intersection of the following two functions: y= y = tan(x), 0 < x < /2 Solution The problem is equivalent to finding a zero of

Numerical Computing

Prepared by: Engr. Aamna Khalid

Lab 3: Solution of non-linear equations: Graphical Method and Bisection Method

7|P ag e

f=

- tan(x),

0 < x < /2

(A)

By plotting the function, we find the root is approximately x = 0.9. To use bisec_n (or bisec_g), we write a function m-file to define Eq.(A): function f = fun_ex2(x) f = sqrt(l+x.^2) - tan(x); Then, bisec_n is used as bisec_n('fun_ex2', 0.8, 1.0) The output is Bisection Scheme It. a b c f(a) f(b) f(c)

1 0.800000, 0.900000 1.000000,

0.250986, 0.085204, -0.143194

2 0.900000, 0.950000 1.000000,

0.085204, -0.019071, -0.143194

3 0.900000, 0.925000 0.950000,

0.085204, 0.035236, -0.019071

4 0.925000, 0.937500 0.950000, 5 0.937500, 0.943750 0.950000, 6 0.937500, 0.940625 0.943750, 7 0.940625, 0.942187 0.943750, 8 0.940625, 0.941406 0.942187, 9 0.941406, 0.941797 0.942187, 10 0.941406, 0.941602 0.941797, 11 0.941406, 0.941504 0.941602, 12 0.941406, 0.941455 0.941504, 13 0.941455, 0.941479 0.941504,

0.035236, 0.008660, -0.019071 0.008660, -0.005056, -0.019071 0.008660, 0.001838, -0.005056 0.001838, -0.001600, -0.005056 0.001838, 0.000122, -0.001600 0.000122, -0.000738, -0.001600 0.000122, -0.000308, -0.000738 0.000122, -0.000093, -0.000308 0.000122, 0.000014, -0.000093 0.000014, -0.000040, -0.000093

Numerical Computing

Prepared by: Engr. Aamna Khalid

Lab 3: Solution of non-linear equations: Graphical Method and Bisection Method

8|P ag e

14 0.941455, 0.941467 0.941479, 15 0.941455, 0.941461 0.941467, 16 0.941461, 0.941464 0.941467, 17 0.941461, 0.941463 0.941464, 18 0.941461, 0.941462 0.941463, 19 0.941461, 0.941462 0.941462, Tolerance is satisfied. Final result: Root = 0.941462

0.000014, -0.000013, -0.000040 0.000014, 0.000001, -0.000013 0.000001, -0.000006, -0.000013 0.000001, -0.000003, -0.000006 0.000001, -0.000001, -0.000003 0.000001, -0.000000, -0.000001

(3.1) Find approximate values of the solutions for the following equations by the graphic method: (a) 0.5 exp(x/3) - sin(x) = 0, x > 0 (b) log(l + x) - x2 = 0 (3.2) Find all the positive solutions of the following equations approximately by the graphic method: (a) tan(x) - x + 1 = 0, 0 < x < 3 (b) sin(x) - 0.3ex = 0, x > 0 (c) 0.1x3 - 5x2 - x + 4 + e-x = 0 (3.3) The surface configuration of the NACA0012 airfoil of chord length 1 m and maximum thickness of 0.2 m is given by y(x) = [0.2969v^r - 0.126x - 0.3516x2 + 0.2843x3 - 0.1015x4] where + and - signs refer to upper and lower surfaces, respectively. The design engineer has to find the following information: (a) The x coordinate where the airfoil thickness becomes the maximum. (b) The x and y coordinates of the airfoil where thickness becomes half of the maximum. Find the answers by the bisection method. (Hint: Consider solving y'(x) = 0 for the first question.)

Numerical Computing

Prepared by: Engr. Aamna Khalid

You might also like