-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewtonsMethodForSystems.m
More file actions
46 lines (29 loc) · 965 Bytes
/
NewtonsMethodForSystems.m
File metadata and controls
46 lines (29 loc) · 965 Bytes
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
%% Newton's Method for Systems
%% Input Information
F = @(x) [3 * x(1)^2 - x(2)^2;
3 * x(1) * x(2)^2 - x(1)^3 - 1];
J = @(x) [6 * x(1), -2 * x(2);
3 * x(2)^2 - 3 * x(1)^2, 6 * x(1) * x(2)];
x = [1;
1];
tol = 1e-6; % tolerance, 1e-6 = 10^{-6}
max_iter = 10; % max number of iterations
%% Newton's Method for Systems
k = 1; % iteration count
fprintf('i\tx1\t\t\tx2\t\t\tinf error\n'); % for display
fprintf('%d\t%f\t%f\t\n',0,x(1),x(2));
while( k <= max_iter)
% solve the system J(x)y = -F(x)
y = J(x)\-F(x);
x = x+y;
% calculate infty norm
inf_error = max(abs(y));
% display information
fprintf('%d\t%f\t%f\t%f\n',k,x(1),x(2),inf_error); % displays iteration i, p_i
% check stopping condition
if(inf_error < tol)
break;
end
% increase iteration count
k = k + 1;
end