diff options
author | priyanka | 2015-06-24 15:03:17 +0530 |
---|---|---|
committer | priyanka | 2015-06-24 15:03:17 +0530 |
commit | b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b (patch) | |
tree | ab291cffc65280e58ac82470ba63fbcca7805165 /845/CH3 | |
download | Scilab-TBC-Uploads-b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b.tar.gz Scilab-TBC-Uploads-b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b.tar.bz2 Scilab-TBC-Uploads-b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b.zip |
initial commit / add all books
Diffstat (limited to '845/CH3')
-rwxr-xr-x | 845/CH3/EX3.1/Ex3_1.sce | 27 | ||||
-rwxr-xr-x | 845/CH3/EX3.10/Ex3_10.sce | 27 | ||||
-rwxr-xr-x | 845/CH3/EX3.2/Ex3_2.sce | 32 | ||||
-rwxr-xr-x | 845/CH3/EX3.3/Ex3_3.sce | 33 | ||||
-rwxr-xr-x | 845/CH3/EX3.4/Ex3_4.sce | 29 | ||||
-rwxr-xr-x | 845/CH3/EX3.5/Ex3_5.sce | 50 | ||||
-rwxr-xr-x | 845/CH3/EX3.6/Ex3_6.sce | 36 | ||||
-rwxr-xr-x | 845/CH3/EX3.7/Ex3_7.sce | 41 | ||||
-rwxr-xr-x | 845/CH3/EX3.8/Ex3_8.sce | 37 | ||||
-rwxr-xr-x | 845/CH3/EX3.9/Ex3_9.sce | 26 |
10 files changed, 338 insertions, 0 deletions
diff --git a/845/CH3/EX3.1/Ex3_1.sce b/845/CH3/EX3.1/Ex3_1.sce new file mode 100755 index 000000000..cc74f2226 --- /dev/null +++ b/845/CH3/EX3.1/Ex3_1.sce @@ -0,0 +1,27 @@ +//Example 3.1
+clc
+clear
+
+A = [2 3 -1; 4 4 -3; -2 3 -1]; //Coefficient Matrix
+B = [5; 3; 1]; //Constant Matrix
+
+n = length(B);
+Aug = [A,B];
+
+// Forward Elimination
+for j = 1:n-1
+ for i = j+1:n
+ Aug(i,j:n+1) = Aug(i,j:n+1) - Aug(i,j) / Aug(j,j) * Aug(j,j:n+1);
+ end
+end
+
+// Backward Substitution
+x = zeros(n,1);
+x(n) = Aug(n,n+1) / Aug(n,n);
+for i = n-1:-1:1
+ x(i) = (Aug(i,n+1)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i);
+end
+disp(strcat(["x = ",string(x(1))]))
+disp(strcat(["y = ",string(x(2))]))
+disp(strcat(["z = ",string(x(3))]))
+
diff --git a/845/CH3/EX3.10/Ex3_10.sce b/845/CH3/EX3.10/Ex3_10.sce new file mode 100755 index 000000000..9ae7b2b35 --- /dev/null +++ b/845/CH3/EX3.10/Ex3_10.sce @@ -0,0 +1,27 @@ +//Example 3.10
+clc
+clear
+
+A = [1 1 1; 4 3 -1; 3 5 3];
+n = length (A(1,:));
+Aug = [A,eye(n,n)];
+
+N = 1:n;
+for i = 1:n
+ dummy1 = N;
+ dummy1(i) = [];
+ index(i,:) = dummy1;
+end
+
+// Forward Elimination
+for j = 1:n
+ [dummy2,t] = max(abs(Aug(j:n,j)));
+ lrow = t+j-1;
+ Aug([j,lrow],:) = Aug([lrow,j],:);
+ Aug(j,:) = Aug(j,:) / Aug(j,j);
+ for i = index(j,:)
+ Aug(i,:) = Aug(i,:) - Aug(i,j) / Aug(j,j) * Aug(j,:);
+ end
+end
+Inv_A = Aug(:,n+1:2*n);
+disp(Inv_A,"Inverse of A (A-1) = ")
diff --git a/845/CH3/EX3.2/Ex3_2.sce b/845/CH3/EX3.2/Ex3_2.sce new file mode 100755 index 000000000..ae2342090 --- /dev/null +++ b/845/CH3/EX3.2/Ex3_2.sce @@ -0,0 +1,32 @@ +//Example 3.2
+clc
+clear
+
+A = [1 1 1; 3 3 4; 2 1 3]; //Coefficient Matrix
+B = [7; 24; 16]; //Constant Matrix
+
+n = length(B);
+Aug = [A,B];
+
+// Forward Elimination
+for j = 1:n-1
+ // Partial Pivoting
+ [dummy,t] = max(abs(Aug(j:n,j)));
+ lrow = t(1)+j-1;
+ Aug([j,lrow],:) = Aug([lrow,j],:);
+
+ for i = j+1:n
+ Aug(i,j:n+1) = Aug(i,j:n+1) - Aug(i,j) / Aug(j,j) * Aug(j,j:n+1);
+ end
+end
+
+// Backward Substitution
+x = zeros(n,1);
+x(n) = Aug(n,n+1) / Aug(n,n);
+for i = n-1:-1:1
+ x(i) = (Aug(i,n+1)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i);
+end
+disp(strcat(["x = ",string(x(1))]))
+disp(strcat(["y = ",string(x(2))]))
+disp(strcat(["z = ",string(x(3))]))
+
diff --git a/845/CH3/EX3.3/Ex3_3.sce b/845/CH3/EX3.3/Ex3_3.sce new file mode 100755 index 000000000..415e7d0ea --- /dev/null +++ b/845/CH3/EX3.3/Ex3_3.sce @@ -0,0 +1,33 @@ +//Example 3.3
+clc
+clear
+
+A = [0 4 2 8; 4 10 5 4; 4 5 6.5 2; 9 4 4 0]; //Coefficient Matrix
+B = [24; 32; 26; 21]; //Constant Matrix
+
+n = length(B);
+Aug = [A,B];
+
+// Forward Elimination
+for j = 1:n-1
+ // Partial Pivoting
+ [dummy,t] = max(abs(Aug(j:n,j)));
+ lrow = t(1)+j-1;
+ Aug([j,lrow],:) = Aug([lrow,j],:);
+
+ for i = j+1:n
+ Aug(i,j:n+1) = Aug(i,j:n+1) - Aug(i,j) / Aug(j,j) * Aug(j,j:n+1);
+ end
+end
+
+// Backward Substitution
+x = zeros(n,1);
+x(n) = Aug(n,n+1) / Aug(n,n);
+for i = n-1:-1:1
+ x(i) = (Aug(i,n+1)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i);
+end
+
+disp(strcat(["x1 = ",string(x(1))]))
+disp(strcat(["x2 = ",string(x(2))]))
+disp(strcat(["x3 = ",string(x(3))]))
+disp(strcat(["x4 = ",string(x(4))]))
diff --git a/845/CH3/EX3.4/Ex3_4.sce b/845/CH3/EX3.4/Ex3_4.sce new file mode 100755 index 000000000..82acb17b9 --- /dev/null +++ b/845/CH3/EX3.4/Ex3_4.sce @@ -0,0 +1,29 @@ +//Example 3.4
+clc
+clear
+
+A = [1 2 1; 2 3 4; 4 3 2];
+B = [8; 20; 16];
+n = length (B);
+Aug = [A,B];
+
+// Forward Elimination
+for j = 1:n-1
+ for i = j+1:n
+ Aug(i,j:n+1) = Aug(i,j:n+1) - Aug(i,j) / Aug(j,j) * Aug(j,j:n+1);
+ end
+end
+
+// Backward Elimination
+for j = n:-1:2
+ Aug(1:j-1,:) = Aug(1:j-1,:) - Aug(1:j-1,j) / Aug(j,j) * Aug(j,:);
+end
+
+// Diagonal Normalization
+for j=1:n
+ Aug(j,:) = Aug(j,:) / Aug(j,j);
+end
+x = Aug(:,n+1);
+disp(strcat(["x = ",string(x(1))]))
+disp(strcat(["y = ",string(x(2))]))
+disp(strcat(["z = ",string(x(3))]))
diff --git a/845/CH3/EX3.5/Ex3_5.sce b/845/CH3/EX3.5/Ex3_5.sce new file mode 100755 index 000000000..653871380 --- /dev/null +++ b/845/CH3/EX3.5/Ex3_5.sce @@ -0,0 +1,50 @@ +//Example 3.5
+clc
+clear
+
+A = [5 -2 1; 7 1 -5; 3 7 4];
+B = [4; 8; 10];
+
+n = length (B);
+L = zeros(n,n); // L = Lower Triangular Matrix Initiation
+U = eye(n,n); // U = Upper Triangular Matrix Initiation
+
+// LU Decomposition
+for i = 1:n
+ sum1 = zeros(n-i+1,1);
+ for k = 1:i-1
+ sum1 = sum1 + L(i:n,k) * U(k,i);
+ end
+ L(i:n,i) = A(i:n,i) - sum1;
+
+ sum2 = zeros(1,n-i);
+ for k = 1:i-1
+ sum2 = sum2 + L(i,k) * U(k,i+1:n);
+ end
+ U(i,i+1:n) = (A(i,i+1:n) - sum2) / L(i,i);
+end
+
+// Forward Substitution
+D = ones(n,1);
+for i = 1:n
+ sum3 = 0;
+ for k = 1:i-1
+ sum3 = sum3 + L(i,k) * D(k);
+ end
+ D(i) = (B(i) - sum3) / L(i,i);
+end
+
+// Back Substitution
+x = ones(n,1);
+for i = n:-1:1
+ sum4 = 0;
+ for k = i+1:n
+ sum4 = sum4 + U(i,k) * x(k);
+ end
+ x(i) = D(i) - sum4;
+end
+
+disp(strcat(["x1 = ",string(x(1))]))
+disp(strcat(["x2 = ",string(x(2))]))
+disp(strcat(["x3 = ",string(x(3))]))
+
diff --git a/845/CH3/EX3.6/Ex3_6.sce b/845/CH3/EX3.6/Ex3_6.sce new file mode 100755 index 000000000..6c2c3d031 --- /dev/null +++ b/845/CH3/EX3.6/Ex3_6.sce @@ -0,0 +1,36 @@ +//Example 3.6
+clc
+clear
+
+A = [83 11 -4; 7 52 13; 3 8 29];
+B = [95; 104; 71];
+
+n = length (B);
+tol = 1e-4;
+iter = 1;
+maxit = 5;
+
+x = zeros(n,1); //Intial guess
+E = ones(n,1); //Assuming to avoid variable size error
+S = diag(diag(A));
+
+
+while (1)
+ x(:,iter+1) = S\(B + (S-A)*(x(:,iter)));
+ E(:,iter+1) = (x(:,iter+1)-x(:,iter))./x(:,iter+1)*100;
+ if x(:,iter) == 0
+ Error = 1;
+ else
+ Error = sqrt((sum((E(:,iter+1)).^2))/n);
+ end
+
+ if Error <= tol | iter == maxit
+ break
+ end
+ iter = iter+1;
+end
+xact = x(:,iter);
+x = round(x*10^4)/10^4;
+x(:,1) = [];
+mprintf('%s %3s %9s %9s','Iter No.','x','y','z');
+disp([(1:iter)' x']);
diff --git a/845/CH3/EX3.7/Ex3_7.sce b/845/CH3/EX3.7/Ex3_7.sce new file mode 100755 index 000000000..936f49343 --- /dev/null +++ b/845/CH3/EX3.7/Ex3_7.sce @@ -0,0 +1,41 @@ +//Example 3.7
+clc
+clear
+
+A = [1 -1/4 -1/4 0; -1/4 1 0 -1/4; -1/4 0 1 -1/4; 0 -1/4 -1/4 1];
+B = [1/2; 1/2; 1/4; 1/4];
+
+n = length (B);
+tol = 1e-4;
+iter = 1;
+maxit = 5;
+
+x = zeros(n,1); //Intial guess
+E = ones(n,1); //Assuming to avoid variable size error
+S = diag(diag(A));
+T = S-A;
+xold = x;
+
+while (1)
+ for i = 1:n
+ x(i,iter+1) = (B(i) + T(i,:) * xold) / A(i,i);
+ E(i,iter+1) = (x(i,iter+1)-xold(i))/x(i,iter+1)*100;
+ xold(i) = x(i,iter+1);
+ end
+
+ if x(:,iter) == 0
+ E = 1;
+ else
+ E = sqrt((sum((E(:,iter+1)).^2))/n);
+ end
+
+ if E <= tol | iter == maxit
+ break
+ end
+ iter = iter + 1;
+end
+X = x(:,iter);
+x = round(x*10^5)/10^5;
+x(:,1) = [];
+mprintf('%s %3s %11s %10s %10s','Iter No.','x1','x2','x3','x4');
+disp([(1:iter)' x']);
diff --git a/845/CH3/EX3.8/Ex3_8.sce b/845/CH3/EX3.8/Ex3_8.sce new file mode 100755 index 000000000..490ef9d73 --- /dev/null +++ b/845/CH3/EX3.8/Ex3_8.sce @@ -0,0 +1,37 @@ +//Example 3.8
+clc
+clear
+
+A = [6 -3 1; 2 1 -8;1 -7 1];
+b = [11; -15; 10];
+
+n = length (b);
+tol = 1e-4;
+iter = 1;
+maxit = 9;
+
+x = zeros(1,n); //Intial guess
+absA = abs(A);
+[dummy,index] = max(absA(1,:),absA(2,:),absA(3,:));
+if length(unique(index)) == n
+ nu_T = diag(diag(A(index,:))) - A(index,:);
+ if abs(diag(A(index,:))) - (sum(abs(nu_T),2)) > 0
+ A(index,:) = A;
+ b(index,:) = b;
+ end
+end
+
+for iter = 1:maxit
+ R(iter,:) = b' - x(iter,:) * A';
+ [mx,i] = max(abs(R(iter,:)));
+ Rmax(iter) = R(iter,i);
+ dx(iter) = Rmax(iter) ./ A(i,i);
+ x(iter+1,:) = x(iter,:);
+ x(iter+1,i) = x(iter,i) + dx(iter);
+end
+R = round(R*10^4)/10^4;
+Rmax = round(Rmax*10^4)/10^4;
+dx = round(dx*10^4)/10^4;
+x = round(x*10^4)/10^4;
+mprintf('%s %3s %9s %9s %12s %10s %6s %9s %9s','Iter No.','R1','R2','R3','Max Ri','Diff dxi','x1','x2','x3');
+disp([(1:maxit)' R Rmax dx x(1:maxit,:)])
diff --git a/845/CH3/EX3.9/Ex3_9.sce b/845/CH3/EX3.9/Ex3_9.sce new file mode 100755 index 000000000..c2cb113dd --- /dev/null +++ b/845/CH3/EX3.9/Ex3_9.sce @@ -0,0 +1,26 @@ +//Example 3.9
+clc
+clear
+
+A = [1 1 1; 4 3 -1; 3 5 3];
+n = length (A(1,:));
+Aug = [A,eye(n,n)];
+
+// Forward Elimination
+for j = 1:n-1
+ for i = j+1:n
+ Aug(i,j:2*n) = Aug(i,j:2*n) - Aug(i,j) / Aug(j,j) * Aug(j,j:2*n);
+ end
+end
+
+// Backward Elimination
+for j = n:-1:2
+ Aug(1:j-1,:) = Aug(1:j-1,:) - Aug(1:j-1,j) / Aug(j,j) * Aug(j,:);
+end
+
+// Diagonal Normalization
+for j=1:n
+ Aug(j,:) = Aug(j,:) / Aug(j,j);
+end
+Inv_A = Aug(:,n+1:2*n);
+disp(Inv_A,"Inverse of A (A-1) = ")
|