Created
January 26, 2019 07:19
-
-
Save mrfarhadir/4b6094071f8973ae21759fdace1c2673 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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