Question
Please help.
Solving Nonlinear Equations by the Bisection Method Write a MATLAB program for solving nonlinear equations f(x) = 0 by using the bisection method. The program should be written in an M-file: function [ ] = filename (a, b, eps) In the beginning of the program, use the Intermediate Value Theorem to check if the given problem has a solution. If not, quit the program with the suitable printout. Use a formula to give N, the least number of iterations. In the program, use a loop to perform the iterations. For example, you may use: Test your program on the following problems: f(x) = l + ln(l + x2), a = 0,b = 1 with |cN ? r|
Explanation / Answer
function root = bisection(f,a,b,epsilon) % Program : bisection.m % % Purpose : find the root of a given single variable function within the % given interval% % Author : Aamir Alaud Din % Inputs : At least three input arguments are required viz., function, initial point of the % interval, and final point of the interval % % Syntax : root = bisection(f,a,b,epsilon) % % Example: root = bisection(f,1,2,1e-9) % where f is again an inline function as mentioned above if (f(a)*f(b) >= 0) disp('The function has positive value at the end points of interval.'); disp('The root can''t be found using bisection method, use some other method.'); root = 'Root can''t be found using bisection method'; return; else fprintf('Iteration a b m f(a) f(b) f(m) '); fprintf('========= ====== ====== ====== ====== ====== ====== '); m = (a + b)/2; if (abs(f(m)) = epsilon) Iter = Iter + 1; m = (a + b)/2; if(f(a)*f(m) > 0) a = m; else b = m; end fprintf('%4i %12.7f %12.7f %12.7f %12.7f ',Iter,a,b,m,f(m)); end root = m; end end end