Professional Documents
Culture Documents
Assign 2 Soln
Assign 2 Soln
Number of equations = 5
Coefficients of eq. 1 = [1 0 0 0 0]
Constant of eq. 1 = 1
Coefficients of eq. 2 = [0 5 0 0 0]
Constant of eq. 2 = 4
Coefficients of eq. 3 = [0 0 10 0 0 ]
Constant of eq. 3 = 5
Coefficients of eq. 4 = [0 0 0 7 0]
Constant of eq. 4 = 8
Coefficients of eq. 5 = [0 0 0 0 2]
Constant of eq. 5 = 12
Initial guess :
Iteration no. 2
Results :
CA( 1) = 1
CA( 2) = 0.8
CA( 3) = 0.5
CA( 4) = 1.143
CA( 5) = 6
n = length(c);
[nr nc] = size(A);
x = x0;
x0 = x + 2 * tol;
iter = 0;
% Substitution procedure
while max(abs(x - x0)) >= tol
x0 = x;
x = c0 - a0 * x0;
if trace
iter = iter + 1;
fprintf('\n Iteration no. %3d\n',iter)
fprintf('%8.6g ',x)
end
end
FORWARD
DIFFERENC
E TABLE:
x y Dyo D2yo D3yo D4yo D5yo D6y0 D7yo D8yo D9y0 D10yo
0 0
106
20 106 1388
1494 -1482
40 1600 -94 2986
1400 1504 -5920
60 3000 1410 -2934 8344
2810 -1430 2424 -5668
80 5810 -20 -510 2676 -12228
2790 -1940 5100 -17896 66074
-
100 8600 -1960 4590 -15220 53846
194060
830 2650 -10120 35950 0 -127986
120 9430 690 0 -5530 20730 -74140
1520 -2880 10610 -38190
140 10950 -2190 5080 -17460
-670 2200 -6850
160 10280 10 -1770
-660 430
180 9620 440
-220
200 9400 0
f(1) f(2)= f(3)= f(4)= f(5) f(6) f(7) f(8) f(9) f(10)
- - - -
x= 50 265 2602.5 -69.375 16.7926 55.45151 105.8601
463.125 116.641 40.7422 13.8379
f(x0)= 0
h= 20
q= 2.5
BACKWARD
DIFFERENCE
TABLE
Dyo D2yo D3yo D4yo D5yo D6y0 D7yo D8yo D9y0
106
1494 1388
1400 -94 -1482
2810 1410 1504 2986
2790 -20 -1430 -2934 -5920
830 -1960 -1940 -510 2424 8344
1520 690 2650 4590 5100 2676 -5668
-670 -2190 -2880 -5530 -10120 -15220 -17896 -12228
-660 10 2200 5080 10610 20730 35950 53846 66074
-220 440 430 -1770 -6850 -17460 -38190 -74140 -12798
A=[1 1
02
-1 2];
>> rank(A)
ans =
2
>> rank(A'*A)
ans =
>> rank(A*A')
ans =
>> inv(A'*A)
ans =
0.5294 0.0588
0.0588 0.1176
A=[2 1 0 0 0;
1 6 2 0 0;
0 2 6 1 0;
0 0 2 4 1;
0 0 0 2 3];
c = [1 4 5 8 12]';
x = Gauss (A , c)
function x = Gauss (A , c)
%GAUSS Solves a set of linear algebraic equations by the Gauss
% elimination method.
%
% GAUSS(A,C) finds unknowns of a set of linear algebraic
% equations. A is the matrix of coefficients and C is the
% vector of constants.
%
%
n = length(c);
[nr nc] = size(A);
% Gauss elimination
for k = 1 : n - 1
pivot = abs(aug(k , k));
prow = k;
pcol = k;
x = zeros(n , 1);
% Back substitution
t(n) = aug(n , n + 1) / aug(n , n);
x(order(n)) = t(n);
for k = n - 1 : -1 : 1
t(k) = (aug(k,n+1) - sum(aug(k,k+1:n) .* t(k+1:n))) /
aug(k,k);
x (order(k)) = t(k);
end
OUTPUT:
x =
0.2802
0.4397
0.5409
0.8755
3.4163
% BY GAUSS JORDAN METHOD:
A=[1 1 0 0 0;
1 2 5 0 0;
0 2 3 1 0;
0 0 2 1 1;
0 0 0 5 4];
c = [1 4 5 8 12]';
x = Jordan (A , c)
function x = Jordan (A , c)
%JORDAN Solves a set of linear algebraic equations by the
% Gauss-Jordan method.
%
% JORDAN(A,C) finds unknowns of a set of linear algebraic
% equations. A is the matrix of coefficients and C is the
% vector of constants.
%
%
c = (c(:).')'; % Make sure it's a column vector
n = length(c);
[nr nc] = size(A);
x = zeros(n , 1);
% Solution
for k = 1 : n
x(order(k)) = aug(k , n + 1);
end
OUTPUT:
x =
93.0000
-92.0000
19.0000
132.0000
-162.0000
A=[12 8 -4 3 1;
1 4 5 8 10;
0 2 6 4 2;
6 7 2 3 1;
1 1 2 4 6];
c = [1 4 5 8 12]';
x = Jordan (A , c)
OUTPUT:
x =
7.5136
-3.0472
8.1724
-13.1261
7.2822
function gaussseidel(A, b, N)
n = size(A,1);
%splitting matrix A into the three matrices L, U and D
D = diag(diag(A));
L = tril(-A,-1);
U = triu(-A,1);
tol = 1e-05;
k = 1;
x = zeros(n,1); %starting vector
while k <= N
x(:,k+1) = Tg*x(:,k) + cg;
if norm(x(:,k+1)-x(:,k)) < tol
disp('The procedure was successful')
disp('Condition ||x^(k+1) - x^(k)|| < tol was met after
k iterations')
disp(k); disp('x = ');disp(x(:,k+1));
break
end
k = k+1;
end
A=[1 0 0 0 0 ;
0 5 0 0 0;
0 0 10 0 0;
0 0 0 7 0;
0 0 0 0 2];
b=[1
4
5
8
12];
n=10;
gaussseidel(A,b,n)
OUTPUT:
x =
1.0000
0.8000
0.5000
1.1429
6.0000