You are on page 1of 5

Driver 22

Some examples to demonstrate Broyden method of solving system of equations.

clear all; clc


disp('testing 2 var function')

testing 2 var function

% 2 variable systemof equations testing


h = @(x) [(x(1)^4)+(x(2)^4)-(3*x(1)^2)+(2*x(1)*x(2))+(2*x(2)^2)-(10*x(1)-1);
(x(1)^2*x(2)^2)-(x(1)*x(2))+(x(2)^2)-x(2)-2];
x0 = [0; 0];

[x, hx, itr, FLAG, history] = Broyden(h, x0, 'hist', 'on')

x = 2×1
0.2608
-0.9001
hx = 2×1
10-4 ×
0.2659
-0.0197
itr = 8
FLAG = 1
history = 8×3 table
itr dx_norm E

1 1 2.0025 286.7636

2 2 1.5874 46.0052

3 3 0.6838 0.2567

4 4 0.2581 2.4055

5 5 0.0998 0.2710

6 6 0.0486 0.0006

7 7 0.0029 0

8 8 0.0003 0

disp('testing 3 var function');

testing 3 var function

% 3 variable system of equations testing


h = @(x)[(16*x(1)^4)+(16*x(2)^4)+(x(3)^4)-16;
(x(1)^2)+(x(2)^2)+(x(3)^2)-3;
(x(1)^3)-x(2)];
x0 = [1; 1; 1];
[x, hx, itr, FLAG, history] = Broyden(h, x0, 'hist', 'on')

x = 3×1
0.8780
0.6768
1.3308
hx = 3×1
10-3 ×
0.4769
-0.0013
0.0421
itr = 6
FLAG = 1
history = 6×3 table
itr dx_norm E

1 1 0.3612 11.4899

2 2 0.0665 1.9034

3 3 0.0713 0.0023

4 4 0.0091 0.0020

5 5 0.0024 0.0001

6 6 0.0005 0

disp('testing 4 var function');

testing 4 var function

% 4 variable system of equations testing


h = @(x) [sin(x(1))+x(2)^2-2*x(3)-exp(x(4));
cos(x(2))+x(3)^2-2*x(1)-exp(x(4));
x(1)*x(2)-2*x(3)-sqrt(x(4));
2*x(1)-x(2)+x(3)^2+log(x(4))];
x0 = [1; 1; 1; 1];

[x, hx, itr, FLAG, history] = Broyden(h, x0, 'hist', 'on')

x = 4×1 complex
0.2498 + 0.5394i
0.4722 - 0.6274i
-0.1484 + 0.4286i
-0.0094 - 1.1435i
hx = 4×1 complex
10-3 ×
-0.3002 + 0.1672i
-0.1330 + 0.1098i
0.1335 + 0.1079i
-0.0930 - 0.0168i
itr = 34
FLAG = 1
history = 34×3 table
itr dx_norm E
1 1 4.1115 47.4289
2 2 6.7903 62.0943
3 3 3.5106 46.1875
4 4 0.9421 46.2994
5 5 1.0112 38.6976
6 6 2.0274 11.4732
7 7 1.6873 5.8524
8 8 4.4155 126.0189
9 9 4.6315 5.5202
10 10 0.6626 5.5952
11 11 0.8029 7.2756
12 12 0.2557 7.8728
13 13 0.4986 6.3070
14 14 1.5058 11.1168
15 15 2.4884 4.2533
16 16 3.0198 6.9918
17 17 2.0026 2.8304
18 18 0.3857 2.5168
19 19 0.5400 1.1929
20 20 0.3314 0.6429
21 21 0.1738 0.6490
22 22 0.4747 0.1344
23 23 0.2199 0.1448
24 24 0.0549 0.1055
25 25 0.9267 1.4565
26 26 0.8801 0.0493
27 27 0.0691 0.0181
28 28 0.1300 0.0057
29 29 0.0334 0.0025
30 30 0.0197 0.0011
31 31 0.0192 0
32 32 0.0063 0
33 33 0.0018 0
34 34 0.0017 0
disp('testing 8 var function');

testing 8 var function

% Define the system of equations using anonymous functions


f1 = @(x) 2*x(1)^2 - 3*x(2)^3 + x(3)^2 + 2*x(4)^3 + 5*x(5) - 7;
f2 = @(x) x(1)*x(2) + x(2)*x(3) - x(4)*x(5) + 2*x(6)^2 - x(7) - 9;
f3 = @(x) x(1) + 2*x(2) + x(3)^2 + x(4)^3 - x(5) - 4*x(8);
f4 = @(x) x(1)*x(5) - x(2)*x(4) + 2*x(6)*x(8) - 5;
f5 = @(x) x(1)^2 + x(2)^2 - x(3)^2 - x(4)^2 - x(5)^2 + x(6)^2 + x(7)^2 + x(8)^2
- 12;
f6 = @(x) x(1)^3 + x(2)^3 - 2*x(3)*x(5) + x(4)^2 - x(6)^2 + x(7) + 10;
f7 = @(x) x(1)^2 - x(2)^2 - x(3)^2 + x(4)^2 + x(5)^2 + 2*x(6) - x(7) - 12;
f8 = @(x) x(1)^2 + x(2)^2 + x(3)^2 + x(4)^2 - x(5)^2 - x(6)^2 + x(7)^2 - 7;

% Define the system of equations as a vector function


h = @(x) [f1(x); f2(x); f3(x); f4(x); f5(x); f6(x); f7(x); f8(x)];

% Define the initial guess for the solution


x0 = [1; 1; 1; 1; 1; 1; 1; 1];

[x, hx, itr, FLAG, history] = Broyden(h, x0, 'hist', 'on')

Warning: Jacobian is close to singular


x = 8×1
105 ×
-0.7398
-0.0136
-0.4373
-1.0288
-0.0568
-0.3901
0.9719
0.7505
hx = 8×1
105 ×
-1.8065
-0.0071
-0.8958
-0.0352
0.0097
-0.0329
0.0204
0.0154
itr = 8
FLAG = -1
history = 7×3 table
itr dx_norm E

1 1 31.3476 1.3007e+08

2 2 48.6217 1.0043e+07
itr dx_norm E

3 3 105.1580 1.3683e+11

4 4 130.4832 1.3091e+09

5 5 158.7278 3.2284e+11

6 6 549.9096 1.0955e+15

7 7 582.8702 2.0346e+10

You might also like