Question

In: Computer Science

How could a root finding algorithm like the bisection method be used to approximate a value...

How could a root finding algorithm like the bisection method be used to approximate a value such as sqrt(3). In other words how can a root finding algorithm find an x value with a given y value? Write a script to illustrate this usage scenario. Compare the output of your script with the result from a calculator. You must use matlab!! using a while loop.

Solutions

Expert Solution

The idea of bisection algorithm is based on the following ideas:

1) If for a given interval, the function changes its sign at the endpoints, it must have atleast 1 root in that interval

2) Keep shortening the interval by bisecting (dividing in half) the original interval (iteratively), untill you reach the shortest such interval which satisfies the required precision or gets limited by the local computation i.e. number of iterations.

Please find the 1) Function script 2) Driver program to call this function script as the following:

NOTE:

- The function script must be kept within a file with the same name as that of the function.

- The function file and the driver program script must be kept in the same folder/location

%================= Function script ================

function [root,iter,error_values] = myBisection(fh,low,high,tol,max_iter)


for i=1:max_iter    % Iterate upto allowed number of iterations
  
mid=(low+high)/2;   % Divide the interval in half

error_values = abs(feval(fh,mid)); % Estimate error

if feval(fh,low)*feval(fh,high)>0   % Verify the change in sigh
    root=NaN;
    iter=NaN;
    error_values = NaN;
    break;
elseif (error_values<tol)           % Verify the tolerance in root estimation
    root = mid;
    iter=i;
    break;
else                                % Reassign the interval
    if(feval(fh,mid)*feval(fh,low)<0)
        high = mid;
    elseif (feval(fh,mid)*feval(fh,high)<0)
        low = mid;
    end
end
  
end
end

%===========================================

The following sample driver script attempts to find the root of sin(x) within the interval [3,4]. Intuitively, it should be . The required precision is 10-4 and maximum allowed iterations are 100.

%============== Driver Script ==============

fh=@(x) sin(x);
low=3;
high=4;
tol=1e-4;
max_iter=100;
[root,iter,error_values] = myBisection(fh,low,high,tol,max_iter)

%=====================================

output:

root =   3.141601562500000

iter =    10

error_values =     8.908910206643689e-06

As one can see, the algorithm find the required root within the precision in 10 iterations itself.

Hope this helps!

************ PLEASE THUMBS UP!!!!!!!!! ************


Related Solutions

Use the bisection method to approximate the root of f(x)=x-cosx in the range [0.0,1.5]. Stop when...
Use the bisection method to approximate the root of f(x)=x-cosx in the range [0.0,1.5]. Stop when the error is less than 0.002%
7. Finding Roots Using the Bisection Method Write a function that implements the "bisection method" for...
7. Finding Roots Using the Bisection Method Write a function that implements the "bisection method" for finding the roots of function. The signature of your function should look like def find_root(f,a,b,n): where n is the maximum number of iterations of to search for the root. The code should follow this algorithm: We are given a continuous function f and numbers a and b and with a<b with f(a)<0<f(b). From the intermediate value theorem we know that there exists a c...
use newtons method with value of x1=3 to approximate the root of equation to 6 decimal...
use newtons method with value of x1=3 to approximate the root of equation to 6 decimal places x^4=4x^2+25
USING BISECTION METHOD, FIND THE ROOT OF 0.5e^x - 5x + 2 = 0 ON THE...
USING BISECTION METHOD, FIND THE ROOT OF 0.5e^x - 5x + 2 = 0 ON THE INTERVAL [ 0 , 1 ] UP TO 3 DECIMAL PLACES. USE NEWTON'S METHOD TO APPROXIMATE THE ROOT OF f(x)=x^2-5    IN THE INTERVAL  [ 2 , 3 ] UP TO 4 DECIMAL PLACES.
To find a positive root for , write a MATLAB script file that uses Bisection method....
To find a positive root for , write a MATLAB script file that uses Bisection method. Choose any initial value that is needed. Use absolute relative approximate error to be less than 0.01. Your code should report the number of iteration and the value of x.
Let . If we use Accelerated Newton-Raphson method to approximate the root of the equation ,...
Let . If we use Accelerated Newton-Raphson method to approximate the root of the equation , which of the following(s) is/are ture: (I)  is multiple root of order (II) Accelerated Newton-Raphson formula is : (III) The sequence  obtained by the Accelerated Newton-Raphson method converge to the root  quadratically.
The Ostrowski method for finding a single root of ?(?)=0 is given by Initial guess ?0...
The Ostrowski method for finding a single root of ?(?)=0 is given by Initial guess ?0 ??=??−?(??)?′(??), ??+1=??−?(??)?′(??) ?(??)?(??)−2?(??). a) Write MATLAB or OCTAVE coding to implement the Ostrowski method. (Hint: You may use the coding of Newton Method given in Moodle pages) b) Use your coding to find a root of the equation (?−2)2−ln(?)=0 With initial guess ?0=1.0 and ?0 = 3.0. Write or print the results in your Homework sheet.
I'm writing a code for the Secant Method for root finding, but my code makes an...
I'm writing a code for the Secant Method for root finding, but my code makes an infinite code of the correct answer. Below is my code, how do I fix it? def f(x): return (x**6)+7*(x**5)-15*(x**4)-70*(x**3)+75*(x**2)+175*x-125 def secant(): p0=float(input('Enter an initial guess for the root ')) p1=float(input('Enter another guess ')) TOL=float(input('Enter a tolerance in decimal form ')) n=15 i=1 while i<=n: p=p1-f(p1)*(p1-p0)/(f(p1)-f(p0)) if abs(p-p1)<TOL: print(p) else: p0=p1 p1=p i=i+1 else: print(p) return p    print(secant())
Use Newton's method to approximate the indicated root of the equation correct to six decimal places....
Use Newton's method to approximate the indicated root of the equation correct to six decimal places. The root of x4 − 2x3 + 4x2 − 8 = 0 in the interval [1, 2] x = ?
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in...
Analyzing Selection Sort Algorithm The selection sort algorithm works by first finding the smallest value in a list and swapping the first value with it, then finding the second smallest value and swapping the second value with it, continuing until all the values are in order. Implement this algorithm, then determine its growth function, and hence the order of the algorithm. Find how many swaps are made. Use Java Code to create algorithm
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT