Exercise 7
Solving Roots of Equations Using Open Methods
This exercise utilizes the function implementing the Newton-Raphson method and allows you to experiment on
different sets of data. At the end of this exercise, you’ll be asked to create a new function implementing the
Chebyshev’s method, which is done by refining the given function.
Learning Objectives:
1) Apply Newton-Raphson method in solving the roots of equations in order to learn how the
algorithm behave
2) Test the function using different sets of data in order to determine its roots
3) Plot the equation using the given sets of data in order to view its figure and validate the roots
4) Modify the function to implement the Chebyshev’s method such that it can be applied to solve the
roots in an alternative manner
5) Use the Matlab's fzero() function in order to verify the roots of the given equation
I. Newton-Raphson Method
1. Open the script editor in Matlab and type in the following program (newton.m).
function [x,fx,xx] = newton(f,df,x0,tolX,maxSteps)
%inputs:
% f = function to be given as a string ’f’
% df = df(x)/dx (If not given, numerical derivative is used.)
% x0 = the initial guess of the solution
% tolX = the upper limit of |x(k) - x(k-1)|
% maxSteps = the maximum # of iteration
%outputs:
% x = the point which the algorithm has reached
% fx = f(x(last))
% xx = the history of x
h = 1e-4; h2 = 2*h; tolFun=eps;
if nargin == 4 & isnumeric(df)
maxSteps = tolX;
tolX = x0;
x0 = df;
end
xx(1) = x0;
fx = feval(f,x0);
for k = 1: maxSteps
if ~isnumeric(df)
dfdx = feval(df,xx(k)); %derivative function
else
dfdx = (feval(f,xx(k) + h)-feval(f,xx(k) - h))/h2;
%numerical drv
end
Numerical Methods Lab Exercise 7 – Solving Roots of Equations Using Open Methods
dx = -fx/dfdx;
xx(k+1) = xx(k)+dx;
fx = feval(f,xx(k + 1));
if abs(fx)<tolFun | abs(dx) < tolX
break;
end
end
x = xx(k + 1);
if k == maxSteps
fprintf(‘The best in %d iterations\n’,maxSteps)
end
2. Save the file and execute the function using y = 6x3 – 9x + 4 with the following values of x0.
Please recall the use of the inline function and how equations are written in Matlab.
x0 = 1, x0 = 0, and x0 = -1.
3. Make a table of each of these values indicating x and fx.
4. Create x1 as a vector of double values from -2 to 2 with an interval of 0.1.
5. Create y1 as the result of the given function using x1 values (i.e., y1 = 6*x1^3 – 9*x1 + 4).
6. Plot x1 and y1 to view the graph of the function and enable its grid.
7. Use the newton method to determine the smallest positive root of the following functions:
a. f2 = 4x7 – 3x-7 + 9x
b. f3 = (x3 – x) sin x
c. f4 = (4x3 − 7x + 8)/x
8. Make a table of x and fx in each function with the corresponding index number (i).
9. Include a graph showing each function with x = -3 through 3 at steps of 0.1.
Machine Problem:
1. Modify the newton.m program to implement the Chebyshev’s method. Name this script as chebyshev.m.
2. Test this program using the following functions:
a. f5 = 4x7 – 3x-7 + 9x
b. f6 = (x3 – x) sin x