-
Notifications
You must be signed in to change notification settings - Fork 0
/
zad1.m
42 lines (34 loc) · 853 Bytes
/
zad1.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
clear
clc
format long
%dane
syms x1 x2
r1(x1, x2) = x1^2 + 8 * x2^2 + 2 * x1 - 655;
r2(x1, x2) = 16 * x1 * x2 + 6 * x2 - 1456;
eps = 0.1;
x0 = [11.3; 7.8];
a = x0(1);
b = x0(2);
i = 0;
f(x1, x2) = 0.5 * (r1 * r1 + r2 * r2);
g(x1, x2) = gradient(f);
h(x1, x2) = hessian(f);
g1 = g(a, b);
norm = sqrt(g1(1)^2 + g1(2)^2);
norm = double(norm);
disp(['x' num2str(i) ' = [' num2str(a) ',' num2str(b) ']; ||grad f(x' num2str(i) ')|| =' num2str(norm) ])
xp = x0;
while eps < norm
dp = - inv(h(a, b)) * g(a, b);
xn = xp + dp;
xn = double(xn);
a = xn(1);
b = xn(2);
g1 = g(a, b);
norm = sqrt(g1(1)^2 + g1(2)^2);
norm = double(norm);
i = i+1;
%wyswietl wyniki
disp(['x' num2str(i) ' = [' num2str(a) ',' num2str(b) ']; ||grad f(x' num2str(i) ')|| =' num2str(norm) ])
xp = xn;
end