forked from sed12008/ME3255S2017
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from rcc02007/master
update 3/6/17
- Loading branch information
Showing
42 changed files
with
17,115 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
File renamed without changes.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
function [x,ea,iter] = GS_rel(A,b,lambda,es,maxit) | ||
% GaussSeidel: Gauss Seidel method | ||
% x = GaussSeidel(A,b): Gauss Seidel without relaxation | ||
% input: | ||
% A = coefficient matrix | ||
% b = right hand side vector | ||
% es = stop criterion (default = 0.00001%) | ||
% maxit = max iterations (default = 50) | ||
% output: | ||
% x = solution vector | ||
if nargin<3,error('at least 2 input arguments required'),end | ||
if nargin<5|isempty(maxit),maxit=50;end | ||
if nargin<4|isempty(es),es=0.00001;end | ||
[m,n] = size(A); | ||
if m~=n, error('Matrix A must be square'); end | ||
C = A-diag(diag(A)); | ||
x=zeros(n,1); | ||
for i = 1:n | ||
C(i,1:n) = C(i,1:n)/A(i,i); | ||
end | ||
|
||
d = b./diag(A); | ||
|
||
iter = 0; | ||
while (1) | ||
xold = x; | ||
for i = 1:n | ||
x(i) = d(i)-C(i,:)*x; | ||
x(i) = lambda*x(i)+(1-lambda)*xold(i); | ||
if x(i) ~= 0 | ||
ea(i) = abs((x(i) - xold(i))/x(i)) * 100; | ||
end | ||
end | ||
iter = iter+1; | ||
if max(ea)<=es | iter >= maxit, break, end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
function x = GaussSeidel(A,b,es,maxit) | ||
% GaussSeidel: Gauss Seidel method | ||
% x = GaussSeidel(A,b): Gauss Seidel without relaxation | ||
% input: | ||
% A = coefficient matrix | ||
% b = right hand side vector | ||
% es = stop criterion (default = 0.00001%) | ||
% maxit = max iterations (default = 50) | ||
% output: | ||
% x = solution vector | ||
if nargin<2,error('at least 2 input arguments required'),end | ||
if nargin<4|isempty(maxit),maxit=50;end | ||
if nargin<3|isempty(es),es=0.00001;end | ||
[m,n] = size(A); | ||
if m~=n, error('Matrix A must be square'); end | ||
C = A-diag(diag(A)); | ||
x=zeros(n,1); | ||
for i = 1:n | ||
C(i,1:n) = C(i,1:n)/A(i,i); | ||
end | ||
|
||
d = b./diag(A); | ||
|
||
iter = 0; | ||
while (1) | ||
xold = x; | ||
for i = 1:n | ||
x(i) = d(i)-C(i,:)*x; | ||
if x(i) ~= 0 | ||
ea(i) = abs((x(i) - xold(i))/x(i)) * 100; | ||
end | ||
end | ||
iter = iter+1; | ||
if max(ea)<=es | iter >= maxit, break, end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
function x = Jacobi(A,b,es,maxit) | ||
% GaussSeidel: Gauss Seidel method | ||
% x = GaussSeidel(A,b): Gauss Seidel without relaxation | ||
% input: | ||
% A = coefficient matrix | ||
% b = right hand side vector | ||
% es = stop criterion (default = 0.00001%) | ||
% maxit = max iterations (default = 50) | ||
% output: | ||
% x = solution vector | ||
if nargin<2,error('at least 2 input arguments required'),end | ||
if nargin<4|isempty(maxit),maxit=50;end | ||
if nargin<3|isempty(es),es=0.00001;end | ||
[m,n] = size(A); | ||
if m~=n, error('Matrix A must be square'); end | ||
C = A-diag(diag(A)); | ||
x=zeros(n,1); | ||
for i = 1:n | ||
C(i,1:n) = C(i,1:n)/A(i,i); | ||
end | ||
|
||
d = b./diag(A); | ||
|
||
iter = 0; | ||
while (1) | ||
xold = x; | ||
x = d-C*x; | ||
% if any values of x are zero, we add 1 to denominator so error is well-behaved | ||
i_zero=find(x==0); | ||
i=find(x~=0); | ||
if length(i_zero)>0 | ||
ea(i_zero)=abs((x-xold)./(1+x)*100); | ||
ea(i) = abs((x(i) - xold(i))./x(i)) * 100; | ||
else | ||
ea = abs((x - xold)./x) * 100; | ||
end | ||
iter = iter+1; | ||
if max(ea)<=es | iter >= maxit, break, end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
function [x,ea,iter]= Jacobi_rel(A,b,lambda,es,maxit) | ||
% GaussSeidel: Gauss Seidel method | ||
% x = GaussSeidel(A,b): Gauss Seidel without relaxation | ||
% input: | ||
% A = coefficient matrix | ||
% b = right hand side vector | ||
% es = stop criterion (default = 0.00001%) | ||
% maxit = max iterations (default = 50) | ||
% output: | ||
% x = solution vector | ||
if nargin<3,error('at least 2 input arguments required'),end | ||
if nargin<5|isempty(maxit),maxit=50;end | ||
if nargin<4|isempty(es),es=0.00001;end | ||
[m,n] = size(A); | ||
if m~=n, error('Matrix A must be square'); end | ||
C = A-diag(diag(A)); | ||
x=zeros(n,1); | ||
for i = 1:n | ||
C(i,1:n) = C(i,1:n)/A(i,i); | ||
end | ||
|
||
d = b./diag(A); | ||
|
||
iter = 0; | ||
while (1) | ||
xold = x; | ||
x = d-C*x; | ||
% Add relaxation parameter lambda to current iteration | ||
x = lambda*x+(1-lambda)*xold; | ||
% if any values of x are zero, we add 1 to denominator so error is well-behaved | ||
i_zero=find(x==0); | ||
i=find(x~=0); | ||
if length(i_zero)>0 | ||
ea(i_zero)=abs((x-xold)./(1+x)*100); | ||
ea(i) = abs((x(i) - xold(i))./x(i)) * 100; | ||
else | ||
ea = abs((x - xold)./x) * 100; | ||
end | ||
iter = iter+1; | ||
if max(ea)<=es | iter >= maxit, break, end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
function [L, U] = LU_naive(A) | ||
% GaussNaive: naive Gauss elimination | ||
% x = GaussNaive(A,b): Gauss elimination without pivoting. | ||
% input: | ||
% A = coefficient matrix | ||
% y = right hand side vector | ||
% output: | ||
% x = solution vector | ||
[m,n] = size(A); | ||
if m~=n, error('Matrix A must be square'); end | ||
nb = n; | ||
L=diag(ones(n,1)); | ||
U=A; | ||
% forward elimination | ||
for k = 1:n-1 | ||
for i = k+1:n | ||
fik = U(i,k)/U(k,k); | ||
L(i,k)=fik; | ||
U(i,k:nb) = U(i,k:nb)-fik*U(k,k:nb); | ||
end | ||
end | ||
%% back substitution | ||
%x = zeros(n,1); | ||
%x(n) = Aug(n,nb)/Aug(n,n); | ||
%for i = n-1:-1:1 | ||
% x(i) = (Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i); | ||
%end |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function iters = lambda_fcn(A,b,lambda) | ||
% function to minimize the number of iterations for a given Ax=b solution | ||
% using default Jacobi_rel parameters of es=0.00001 and maxit=50 | ||
|
||
[x,ea,iters]= Jacobi_rel(A,b,lambda,1e-8); | ||
end | ||
|
||
|
Oops, something went wrong.