Skip to content

Instantly share code, notes, and snippets.

@carbolymer
Created March 7, 2012 17:45
Show Gist options
  • Save carbolymer/1994647 to your computer and use it in GitHub Desktop.
Save carbolymer/1994647 to your computer and use it in GitHub Desktop.
Dekompozycja LU
clear;
function [L,U] = LU_dec(A)
n = length(A(1,:));
L = eye(n);
for i=1:n
for j=i:n %wiersze U
U(i,j) = A(i,j);
for k=1:(i-1)
U(i,j) = U(i,j) - L(i,k)*U(k,j);
endfor
endfor
for j=(i+1):n %kolumny L
L(j,i) = A(j,i);
for k=1:(i-1)
L(j,i) = L(j,i) - L(j,k)*U(k,i);
endfor
L(j,i) = L(j,i)/U(i,i);
endfor
endfor
endfunction
function X = solve(A,B)
[L,U] = LU_dec(A);
Z = inv(L)*B;
X = inv(U)*Z;
endfunction
A1 = [1 3 2 -1 ; 4 2 5 -1 ; 3 -3 2 -4; -1 2 -3 -5];
B1 = [9;27;19;14];
A2 = [3 1 -1 3 ; 2 1 -2 0 ; 0 3 2 -1 ; 1 1 1 5];
B2 = [4;-1;4;-2];
A3 = [2 -2 2 1; 2 -4 1 1; -1 3 -4 1; 2 4 3 1];
B3 = [7;10;-14;1];
disp("1:");
solve(A1,B1)
inv(A1)*B1
disp("2:");
solve(A2,B2)
inv(A2)*B2
disp("3:");
solve(A3,B3)
inv(A3)*B3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment