-
Notifications
You must be signed in to change notification settings - Fork 0
/
Newton.m
61 lines (39 loc) · 1.32 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
54
55
56
57
58
59
60
61
function root=Newton(F,x1,i,t,p)
%%newton
syms x
x = x1;
sf=sym(F); %anon to symbolic
diffi=diff(sf);%derivative of f
%%%newton
%root calc
v=0; %loop conter
ee=t;
ie=100;
while v <= i && ie >= ee %loop vs iterations
fp=subs(diffi); %evaluation of derivative of f
evalx=subs(sf); %eval of f
xn=x-(evalx/fp); %NR x+1 formula
groots(v+1)=x; %array
%act errero calc
disp('err act')
eerxn = double(xn);
eerx = double(x);
disp((((abs((eerx-eerxn)))/eerx)*100)) %muestra valor actual de error
ie=((abs((eerx-eerxn)))/eerx)*100; %calcula error de elemento actual vs pasado
x=xn;
v=v+1; %loop
groots(v+1)=x;
end %while loop
%final val vs iteration error array
e=1; %counter de loop error
while e<v %loop de matriz de errores
errs(e) = ((abs((groots(v)-groots(e+1))))/groots(v))*100; %calcula el errores de cada elemento del array vs el final
e=e+1;
end
%p flag graphs
if (p==true) %graficos de error y valor si p=1
grapher(F,errs,groots,v)
else
verboser(v,groots)
end
end