Sei sulla pagina 1di 10

# UJIAN AKHIR SEMESTER

KOMPUTASI NUMERIK

KELOMPOK 1
1829101021 - Komang Gede Hendra Kusuma Putra
1829101023 - Luciana Hendrika Loekito
1829101026 - Hariyant
1829101032 – Anak Agung Gede Agung Bimantara Putra
1829101037 - I Wayan Aries Ageta

## UNIVERSITAS PENDIDIKAN GANESHA

ILMU KOMPUTER

2019
Computer Problem Set 4.2

## 1. Write a computer program in a language of your choice to solve a system of n

linear equations and n unknowns using naive Gaussian elimination.

## a) The number of equations n.

b) The augmented matrix.

## a) The augmented matrix.

b) The vector solution.

Jawaban

1. Input Data

## %MSc student of structure engineering of Azad university of kerman.

clc

clear all

%================================================
=================

## r=input ('Enter Matrix [r]:');

D=A;d=r;

%-----------------------------------------------------------------

## %create upper triangular matrix

s=0;

for j=1:n-1

if A(j,j)==0

k=j;

for k=k+1:n

if A(k,j)==0

continue

end

break

end

B=A(j,:); C=r(j);

A(j,:)=A(k,:); r(j)=r(k);

A(k,:)=B; r(k)=C;

end

for i=1+s:n-1

L=A(i+1,j)/A(j,j);

A(i+1,:)=A(i+1,:)-L*A(j,:);

r(i+1)=r(i+1)-L*r(j);

end

s=s+1;

end

%-----------------------------------------------------------------

%Solution of equations
x(n)=r(n)/A(n,n);

for i=n-1:-1:1

sum=0;

for j=i+1:n

sum=sum+A(i,j)*x(j);

end

x(i)=(1/A(i,i))*(r(i)-sum);

end

%------------------------------

## %Checking with matlab functions

p=inv(D)*d;

%------------------------------

%Output

disp('@----------------------------------------------------------@')

disp('Output [A][x]=[b]')

## disp('solve with matlab functions(for checking):');disp(p)

Output

2. Generate a 15 × 15 matrix A using the MATLAB command rand and then use
the MATLAB function lufact to solve the linear system Ax = b for the following
b’s:

## c) b = [0.1 0.2 0.3 . . . 0.15]’,

Jawaban :

function x = LUPivot(A,b)

clc

clear all

A=randn(15,15);

## b=input ('Enter Matrix b :');

% b=[1;1/2;1/3;1/4;1/5;1/6;1/7;1/8;1/9;1/10;1/11;1/12;1/13;1/14;1/15]

% b=[0.1;0.2;0.3;0.4;0.5;0.6;0.7;0.8;0.9;0.10;0.11;0.12;0.13;0.14;0.15]

% input:
% A= coefficient matrix

## % b = right hand side vector

% output:

% x = solution vector

[m,n]=size(A);

## if m~=n, error ('Matrix A must be square'); end

L= eye(n); P = L; U = A;

for k=1:n

[pivot m]=max(abs(U(k:n,k)));

m = m+k-1;

if m~=k

temp=U(k,:);

U(k,:)=U(m,:);

U(m,:)=temp;

temp=P(k,:);

P(k,:)=P(m,:);

P(m,:)=temp;

if k>= 2

temp=L(k,1:k-1);

L(k,1:k-1)=L(m,1:k-1);

L(m,1:k-1)=temp;

end

end

for j=k+1:n
L(j,k)=U(j,k)/U(k,k);

U(j,:)=U(j,:)-L(j,k)*U(k,:);

end

end

disp('A = ');disp(A)

disp('L = ');disp(L)

disp('U = ');disp(U)

Pb = P*b;

d = L\Pb;

x = U\d;

disp('x = ');disp(x)

Output 3 A :
Output 3 C :