MATLAB: Newton’s Method Help

MATLABnewton's method

For my Numerical Analysis class we are using Newton's Method to find the roots of a given function. The function given was "x = 2*sin(x)", and the answer we were given was "1.8954942670340", but my code returns -1.4014 after 7 iterations in the loop. For the variable "functn" I subtracted x in the orignal equation to get "2*sin(x) – x = 0". The tolerance we needed for the roots was 1e^-6. There is certainly an issue in the code, I just can't seem to find it. Thanks for reading, and looking this over.
clear all
close all
clc
functn = @(x) 2*sin(x) - x
interval = 0.001; %small x-step used to approximate the function derivative
x = 0.01:0.01:1;
dydx = (functn(x+interval) - functn(x))/interval;
Root_value = -0.57; % Function value
ytolerance = 1e-6; % The convergence tolerance
initial_guess = 0.6; % The initial guess for the location of the root x0
% set the starting function value to the initial guess, and compute the initial error
x=initial_guess;
yerror = Root_value-functn(x);
check=1;
while abs(yerror)>ytolerance
dydx = (functn(x+interval)-functn(x))/interval;
dx = yerror/dydx;
x=x+dx;
disp("x"+check+" = "+x);
disp(" ");
yerror = Root_value - functn(x);
check=check+1;
end

Best Answer

f = @(x) 2*sin(x) - x;
fp= @(x) 2*cos(x) - 1;
tol=1e-10;
yerror=1;
g=1.2;%guess needs to be close enough to the root you are seeking
while abs(yerror)>tol
yerror=f(g)/fp(g);
g=g-yerror;
end