Skip to content

Instantly share code, notes, and snippets.

@mrfarhadir
Created January 26, 2019 07:19
Show Gist options
  • Save mrfarhadir/4b6094071f8973ae21759fdace1c2673 to your computer and use it in GitHub Desktop.
Save mrfarhadir/4b6094071f8973ae21759fdace1c2673 to your computer and use it in GitHub Desktop.
function root = newton(f,df)
% Newton-Raphson method
% Author: Farhad Mehryari
i = 1;
p0 = 0.5*pi; %شرایط اولیه
N = 100; %حداکثر تعداد تکرار
error = 0.0001; %خطا
while i <= N
p = p0 - (f(p0)/df(p0)); %روش نیوتن
if (abs(p - p0)/abs(p)) < error % شرط خروج از حلقه
break;
end
i = i + 1;
p0 = p;
end
root=p;
end
% Author: Farhad Mehryari
% Web: blog.mrfarhad.ir
% USAGE طریقه استفاده :
f = @(x) x ^ 2 + 3
df = @(x) 2 * x
root = newton(f,df)
root = -7.8708
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment