(1) Implement Newton\'s method, including a damping factor A. Your code should c
ID: 3282367 • Letter: #
Question
(1) Implement Newton's method, including a damping factor A. Your code should contain a function newton(f, df, ??, 1ambda, tol), f s the function f(x) for which we want to determine a root, df is the derivative function f'(), pO is an initial guess, 1ambda is the damping parameter, and tol is the error tolerance. Your function should terminate, covering the following scenarios . The procedure converges: Stop when lpn+ Pnl tol (i.e. when the method does not improve the approximations by much. return the approximation Pn The procedure does not converge: Either p, -0, or still pntl - Pnl > tol after a large number of steps (specify this yourself, e.g. 1000). return NaNExplanation / Answer
x = initial value // initial guess
h = func(x)/ df(x); //value y which we will update current root
i = 0; // number of iterations
while(abs(h) > tolerance & i<=maxiter)
h = func(x)/ df(x);
x = x - h;
resarray = [resarray, x];
i=i+1;
end
Matlab code:
create a file named newton.m and paste given code into it!
function [xroot, resarray] = newton(func,df,xo,tol,maxiter)
resarray = [];
x=xo;
h = (func(x))/ (df(x));
i = 1;
while(abs(h) > tol & i<=maxiter)
h = func(x)/ df(x);
x = x - h;
resarray = [resarray, x];
i=i+1;
end
xroot = x;
end
Sample Output:
>> f = @(x) (x^2 - 4)
>> df = @(x) (2*x - 4)
>> [r,resarray] = newton(f,df,1,0.001,10)
r = -1.9971
resarray = -0.50000 -1.25000 -1.62500 -1.81250 -1.90625 -1.95312 -1.97656 -1.98828 -1.99414 -1.99707
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.