Anda di halaman 1dari 45

HAMZAH AFIF

3714100011

SCRIPT MATLAB MINGGU KE 1

1. BASIC MATLAB

A. Dasar Teori

B. Script Matlab
clc
clear all;

x=input('masukan nilai x:');


z=input('masukan nilai z:');

y=x.^2+7.*x+5.*z+7;
%save nama x y z
%load nama

obj_file=fopen('nama.txt','w');
fprintf(obj_file,'nilai x: %3.2f; nilai z: %f; nilai y:
%f\r\n',x,y,z);
fprintf(obj_file,'nilai x: %3.2f; nilai z: %f; nilai y:
%f\n',x,y,z);
fclose(obj_file);
y

C. Hasil running
HAMZAH AFIF
3714100011

2. MEMBUAT GRAFIK

A. Dasar Teori

B. Script Matlab
clear all
clear cfg

y1=[1 2 3 4 5 6 7 8 9 10];
y2=[5:1:14];

subplot(221)
plot(y1,y2);
grid on;
title('y1 & y2');
xlabel('km');
ylabel('seconds');

%hold on

z=[y1;y2]';
subplot(224)
plot(z);
title('z');

C. Hasil running
HAMZAH AFIF
3714100011

SCRIPT MATLAB MINGGU KE 2


HAMZAH AFIF
3714100011

1. FUNCTION

A. Dasar Teori

B. Script Matlab

function y=kuadrat(x)
a=8.*x.^2;
y=1./(1+a);
%x sebagai variabel input
%y sebagai variabel output
%kuadrat sebagai new function

function y=kuadrat_vektor(x)
a=8.*x.^2;
y(1)=1./(1+a);
y(2)=4.*x.^2+25;

clear all
clc

x=5

y=kuadrat(x)
y=kuadrat_vektor(x)

C. Hasil Running
HAMZAH AFIF
3714100011

2. FLOW CONTROL

A. Dasar Teori

B. Script Matlab
clc
clear all

nilai=71;

%if nilai>=81
% disp('A');
%else
% disp('C');
%end

% if nilai>=81
% disp('A');
% end
% if ((nilai>=71) && (nilai <81))
% disp('B');
% end
% if((nilai==60) && (nilai<71))
% disp('BC');
% end
% if(nilai<60)
% disp('C');
% end

%A=8 artinya 8 menjadi A


%A==8 artinya nilai A sebanding dengan 8

if nilai>=81
disp('A');
elseif nilai >=65
disp('BC');
elseif nilai >=71
disp('B');
else disp ('C');
end
HAMZAH AFIF
3714100011

C. Hasil Running

- Karena nilai yang dimasukkan adalah 71, maka output bernilai BC

3. FOR LOOP

A. Dasar Teori

B. Script Matlab
%loop
clear all
clc
point=[76 85 80 65 87]
for n=1:length(point)
if point(n)>=80
pf(n,:)='Pass';
elseif point(n)>=0
pf(n,:)='Fail';
else
pf(n,:)='????';
fprintf('\n\a Something wrong with the data??\n');
break;
end
end
pf
%length(point) untuk langsung menyesuaikan jumlah data di matriks
%pf(n,:) dibaca variabel pf (baris ke n,kolom berapapun)
%fungsi dari break adalah mengeluarkan proses dari for

C. Hasil Running
HAMZAH AFIF
3714100011

4. WHILE LOOP

A. Script Matlab

%loop
clear all
clc
point=[76 85 -1 65 87]
n=1;
nmax=length(point);
while n<=nmax
if point(n)>=80
pf(n,:)='Pass';
elseif point(n)>=0
pf(n,:)='Fail';
else
pf(n,:)='????';
fprintf('\n Something wrong with the data??\n');
break;
end
n=n+1;
end
pf

B. Hasil Running
HAMZAH AFIF
3714100011

SCRIPT MATLAB MINGGU KE 3

1. MATRIKS

A. Dasar Teori
HAMZAH AFIF
3714100011

B. Script Matlab

clc
clear all

A=[1 1 0 3; 2 1 -1 1; 3 -1 -1 2; -1 2 3 -1];
b=[4;1;-3;4];

x=inv(A)*b
x=(A^-1)*b
x=A\b

%ketiga bentuk x diatas sama, paling akurat yang paling bawah

C. Hasil Running

2. CONTOH SOAL MATRIKS


HAMZAH AFIF
3714100011

A. Script Matlab
clear all
clc

A=[0:0.25:5];
C=[A].^2;
D=[A;C];
E=[D]';

F=[5 5.75 6.40 6.94 7.38 7.72 7.96 8.10 8.13 8.07 7.90 7.62 7.25 6.77
6.20 5.52 4.73 3.85 2.86 1.77 0.58];
G=[F-5]';

x=(D*D')\(D*G)
v=x(1)
g=-2.*x(2)
HAMZAH AFIF
3714100011

B. Hasil Running

3. RLSE

A. Dasar Teori

B. Script Matlab
clear all
clc
HAMZAH AFIF
3714100011

load data_rlse

dim_A=size(A);
dim_b=size(b);
x= zeros(dim_A(2),dim_b(2))
P=90000*eye(dim_A(2),dim_A(2));
pause;

for J=1:dim_A(1)
[x,P]=rlse_update(A(J,:),b(J,:),x,P);
x
pause;
end

C. Hasil Running

SCRIPT MATLAB MINGGU KE 4

1. GAUSS
HAMZAH AFIF
3714100011

A. Dasar Teori

B. Script Matlab
clear all
clc

A=[1 1 0 3; 2 1 -1 1; 3 -1 -1 2; -1 2 3 -1];
b=[4;1;-3;4];
mat_aug=[A b];

%menghilangkan x1
%acuan baris ke-1
ref=mat_aug(1,1); %acuan ada pada kolom 1 baris 1
for i=2:4 %untuk menghilangkan x1, di nol kan baris 2 sampai 4
faktor_kali=mat_aug(i,1)/ref; %x2/x1 dikali 1 baris ke baris
pertama,mat_aug(i,1)=x(i,1) dan ref=x1,1
mat_aug(i,:)=mat_aug(i,:)-(faktor_kali*mat_aug(1,:));
end

%menghilangkan x2
%acuan baris ke-2
ref=mat_aug(2,2); %ganti baris kolom
for i=3:4 %ganti baris
faktor_kali=mat_aug(i,2)/ref; %ganti kolom
mat_aug(i,:)=mat_aug(i,:)-(faktor_kali*mat_aug(2,:)); %ganti baris
di mat_aug
end
HAMZAH AFIF
3714100011

%menghilangkan x3
%acuan baris ke-3
ref=mat_aug(3,3); %ganti baris kolom
for i=4:4 %ganti baris
faktor_kali=mat_aug(i,3)/ref; %ganti kolom
mat_aug(i,:)=mat_aug(i,:)-(faktor_kali*mat_aug(3,:)) %ganti baris
di mat_aug
end

C. Hasil Running

2. GAUSS FORWARD

A. Script Matlab

clear all
clc

A=[1 1 0 3; 2 1 -1 1; 3 -1 -1 2; -1 2 3 -1];
b=[4;1;-3;4];
n=length(A);

mat_aug=[A b];

for j=2:4
ref=mat_aug(j-1,j-1);
for i=j:4
faktor_kali=mat_aug(i,j-1)/ref;
mat_aug(i,:)=mat_aug(i,:)-(faktor_kali*mat_aug(j-1,:))
end
end

B. Hasil Running
HAMZAH AFIF
3714100011

3. GAUSS BACKWARD

A. Script Matlab
clear all
clc

A=[1 1 0 3; 2 1 -1 1; 3 -1 -1 2; -1 2 3 -1];
b=[4;1;-3;4];
n=length(A);

mat_aug=[A b];

%forward
for j=2:4
ref=mat_aug(j-1,j-1);
for i=j:4
faktor_kali=mat_aug(i,j-1)/ref;
mat_aug(i,:)=mat_aug(i,:)-(faktor_kali*mat_aug(j-1,:))
end
end

%backward
x(4)=mat_aug(4,5)/mat_aug(4,4);
x(3)=(mat_aug(3,5)-(mat_aug(3,4)*x(4)))/mat_aug(3,3);
x(2)=(mat_aug(2,5)-
(mat_aug(2,4)*x(4)+mat_aug(2,3)*x(3)))/mat_aug(2,2);
x(1)=(mat_aug(1,5)-
(mat_aug(1,4)*x(4)+mat_aug(1,3)*x(3)+mat_aug(1,2)*x(2)))/mat_aug(1,1)
;

%x=zeroes(n,1)
for i=n:-1:1
pengurangan=0;
for j=n:-1:(i+1)
pengurangan=pengurangan+mat_aug(i,j)*x(j);
end
x(i)=(mat_aug(i,n+1)-pengurangan)/mat_aug(i,i);
end

x=x'
HAMZAH AFIF
3714100011

B. Hasil Running

4. METODE GAUSS JORDAN DAN PIVOTING

A. Dasar Teori

B. Script Matlab
HAMZAH AFIF
3714100011

clear all
clc

A=[0 2 3 4; 2 1 -1 1; 3 -1 -1 2; -1 2 3 -1];
b=[8;1;-3;4];
n=length(A);
mat_aug=[A b];

for j=1:n
while (mat_aug(j,j)==0)%partial pivoting
for k=j:n
buffer=mat_aug(j,:);
mat_aug(j,:)=mat_aug(k,:);
mat_aug(k,:)=buffer;
end
end
mat_aug(j,:)=mat_aug(j,:)/mat_aug(j,j);
for i=1:n
%ref-mat_aug(j,j)
if(i~=j)
faktor_kali=mat_aug(i,j);
mat_aug(i,:)=mat_aug(i,:)-(faktor_kali*mat_aug(j,:));
end
end
end
mat_aug

C. Hasil Running

SCRIPT MATLAB MINGGU KE 5


HAMZAH AFIF
3714100011

3. LU-DECOMPOSITION

D. Dasar Teori

E. Script Matlab

clear all
clc

A=[1 1 0 3; 2 1 -1 1; 3 -1 -1 2; -1 2 3 -1];
b=[4; 1; -3; 4];

%metode LU Decomposition
%Ax=b
%Memecah matriks A menjadi matrik L (lower) dan U (upper)
%LUx=b
%Ux=y
%sehingga Ly=b

U=A; %membuat matriks U berukuran dan berisi sama dengan matriks


A
n=length(U);
L=eye(n,n); %membuat matriks L dari matriks identitas yang berukuran
sama dengan matriks U

%mencari matriks U dan L dengan metode Gauss


for j=2:n;
HAMZAH AFIF
3714100011

ref=U(j-1,j-1);
for i=j:n;
faktor_kali=U(i,j-1)/ref;
U(i,:)=U(i,:)-(faktor_kali*U(j-1,:)); %matriks U diperoleh dengan
metode gauss
L(i,j-1)=faktor_kali; %matriks L diperoleh dengan
mengisikan faktor kali pada lower
end
end
L
U

%mencari nilai y dengan forward (Ly=b)


for i=1:n
pengurang=0;
for j=1:i-1
pengurang=pengurang+L(i,j)*y(j);
end
y(i)=b(i)-pengurang;
end
y=y'

%mencari nilai x dengan backward (Ux=y)


for i=n:-1:1
pengurang=0;
for j=n:-1:(i+1)
pengurang=pengurang+U(i,j)*x(j);
end
x(i)=(y(i,1)-pengurang)/U(i,i);
end
x=x'

F. Hasil Running
L=
1 0 0 0
2 1 0 0
3 4 1 0
-1 -3 0 1
U=
1 1 0 3
0 -1 -1 -5
0 0 3 13
0 0 0 -13

y=
4
-7
HAMZAH AFIF
3714100011

13
-13
x=
-1
2
0
1
>>

4. METODE LU DECOMPOSITION DENGAN MATRIKS PERMUTASI

D. Dasar Teori

E. Script Matlab

clear all
clc
HAMZAH AFIF
3714100011

A=[0 2 3 4; 2 1 -1 1; 3 -1 -1 2; -1 2 3 -1];
b=[8; 1; -3; 4];

%metode LU Decomposition
%Ax=b
%Memecah matriks A menjadi matrik L (lower) dan U (upper)
%LUx=b
%Ux=y
%sehingga Ly=b

U=A; %membuat matriks U berukuran dan berisi sama dengan


matriks A
n=length(U);
P=eye(n,n);

UU=[U P]; %UU adalah matriks gabungan U dan P


L=eye(n,n); %membuat matriks L dari matriks identitas yang
berukuran sama dengan matriks U

%mencari matriks U dan L dengan metode Gauss


for j=2:n;
%pivoting matriks permutasi
k=j;
while (UU(j-1,j-1)==0)
buffer=UU(j-1,:);
UU(j-1,:)=UU(k,:);
UU(k,:)=buffer;
k=k+1;
end

%elimianasi untuk memperoleh UU dan L


ref=UU(j-1,j-1);
for i=j:n;
faktor_kali=UU(i,j-1)/ref;
UU(i,1:n)=UU(i,1:n)-(faktor_kali*UU(j-1,1:n)); %matriks U
diperoleh dengan metode gauss
L(i,j-1)=faktor_kali; %matriks L diperoleh dengan
mengisikan faktor kali pada lower
end
end
L
U=UU(:,1:n)
P=UU(:,n+1:2*n)

%mencari nilai y dengan forward (Ly=b)


%Ly=Pb
Pb=P*b;
for i=1:n
pengurang=0;
for j=1:i-1
pengurang=pengurang+L(i,j)*y(j);
end
y(i)=Pb(i)-pengurang;
end
y=y'

%mencari nilai x dengan backward (Ux=y)


for i=n:-1:1
pengurang=0;
for j=n:-1:(i+1)
HAMZAH AFIF
3714100011

pengurang=pengurang+U(i,j)*x(j);
end
x(i)=(y(i,1)-pengurang)/U(i,i);
end
x=x'

F. Hasil running
L=
1.0000 0 0 0
0 1.0000 0 0
1.5000 -1.2500 1.0000 0
-0.5000 1.2500 -0.2941 1.0000

U=
2.0000 1.0000 -1.0000 1.0000
0 2.0000 3.0000 4.0000
0 0 4.2500 5.5000
0 0 0 -3.8824

P=
0 1 0 0
1 0 0 0
0 0 1 0
0 0 0 1
y=
1.0000
8.0000
5.5000
-3.8824

x=
-1.0000
2.0000
HAMZAH AFIF
3714100011

-0.0000
1.0000

5. INVERS

A. Script Matlab
clear all
clc

A=[0 2 3 4; 2 1 -1 1; 3 -1 -1 2; -1 2 3 -1];

n=length(A);

A_inv=zeros(n,n); %membuat matriks berisi 0


bb=eye(n,n); %membuat matriks identitas
for i=1:n
A_inv(:,i)=function_LU(A,bb(:,i));`%memanfaatkan fungsi LU
decomposititon permutasi yg sudah dibuat
end
A_inv

B. Hasil running

SCRIPT MATLAB MINGGU KE 6

6. ITERASI JACOBI

G. Dasar Teori
HAMZAH AFIF
3714100011

H. Script Matlab
clear all
clc

A=[10 -1 2 0; -1 11 -1 3; 2 -1 10 -1; 0 3 -1 8];


b=[6;25;-11;15];

p=length(A);

J=zeros(p,p);
u=zeros(p,1);
for m=1:p
u(m,1)=b(m,1)/A(m,m);
HAMZAH AFIF
3714100011

for n=1:p
if(m~=n) %ketika m=n, maka nilainya langsung 0
J(m,n)=-A(m,n)/A(m,m); %nilai per baris tiap kolom dibagi
dengan diagonalnya
end
end
end
J

x=zeros(p,1); %inisialisasi matriks x


current_error=1;
current_iter=0;
error_max=0.00001;
iter_max=1000; %jumlah iterasi maksimum

while (current_iter<=iter_max && current_error>=error_max)


x_lama=x;
x=J*x+u; %persamaan Jacobi
selisih=x-x_lama;
current_error=sqrt(sum(selisih.^2)); %menentukan error
current_iter=current_iter+1;
end
current_error
current_iter
x

I. Hasil running

7. ITERASI GAUSS SEIDEL

G. Dasar Teori
HAMZAH AFIF
3714100011

H. Script Matlab
clear all
clc

A=[10 -1 2 0; -1 11 -1 3; 2 -1 10 -1; 0 3 -1 8];


b=[6;25;-11;15];

p=length(A);

J=zeros(p,p);
u=zeros(p,1);

for m=1:p
u(m,1)=b(m,1)/A(m,m);
for n=1:p
if (m~=n)
J(m,n)=-A(m,n)/A(m,m);
end
end
end

L=zeros(p,p);
HAMZAH AFIF
3714100011

U=zeros(p,p);
for m=1:p
L(m,1:m)=J(m,1:m); %membuat matriks lower dengan nilai J
U(m,m:p)=J(m,m:p); %membuat matriks upper dengan nilai J
end
L
U

x=zeros(p,1);
current_error=1;
current_iter=0;
error_max=0.00001;
iter_max=1000;
while (current_iter<=iter_max && current_error>=error_max)
x_lama=x;
x=L*x+U*x_lama+u; %persamaan gauss seidel
selisih=x-x_lama;
current_error=sqrt(sum(selisih.^2));
current_iter=current_iter+1;
end
current_error
current_iter
x

I. Hasil running
HAMZAH AFIF
3714100011

SCRIPT MATLAB MINGGU KE 9

1. REGRESI LINIER

A. Dasar Teori

B. Script Matlab
x=10:10:80;
y=[25 70 380 550 610 1220 830 1450];
x_kuadrat=x.^2;
HAMZAH AFIF
3714100011

n=length(x);
rata_x=mean(x);
rata_y=mean(y);
sum_x=sum(x);
sum_y=sum(y);
sum_x_kuadrat=sum(x_kuadrat);
sum_xy=sum(x.*y);

a1=(n*sum_xy-sum_x*sum_y)/(n*sum_x_kuadrat-sum_x.^2)
a0=rata_y-a1*rata_x

x2=0:80;
y2=a1*x2+a0;

plot(x,y,'*')
hold on
plot(x2,y2)

C. Hasil Running

2. REGRESI POLYNOMIAL

A. Dasar Teori
HAMZAH AFIF
3714100011

B. Script Matlab
clear all
clc

x=0:5;
y=[2.1 7.7 13.6 27.2 40.9 61.1];

orde=2
% b(1,1)=sum(y);
% b(2,1)=sum(x.*y);
% b(3,1)=sum(x.^2.*y);

% A(1,1)=sum(x.^0);
% A(1,2)=sum(x);
% A(1,3)=sum(x.^2);
%
% A(2,1)=sum(x);
% A(2,2)=sum(x.^2);
% A(2,3)=sum(x.^3);
%
% A(3,1)=sum(x.^2);
% A(3,2)=sum(x.^3);
% A(3,3)=sum(x.^4);
b=zeros(orde+1,1);
A=zeros(orde+1,orde+1);

for i=1:orde+1 %baris


pangkat=i-1;
b(i,1)=sum(x.^pangkat.*y);
for j=1:orde+1 %kolom
A(i,j)=sum(x.^pangkat);
pangkat=pangkat+1;
end
end

%A*z=b
z=A\b %artinya sama dengan z=inv(A)*b
x2=0:0.1:5; %y2=z(1,1)+z(2,1)*z(3,1)*x2.^2;

y2=0;
for i=1:orde+1
y2=y2+z(i,1)*x2.^(i-1);
end

plot(x,y,'*')
hold on
plot(x2,y2)
HAMZAH AFIF
3714100011

C. Hasil Running

SCRIPT MATLAB MINGGU KE 10

1. INTERPOLASI LAGRANGE

A. Dasar Teori

B. Script Matlab
1. Lagrange Sequential
clear all
clc

x=[0 20 40];
y=[3.85 0.8 0.212];
xx=15;

n=length(x);
if xx<x(1)||xx>x(n)
error('out of range!')
end

%sequential search
i=1;
while(1)
i=i+1;
if xx<=x(i),break,end
end
HAMZAH AFIF
3714100011

yi=((xx-x(i))/(x(i-1)-x(i))*y(i-1))+((xx-x(i-1))/(x(i)-x(i-
1))*y(i))

2. Lagrange Binary
clear all
clc

x=[0 20 40];
y=[3.85 0.8 0.212];
xx=15;

n=length(x);
if xx<x(1)||xx>x(n)
error('out of range!')
end

%binary search
iL=1;
iU=n;
while(1)
if iU-iL<=1, break,end;
iM=fix((iL+iU)/2);
if x(iM)<xx;
iL=iM;
else
iU=iM;
end
end
%linear interpolation
yi=((xx-x(iU))/(x(iL)-x(iU))*y(iL))+((xx-x(iL))/(x(iU)-
x(iL))*y(iU))

C. Hasil Running

1. 2.

2. INTERPOLASI SPLINE

A. Dasar Teori
HAMZAH AFIF
3714100011

B. Script Matlab

clear all
clc

%inisialisasi
max_iter=1000;
x=0;
max_error=0.00001;

error=1;
iter=1;

while (error>max_error||iter<max_error)
x=exp(-x);
error=abs(exp(-x)-x);
iter=iter+1;
end

x
iter

C. Hasil Running
HAMZAH AFIF
3714100011

SCRIPT MATLAB MINGGU KE 11

1. GRAPHICAL METHOD

A. Dasar Teori

B. Script Matlab
clear all
clc

cd=0.25;
g=9.81;
v=36;
t=4;

mp=linspace(100,200);

fp=sqrt(g*mp/cd).*tanh(sqrt(g*cd./mp)*t)-v;
plot(mp,fp),grid

C. Hasil Running
HAMZAH AFIF
3714100011

2. INCREMENTAL SEARCH

A. Dasar Teori

B. Script Matlab
function xb=incsearch( func,xmin,xmax,ns)
if nargin<3, error('at least 3 arguments required'),end
if nargin<4, ns=50; end %if ns blank set to 50

%incremental search
x=linspace(xmin,xmax,ns);
f=func(x);
nb=0; xb=[]; %xbxb is null unless sign detected
for k=1:length(x)-1
if sign(f(k))~=sign(f(k+1))
nb=nb+1;
xb(nb,1)=x(k);
xb(nb,2)=x(k+1);
end
end
if isempty(xb) %display that no brackets were found
disp('no brackets found')
disp('check interval or increase ns')
else
disp('number of brackets:') %display number of brackets
disp(nb)
end

plot(x,f);
HAMZAH AFIF
3714100011

hold on
plot((xb(:,1)+xb(:,2))/2,0,'o');
grid on

C. Hasil Running

3. BISECTION

A. Dasar Teori

B. Script Matlab
clear all
clc

f=@(x) exp(-x)-x;

xmin=-10;
xmax=10;
error_max=0.00001;
iter_max=1000;

xl=xmin;
xu=xmax;
iter=0;
error=1;

while(1)
iter=iter+1;
xr=(xl+xu)/2;

error=abs(f(xr));
if (error<=error_max || iter>=iter_max)
break;
end
HAMZAH AFIF
3714100011

if (f(xl)*f(xr))>0
xl=xr;
elseif (f(xl)*f(xr))<0
xu=xr;
end
end
Root=xr
C. Hasil Running

SCRIPT MATLAB MINGGU KE 12

1. FALSE POSITION

A. Dasar Teori

B. Script Matlab
clear all
clc

f=@(x) exp(-x)-x;

xmin=0;
xmax=10;
error_max=0.00001;
iter_max=1000;

xl=xmin;
xu=xmax;
iter=0;
error=1;

while(1)
iter=iter+1;
xr=xu-(f(xu)*(xl-xu)/(f(xl)-f(xu)));

error=abs(f(xr));
if (error<=error_max || iter>=iter_max)
HAMZAH AFIF
3714100011

break;
end

if (f(xl)*f(xr))>0
xl=xr;
elseif (f(xl)*f(xr))<0
xu=xr;
end
end

Root=xr
iter
error
C. Hasil Running

2. NEWTON RAPHSON
A. Dasar Teori

B. Script Matlab
clear all
clc

f=@(x) x^2-4;
df=@(x) 2*x;
iter_max=1000;
error_max=0.00001;
xr=0;
iter=0;
error=abs(f(xr));
while (iter<=iter_max && error>=error_max)
dfxr=df(xr);
if(dfxr==0)
dfxr=2*error_max;
end
xr=xr-(f(xr)/dfxr);
HAMZAH AFIF
3714100011

iter=iter+1;
error=abs(f(xr));
end
xr
C. Hasil Running

SCRIPT MATLAB MINGGU KE 13

1. SECANT METHOD

A. Dasar Teori

B. Script Matlab
clear all
clc

f=@(x) x^2-4;

iter_max=1000;
error_max=0.00001;

%inisialisasi
xr(1)=0;
h=0.0001;
dfdx=(f(xr)+h)-(f(xr(1)-h))/(2*h);

if(dfdx==0)
dfdx=2*error_max;
end

xr(2)=xr(1)-f(xr(1))/dfdx;
iter=2;
error=abs(f(xr(2)));

while (iter<=iter_max && error>=error_max)

dfdx=(f(xr(iter))-f(xr(iter-1)))/(xr(iter)-xr(iter1));
if(dfdx==0)
HAMZAH AFIF
3714100011

dfdx=2*error_max;
end

xr(iter+1)=xr(iter)-(f(xr(iter))/dfdx);

error=abs(f(xr(iter+1)));
iter=iter+1;
end
xr(iter)

C. Hasil Running

2. APLIKASI NEWTON
A. Script Matlab
clear all
clc

error_max=0.0001;
iter_max=1000;

x=ones(3,1);
error=1;
iter=0;

while(1)
x=x-(jaco(x))\fun(x);

error=max(abs(fun(x)));
iter=iter+1;

if(error<=error_max || iter>=iter_max)
break;
end
end

B. Hasil Running

3. APLIKASI NEWTON DALAM MENCARI SINYAL

A. Script Matlab
HAMZAH AFIF
3714100011

clear all
clc

error_max=0.0001;
iter_max=1000;

x=ones(2,1);
error=1;
iter=0;

while(1)
x=x-(jaco2(x))\fun2(x);

error=max(abs(fun2(x)));
iter=iter+1;

if(error<=error_max || iter>=iter_max)
break;
end
end

B. Hasil Running
HAMZAH AFIF
3714100011

SCRIPT MATLAB MINGGU KE 14

1. INTEGRAL TRAPEZOIDAL

A. Dasar Teori

B. Script Matlab
clc

a=0;
b=0.8;
n=10;
h=(b-a)/n;

f=@(x) 0.2+25*x-200*x^2+675*x^3-900*x^4+400*x^5;

integral=f(a)+f(b);
xx=a;
for i=1:n-1
xx=xx+h;
integral=integral+2*f(xx);;
end

integral=integral*h/2

C. Hasil Running
HAMZAH AFIF
3714100011

SCRIPT MATLAB MINGGU KE 15

1. INTEGRAL ROMBERG

A. Dasar Teori

B. Script Matlab
function [integral error]=int_romberg(fun,a,b,max_iter,error_max)
n=1;
int(1,1)=int_trap_std(fun,a,b,n);

for iter=1:max_iter
n=2*n;
int(iter+1,1)=int_trap_std(fun,a,b,n);

for k=2:iter+1
j=iter-k+2;
int(j,k)=(4^(k-1)*int(j+1,k-1)-int(j,k-1))/(4^(k-1)-1);
end

error=abs((int(1,k)-int(2,k-1))/int(1,k))*100;
if(error<=error_max)
break;
end
end

integral=int(j,k)
HAMZAH AFIF
3714100011

clear all
clc

format long
f=@(x) 0.2+25*x-200*x^2+675*x^3-900*x^4+400*x^5;
[integral error]=int_romberg(f,0,0.8,30,0.000001)

C. Hasil Running

2. INTEGRAL ADAPTIVE QUADRATURE

A. Dasar Teori

B. Script Matlab
function int=int_adaptive(fun,a,b,error_tol)
%fun= persamaan matematis
%a= batas kiri
%b=batas kanan
%error_tol=toleransi error

c=(a+b)/2;
h=b-a;
HAMZAH AFIF
3714100011

fa=fun(a);
fb=fun(b);
fc=fun(c);
int_1=(fa+4*fc+fb)*h/6;

d=(a+c)/2;
e=(b+c)/2;
fd=fun(d);
fe=fun(e);
int_2=(fa+4*fd+2*fc+4*fe+fb)*h/12;

error=int_2-int_1;
if error<=error_tol
int=int_2+(int_2-int_1)/15
else
int_a=int_adaptive(fun,a,c,error_tol);
int_b=int_adaptive(fun,c,b,error_tol);
int=int_a+int_b;
end

clear all
clc

format long
f=@(x) 0.2+25*x-200*x^2+675*x^3-900*x^4+400*x^5;
integral=int_adaptive(f,0,0.8,0.000001)

C. Hasil Running