Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Newton Raphson and Secant Method Consider the equation f(x) = tan(pi*x) - x - 6

ID: 3799931 • Letter: N

Question

Newton Raphson and Secant Method

Consider the equation

f(x) = tan(pi*x) - x - 6

a) Write a MATLAB function program that implements the Newton-Raphson method to solve f(x) = 0.

b) Write another MATLAB function that implements the secant method.

c) Using the initial guesses of xl = 0.48 (and x0 = 0.54 for the secant method) write out explicitly your calculations (do this by hand) for the first two steps of each method. List the number of iteration, previous estimate of the root, most updated estimate of the root and the approximate relative error.

d) Use both programs to find the root of the above equation to within an approximate relative error of 10-6. Design your MATLAB codes as such to output the number k of each iteration, the previous and new estimate of the solution and the relative approximate error. Limit your code to run no more than 25 iterations.

e) Plot the relative approximate error as a function of iteration (on a lin-log plot). For each method, which has the steepest slope and why (a qualitative answer is good enough) ?

Explanation / Answer

a)function [x,iter]=newton(x0,f,fp) % newton-raphson algorithm N = 100; eps = 1.e-5; % define max. no. iterations and error maxval = 10000.0; % define value for divergence xx = x0; while (N>0) xn = xx-f(xx)/fp(xx); if abs(f(xn))maxval disp(['iterations = ',num2str(iter)]); error('Solution diverges'); break; end; N = N - 1; xx = xn; end; error('No convergence'); break; % end function

b)function [x,iter]=secant(x0,x00,f) % newton-raphson algorithm N = 100; eps = 1.e-5; % define max. no. iterations and error maxval = 10000.0; % define value for divergence xx1 = x0; xx2 = x00; while N>0 gp = (f(xx2)-f(xx1))/(xx2-xx1); xn = xx1-f(xx1)/gp; if abs(f(xn))maxval iter=100-N; disp(['iterations = ',num2str(iter)]); error('Solution diverges'); abort; end; N = N - 1; xx1 = xx2; xx2 = xn; end; iter=100-N; disp(['iterations = ',iter]); error('No convergence'); abort; % end function