Write a MATLAB function, root = newton(x, stop, func), to solve for the root of
ID: 3872118 • Letter: W
Question
Write a MATLAB function, root = newton(x, stop, func), to solve for the root of any general user-defined function using the Newton-Raphson method.
The newton function should accept as input arguments the initial guess, x, the stopping criterion, stop, and the userdefined anonymous function called “func.”
The user-defined function should be defined at the command line prior to invoking the newton function. The derivative of the function should be calculated by a function called deriv(x, h), using the Forward Difference formula, double precision, and an appropriate step size (h) for double precision variables, which will be passed to it from the newton function. Use your program to solve the equation below, using an initial guess of x = 2.0 and a stopping criterion of stop = 0.001:
f(x) = x + 2 – (e^x) = 0.
x, h, func x, stop, Cmd func Line K newton deriv func root f(x) f(x) Figure 1 - Data Flow DiagramExplanation / Answer
function root=Newton(x,tol,func)
h=0.01 %step size
erorr=0.1;
while(erorr>tol&n<100)
root=x-(func(x)/deriv(x,h)); %newton method
erorr=abs((x-root)/root);
x=root;
n=n+1;
end
end
function df=deriv(x,h)
df=(func(x+h)-func(x))/h
end
function f=func(x)
f=x-0.8-0.2*sin(x) ;% you have to give your example, i just given my example for understanding
end
%Note: All three functions should be save in same folder.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.