-
Notifications
You must be signed in to change notification settings - Fork 0
/
newton.m
53 lines (43 loc) · 1.3 KB
/
newton.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
%% H synarthsh newton ylopoiei th me8odo tou Newton gia thn
%% eyresh ths rizas ths synarthshs f(x) = x^2*sin(x) me arxikh timh to x0.
%% x0 = arxikh timh
%% tol = anoxh sto sfalma
%% maxiter = megisto plh8os synarthsewn
%% xstar = proseggish thw rizas
%% fxstar = timh ths synarthshs sth riza
%% iter = plh8os epanalhpsewn poy ektelesthkan
%%
function [xstar, fxstar, iter] = newton(x0, tol, maxiter)
f = inline('x*x*sin(x)'); %% h ekfrash metatrepetai se synarthsh
df = inline('2*x*sin(x) + x^2*cos(x)'); %% ypologismos ths paragwgoy
xold = x0;
fxold = f(xold);
dfxold = df(xold);
iter = 0;
if abs(dfxold) <= eps
fprintf('H paragwgos mhdenizetai sto %f kai h me8odos Newton den mporei na synexisei.\n',xold);
xstar = xold;
fxstar = f(xstar);
return;
end
xnew = xold - fxold/dfxold;
fxnew = f(xnew);
iter = 1;
while (iter < maxiter) & (abs(xold-xnew) > tol) & (abs(fxnew) >tol)
xold = xnew;
fxold = fxnew;
dfxold = df(xold);
if abs(dfxold) > eps
xnew = xold - fxold/dfxold;
fxnew = f(xnew);
else
fprintf('H paragwgos mhdenizetai sto %f kai h me8odos Newton den mporei na synexisei.\n',xold);
xstar = xnew;
fxstar = fxnew;
return;
end
iter = iter + 1 ;
end
xstar = xnew;
fxstar = f(xstar);
end