Write a MATLAB function that implements the Newton-Raphson method. The function
ID: 3671945 • Letter: W
Question
Write a MATLAB function that implements the Newton-Raphson method. The function should have the form function xr = NewtonRaphson(f,df dx,xi,es, imax), where f is a function handle that defines the root-finding problem f(x) = 0, df dx is a function handle for the derivative off, xi is the initial guess, es is the stopping criterion for the relative approximate error defined in class, and imax is the maximum number of allowable iterations. The value of the root is returned in the output argument xr. The function should print the root value, the number of iterations used and the value of the percent approximate error for the last iteration in the command window in a nicely formatted way. In addition, it should substitute the value of the root into the original function and display the result, to check if it makes sense. It should also warn the user in case it had to stop because f'(x_ i) = 0 (and not because the root was found).Explanation / Answer
% Newton Raphson Method clear all close all clc % Change here for different functions f=@(x) cos(x)-3*x+1 %this is the derivative of the above function df=@(x) -sin(x)-3 % Change lower limit 'a' and upper limit 'b' a=0; b=1; x=a; for i=1:1:100 x1=x-(f(x)/df(x)); x=x1; end sol=x; fprintf('Approximate Root is %.15f',sol) a=0;b=1; x=a; er(5)=0; for i=1:1:5 x1=x-(f(x)/df(x)); x=x1; er(i)=x1-sol; end plot(er) xlabel('Number of iterations') ylabel('Error') title('Error Vs. Number of iterations')
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.