Vous êtes sur la page 1sur 6

# Experiment 5

Slot: - L5+L6

## GAUSS SEIDEL METHOD

Code: -
clc,clear;
var_count=input('enter the number of variables to be solved');
disp('the coefficients of equations are to be entered in a diagonal dominant
manner');
coeff_mat=zeros(var_count,var_count);
const_mat=zeros(1,var_count);
for i=1:var_count
lit_1=['Enter the coefficients of equation ' num2str(i) ' as a row vector: '];
lit_2=['Enter the constant of equation ' num2str(i) ' : '];
coeff_mat(i,:)=input(lit_1);
const_mat(i)=input(lit_2);
end
var_mat=input('enter the initial guess values for the variables as row vector:');
emax=input('input the tolerence needed:');
e=ones(1,var_count);
count=1;
while (e>emax)
for i=1:var_count
new_var = (const_mat(i) - sum(var_mat.*coeff_mat(i,:)) +
var_mat(i)*coeff_mat(i,i)) / coeff_mat(i,i);
e(i)=abs((var_mat(i)-new_var)/var_mat(i));
var_mat(i)=new_var;
end
disp(num2str([count,var_mat]));
count=count+1;
end
disp(['the final set of solution for given equation system is ' num2str(var_mat)]);

Output: -
enter the number of variables to be solved3
the coefficients of equations are to be entered in a diagonal dominant manner
Enter the coefficients of equation 1 as a row vector: [52 20 25]
Enter the constant of equation 1 : 4800
Enter the coefficients of equation 2 as a row vector: [30 50 20]
Enter the constant of equation 2 : 5810
Enter the coefficients of equation 3 as a row vector: [18 30 55]
Enter the constant of equation 3 : 5690
enter the initial guess values for the variables as row vector:[38 68 50]
input the tolerence needed:0.00001
1 42.1154 70.9308 50.9818
2 40.5161 71.4976 51.196
3 40.1951 71.6045 51.2428
4 40.1316 71.624 51.253
5 40.1192 71.6273 51.2552
6 40.1168 71.6278 51.2557
the final set of solution for given equation system is 40.1168 71.6278
51.2557

## Gauss Jacobi Method

clc
clear all
disp('do not enter zero anywhere in the coefficient matrix')%CHECK FOR THIS
var=input('enter the number of unknowns in the system of equations');
iter_max=input('maximum permissible iterations to reach convergence');
coeff=zeros(var,var);%coefficient matrix initialisation
cost=zeros(1,var);%initialising constant matrix
%now we are taking individual entry
for i=1:var
disp(num2str(i))
coeff(i,:)=input('enter the coefficient of equation as row vector of equation') ;
cost(i)=input('constant of equation as a row vector');
if(length(coeff)~=var || length(cost)~=var)%insufficient data
entry
disp('you entry is invalid Be careful')
return
end
end
dummy=abs(coeff);%converting all the elements to positive value
if(det(coeff)==0)%checking the matrix for singularity disp('no solution of the
system is possible')
return
end
%taking initial guess guess=0;
guess=input('enter as row vector the initial guess values and IT SHOULD BE NON
ZERO');
tol=input('enter the tolerance limit');
err=ones(1,var);
iter=1;
while(err>tol)
%formula wise alignment
new=zeros(1,var);
for i=1:var
imp_var=(cost(i)-sum(guess.*coeff(i,:))+(coeff(i,i)*guess(i)))/coeff(i,i);
%evaluating improved value
err(i)=abs(guess(i)-imp_var)/guess(i);
new(i)=imp_var;
end
disp(num2str([iter guess(1,:) new(1,:) err(1,:)])) %error
iter=iter+1;
guess=new;
if(iter<=iter_max)
continue
else
break
end
end
if(iter-1==iter_max)
disp('maximum iterations reached suggesting non converging system of equations')
else
disp(['your final approximate roots are' num2str(guess(1,:))])
end

Output:-

## do not enter zero anywhere in the coefficient matrix

enter the number of unknowns in the system of equations3
maximum permissible iterations to reach convergence1000
1
enter the coefficient of equation as row vector of equation[52 20 25]
constant of equation as a row vector4800
2
enter the coefficient of equation as row vector of equation[30 50 20]
constant of equation as a row vector5810
3
enter the coefficient of equation as row vector of equation[18 30 55]
constant of equation as a row vector5690
enter as row vector the initial guess values and IT SHOULD BE NON
ZERO[38 68 50]
enter the tolerance limit0.0001
1 38 68 50 42.1154 73.4
53.9273 0.1083 0.0794118 0.0785455
2 42.1154 73.4 53.9273 38.1503 69.3599
49.635 0.0941469 0.0550428 0.0795944
3 38.1503 69.3599 49.635 41.7679 73.4558
53.1363 0.0948224 0.0590535 0.0705422
4 41.7679 73.4558 53.1363 38.5091 69.8848
49.7183 0.0780195 0.0486149 0.0643263
5 38.5091 69.8848 49.7183 41.5259 73.2072
52.7326 0.0783393 0.0475419 0.0606283
6 41.5259 73.2072 52.7326 38.7989 70.1914
49.933 0.0656714 0.0411954 0.0530897
7 38.7989 70.1914 49.933 41.3047 72.9475
52.4705 0.064586 0.0392649 0.0508176
8 41.3047 72.9475 52.4705 39.0248 70.429
50.1471 0.0551986 0.034525 0.0442802
9 39.0248 70.429 50.1471 41.1104 72.7263
52.267 0.053445 0.0326193 0.0422737
10 41.1104 72.7263 52.267 39.2077 70.6269
50.3313 0.0462845 0.0288667 0.0370345
11 39.2077 70.6269 50.3313 40.9457 72.5429
52.0992 0.0443297 0.0271276 0.035124
12 40.9457 72.5429 52.0992 39.3589 70.7929
50.4853 0.0387542 0.0241233 0.0309771
13 39.3589 70.7929 50.4853 40.8079 72.3905
51.9591 0.0368143 0.0225679 0.0291937
14 40.8079 72.3905 51.9591 39.4848 70.9316
50.6135 0.0324217 0.0201535 0.0258982
15 39.4848 70.9316 50.6135 40.6929 72.2637
51.8423 0.0305959 0.01878 0.0242777
16 40.6929 72.2637 51.8423 39.5898 71.0474
50.7203 0.027108 0.0168322 0.0216419
17 39.5898 71.0474 50.7203 40.597 72.158
51.7448 0.0254418 0.0156325 0.0201987
18 40.597 72.158 51.7448 39.6773 71.1439
50.8093 0.0226547 0.0140544 0.0180781
19 39.6773 71.1439 50.8093 40.5171 72.0699
51.6635 0.0211654 0.013016 0.0168111
20 40.5171 72.0699 51.6635 39.7503 71.2243
50.8836 0.0189256 0.0117322 0.0150964
21 39.7503 71.2243 50.8836 40.4505 71.9964
51.5957 0.0176143 0.0108398 0.0139958
22 40.4505 71.9964 51.5957 39.8111 71.2914
50.9454 0.0158053 0.00979172 0.0126032
23 39.8111 71.2914 50.9454 40.3949 71.9351
51.5392 0.0146635 0.00902923 0.0116549
24 40.3949 71.9351 51.5392 39.8619 71.3474
50.997 0.0131958 0.00817082 0.0105195
25 39.8619 71.3474 50.997 40.3486 71.8841
51.4921 0.0122102 0.00752224 0.00970746
26 40.3486 71.8841 51.4921 39.9042 71.394
51.0401 0.0110146 0.00681728 0.00877866
27 39.9042 71.394 51.0401 40.31 71.8415
51.4528 0.0101695 0.00626758 0.00808679
28 40.31 71.8415 51.4528 39.9394 71.4329
51.0759 0.00919229 0.00568729 0.00732482
29 39.9394 71.4329 51.0759 40.2778 71.806
51.4201 0.00847135 0.00522277 0.00673764
30 40.2778 71.806 51.4201 39.9688 71.4653
51.1058 0.00767021 0.00474413 0.00611097
31 39.9688 71.4653 51.1058 40.2509 71.7764
51.3928 0.00705781 0.00435252 0.00561424
32 40.2509 71.7764 51.3928 39.9933 71.4923
51.1308 0.00639932 0.00395706 0.00509774
33 39.9933 71.4923 51.1308 40.2285 71.7517
51.37 0.00588086 0.00362755 0.0046786
34 40.2285 71.7517 51.37 40.0138 71.5149
51.1516 0.00533842 0.00330034 0.00425214
35 40.0138 71.5149 51.1516 40.2099 71.7311
51.351 0.00490067 0.00302352 0.00389921
36 40.2099 71.7311 51.351 40.0308 71.5337
51.1689 0.00445298 0.00275246 0.00354654
37 40.0308 71.5337 51.1689 40.1943 71.714
51.3352 0.00408421 0.00252021 0.00324987
38 40.1943 71.714 51.3352 40.045 71.5493
51.1833 0.00371412 0.00229542 0.00295784
39 40.045 71.5493 51.1833 40.1813 71.6996
51.322 0.00340401 0.00210077 0.00270882
40 40.1813 71.6996 51.322 40.0569 71.5624
51.1954 0.00309766 0.00191419 0.00246674
41 40.0569 71.5624 51.1954 40.1705 71.6877
51.311 0.00283726 0.0017512 0.00225795
42 40.1705 71.6877 51.311 40.0667 71.5733
51.2054 0.00258337 0.00159623 0.00205709
43 40.0667 71.5733 51.2054 40.1615 71.6778
51.3018 0.00236499 0.00145984 0.0018822
44 40.1615 71.6778 51.3018 40.075 71.5824
51.2138 0.00215438 0.00133104 0.00171541
45 40.075 71.5824 51.2138 40.154 71.6695
51.2942 0.00197141 0.00121699 0.00156904
46 40.154 71.6695 51.2942 40.0818 71.5899
51.2208 0.00179655 0.00110989 0.00143044
47 40.0818 71.5899 51.2208 40.1477 71.6626
51.2878 0.00164338 0.00101456 0.00130801
48 40.1477 71.6626 51.2878 40.0876 71.5963
51.2266 0.00149811 0.000925459 0.00119278
49 40.0876 71.5963 51.2266 40.1425 71.6568
51.2825 0.00136998 0.00084582 0.00109043
50 40.1425 71.6568 51.2825 40.0923 71.6015
51.2315 0.00124922 0.000771666 0.000994588
51 40.0923 71.6015 51.2315 40.1381 71.652
51.278 0.00114209 0.000705152 0.000909063
52 40.1381 71.652 51.278 40.0963 71.6059
51.2355 0.00104165 0.000643421 0.000829311
53 40.0963 71.6059 51.2355 40.1345 71.648
51.2743 0.000952123 0.000587886 0.000757873
54 40.1345 71.648 51.2743 40.0996 71.6096
51.2389 0.000868557 0.000536484 0.00069149
55 40.0996 71.6096 51.2389 40.1315 71.6447
51.2713 0.000793769 0.000490126 0.000631837
56 40.1315 71.6447 51.2713 40.1024 71.6126
51.2417 0.000724216 0.000447315 0.000576566
57 40.1024 71.6126 51.2417 40.1289 71.6419
51.2687 0.000661761 0.000408626 0.000526766
58 40.1289 71.6419 51.2687 40.1047 71.6152
51.2441 0.000603854 0.000372965 0.000480737
59 40.1047 71.6152 51.2441 40.1268 71.6396
51.2666 0.000551713 0.000340681 0.000439172
60 40.1268 71.6396 51.2666 40.1066 71.6173
51.246 0.000503491 0.00031097 0.000400832
61 40.1066 71.6173 51.246 40.1251 71.6376
51.2648 0.00045997 0.000284035 0.000366147
62 40.1251 71.6376 51.2648 40.1082 71.619
51.2476 0.000419805 0.000259279 0.000334206
63 40.1082 71.619 51.2476 40.1236 71.636
51.2633 0.000383485 0.000236809 0.000305266
64 40.1236 71.636 51.2633 40.1096 71.6205
51.249 0.000350026 0.000216179 0.000278653
65 40.1096 71.6205 51.249 40.1224 71.6347
51.262 0.000319721 0.000197436 0.000254509
66 40.1224 71.6347 51.262 40.1107 71.6217
51.2501 0.000291844 0.000180243 0.000232333
67 40.1107 71.6217 51.2501 40.1214 71.6335
51.261 0.000266561 0.00016461 0.000212193
68 40.1214 71.6335 51.261 40.1116 71.6228
51.2511 0.000243332 0.000150281 0.000193712
69 40.1116 71.6228 51.2511 40.1205 71.6326
51.2601 0.00022224 0.000137242 0.000176913
70 40.1205 71.6326 51.2601 40.1124 71.6236
51.2519 0.000202883 0.000125298 0.000161511
71 40.1124 71.6236 51.2519 40.1198 71.6318
51.2594 0.00018529 0.000114424 0.000147499
72 40.1198 71.6318 51.2594 40.113 71.6243
51.2525 0.000169157 0.000104469 0.000134662
73 40.113 71.6243 51.2525 40.1192 71.6312
51.2588 0.000154483 9.54006e-005 0.000122976
your final approximate roots are 40.1192 71.6312 51.2588
>>