-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab1.m
96 lines (73 loc) · 1.23 KB
/
lab1.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
%METODA POŁOWIENIA
a = 0;
b = 6;
e = 0.00001;
L = b - a;
f = @(x) x.^3+x.^2-20*x;
i = 0;
while abs (L) > e
i = i + 1;
xm = (a + b) / 2;
x1 = a + 0.25 * L;
x2 = b - 0.25 * L;
ym = f(xm);
y1 = f(x1);
y2 = f(x2);
if ym > y1
b = xm;
elseif ym > y2
a = xm;
else
a = x1;
b = x2;
end
L = b - a;
end
disp (['Metoda połowienia minimum(',num2str(i),'): ', num2str( (a + b) /2 )]);
clear
%METODA ZŁOTEGO PODZIAŁU
a = 0;
b = 6;
e = 0.00001;
L = b - a;
f = @(x) x.^3+x.^2-20*x;
i = 0;
while abs (L) > e
i = i + 1;
x1= a + 0.382 * L;
x2= a + 0.618 * L;
y1=f(x1);
y2=f(x2);
if y1 > y2
a = x1;
else
b = x2;
end
L = b - a;
end
disp (['Metoda złotego podziału(',num2str(i),'): ', num2str( (a + b) /2 )]);
clear
%METODA NEWTONA
a = 0;
b = 6;
e = 0.0001;
L = b - a;
f = @(x) x.^3+x.^2-20*x;
i = 0;
h = 0.005;
xn = 0;
x = a;
while true
i = i + 1;
x1 = x + h;
x2 = x - h;
fx1 = f(x1);
fx2 = f(x2);
fx=f(x);
xn = x - ( ((fx1- fx2) / ( 2 * h )) / (( fx1 - 2 * fx + fx2 ) / h^2 ));
if abs(xn - x) < e
break
end
x = xn;
end
disp (['Metoda Newtona(',num2str(i),'): ', num2str(xn)]);