0% found this document useful (0 votes)
91 views

Latihan 1 Metode Bisection

The document discusses several numerical methods in MATLAB including: 1. Bisection method to find roots of functions. 2. False position method to find roots. 3. Newton-Raphson and modified Newton-Raphson methods to find roots of functions and their derivatives.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Latihan 1 Metode Bisection

The document discusses several numerical methods in MATLAB including: 1. Bisection method to find roots of functions. 2. False position method to find roots. 3. Newton-Raphson and modified Newton-Raphson methods to find roots of functions and their derivatives.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Latihan 1

Metode bisection
n=0;
err=0.00000001;
b=2;
a=0;
disp('metode bisection')
disp('-------------------------------------------')
disp(' n a
b
m
fx(m)')
disp('-------------------------------------------')
while abs (a-b)>=err
m=(a+b)/2;
if fx(a)*fx(m)<0
b=m;
else
a=m;
end
if fx(a)*fx(b)==0
exit
end
disp(sprintf('%3g %8.5f %8.5f %8.5f %8.5f',n,a,b,m,fx(m)))
n=n+1;
end
disp(sprintf('Hasil Akar=%7.5f',m))
xg=linspace(-0.5,2.5);
yg=fx(xg);
plot(xg,yg)
grid on
Fungsinya
function y=fx(x)
%y=x.^3-x.^2-1;
y=exp(-3*x).*cos(2*x-1)-sin(3*x);
Grafik fungsi bisection

Metode False Position


clear all
clc
n=0;
err=0.00000001;
b=1.05;
a=0.8;
disp('metode false position')
disp('-------------------------------------------')
disp(' n a
b
w
fx(w)')
disp('-------------------------------------------')
selisih=1;
wl=1;
while selisih >=err
w=(abs(f(b)*a)-abs(f(a)*b))/(abs(f(b))-abs(f(a)));
if fx(a)*fx(b)<=0
b=w;
else
a=w;
end
selisih=abs(wl-w);
wl=w;
disp(sprintf('%3g %8.5f %8.5f %8.5f %8.5f',n,a,b,w,fx(w)))
n=n+1;
end
disp(sprintf('Hasil Akar=%7.5f',w))
xg=linspace(-0.5,2.5);
yg=fx(xg);

plot(xg,yg)
grid on
latihan 2
Metode Gauss Pivot
Fungsinya
function x = gauss_pivot(A,b)
[n,l] = size(A);
for i = 1:n-1
[pivot,k] = max(abs(A(i:n,i)));
if k > 1
temp1 = A(i,:);
temp2 = b(i,:);
A(i,:) = A(i+k-1,:);
b(i,:) = b(i+k-1,:);
A(i+k-1,:) = temp1;
b(i+k-1,:) = temp2;
end
for h = i+1:n
m = A(h,i)/A(i,i);
A(h,:) = A(h,:)-m*A(i,:);
b(h,:) = b(h,:)-m*b(i,:);
end
end
x(n,:) = b(n,:)/A(n,n);
for i = n-1:-1:1
x(1,:) = (b(i,:)-A(i,i+1:n)*x(i+1:n,:))/ A(i,i);
end
Program matlaba
clc
clear all
AA = [2 1 3 ; 4 3 10 ; 2 4 17]
bb = [11 ; 28 ; 31]
x = gauss_pivot (AA,bb)
Metode Jacobi
Fungsinya
function x = jacobi(A,b,tol,max_i)
[n m] = size(A);
for i = 1:n
xlama(i) = b(i)/A(i,i);
end
xlama = xlama';
C = -A;
for i = 1:n
C(i,i) = 0.0;
C(i,:) = C(i,:)/A(i,i);
d(i,1) = xlama(i);

end
i = 1;
while (i<=max_i)
xbaru = C*xlama + d;
if norm(xbaru - xlama) <=tol;
x = xbaru;
disp('jacobi method konvergen')
return;
else
x = xbaru;
xlama = xbaru;
end
disp ([i xbaru']);
i = i + 1;
end
disp('jacobi method tidak konvergen')
program matlabnya
clc
clear all
AA = [2 1 3; 4 3 10; 2 4 17]
bb = [11; 28; 31]
err = 0.00000001;
maks = 1000;
xx = jacobi(AA,bb,err,maks)
latihjan 3
Metode Secant
Fungsinya
function y=f(x)
y = x.^3-3*x.^2-x+9;
Program matlabnya
clc
clear all
x(1) = 0;
x(2) =-1;
err = 0.00001;
i = 2;
disp('
metode secant
')
disp('------------------------------------')
disp(' i
x
f(x)
')
disp('------------------------------------')
disp(sprintf('%2g %14.10f %15.10f',i-1,x(i),f(x(i))))
while abs(x(i)-x(i-1))>err
x(i+1) = x(i)-f(x(i))*(x(i)-x(i-1))/(f(x(i))-f(x(i-1)));
disp(sprintf('%2g %14.10f %15.10f',i,x(i+1),f(x(i+1))))
i = i+1;
end

disp(sprintf('Hasil Akar=%7.5f',x(i)+1))
xg=linspace(-2,1);
yg=f(xg);
plot(xg,yg)
grid on
Grafiknya

Latihan 4
Metode Newton Raphson
Fungsinya
function [y,y1]= fungsi_x(x)
y= x^3-3*x-20;
y1=3*x^2-3;
Program matlaba
clc
clear all
x0=5;
err=0.000001;
fprintf('err =%10.8f, x0=%8.6f\n',err,x0);
%y=x^3-3*x-20;
%y1=3*x^2-3;
[y,y1]= fungsi_x(x0);
fprintf('y(x0)=%11.8f, y1(x0)=%10.8f',y,y1);
i=1;
x=x0-y/y1;
fprintf(' maka x%g=%10.8f\n',i,x);
while abs((x-x0)/x)>err
x0=x;
[y,y1]=fungsi_x(x0);
fprintf('y(x%g)=%11.8f, y1(x%g)=%10.8f',i,y,i,y1);
i=i+1;
x=x0-y/y1;
fprintf(' maka x%g=%11.8f\n',i,x);
end

%fprintf('|(x-x0)/x|=%10.8f<=err=%10.8f\n',abs((x-x0)/x),err);
fprintf('akarnya=%8.6f, banyak iterasi=%g\n',x,i);
Newton raphson yang diperbaharui
Fungsinya
function [y,y1,y2]= fs(x)
y= x^3-3*x-20;
y1=3*x^2-3;
y2=6*x;
Program matlaba
clc
clear all
x0=5;
err=0.000001;
fprintf('err =%5.8f, x0=%8.6f\n',err,x0);
%y=x^3-3*x-20;
%y1=3*x^2-3;
%y2=6*x
[y,y1,y2]= fs(x0);
fprintf('y(x0)=%10.8f, y1(x0)=%9.8f y2(x0)=%10.8',y,y1,y2);
i=1;
x=x0-y*y1/(((y1)^2)-(y*y2));
fprintf(' maka x%g=%10.8f\n',i,x);
while abs((x-x0)/x)>err
x0=x;
[y,y1,y2]=fs(x0);
fprintf('y(x%g)=%11.8f, y1(x%g)=%9.8f y2(x%g)=%10.8f',i,y,i,y1,i,y2);
i=i+1;
x=x0-y*y1/(((y1)^2)-(y*y2));
fprintf(' maka x%g=%11.8f\n',i,x);
end
%fprintf('|(x-x0)/x|=%10.8f<=err=%10.8f\n',abs((x-x0)/x),err);
Perbedaan fprintf dengansprintf:
Jika menggunakan fprintf,maka harus menggunakan tanda \n di akhir.
Jika menggunakan sprintfmaka cukup tambahkan dispdi depannya.

fprintf('akarnya=%8.6f, banyak iterasi=%g\n',x,i);

Latihan 5
Interpolasi
1. Interpolasi Linear
clear all

clc
x =15;
y1=12.5;
y2=14;
x2=20;
x1=10;
for i= 1:10;
y=y1 + ((y2-y1)*(x-x1)/(x2-x1));
disp(sprintf('%3g %4g %7.4f', i, x, y))
x=x+10;
end
2. Interpolasi Kuadrik
clear all
clc
x1=1;
x2=2;
x3=3;
y1=5;
y2=2;
y3=3;
x=5;
for i= 1:10;
y=y1 *(x-x2)*(x-x3)/((x1-x2)*(x1-x3))+y2*(x-x1)*(x-x3)/((x2-x1)*(x2-x3))+y3*(x-x1)*(xx2)/((x3-x1)*(x3-x2));
disp(sprintf('%3g %4g %7.4f ', i, x, y))
x=x+10;
end
3. Interpolasi Polinomial
clear all
clc
x1=3.2; x2= 2.7; x3=1; x4=4.8; x5=5.6;
y1=22; y2=17.8; y3=14.2; y4=38.3; y5=51.7;
X = [x1^0 x1^1 x1^2 x1^3 x1^4 ;
x2^0 x2^1 x2^2 x2^3 x2^4 ;
x3^0 x3^1 x3^2 x3^3 x3^4 ;
x4^0 x4^1 x4^2 x4^3 x4^4 ;
x5^0 x5^1 x5^2 x5^3 x5^4 ];
Y = [22; 17.8; 14.2; 38.3; 51.7];
A = X\Y;
x = 3;
for i = 1:10;
y = A(1)*x^0 + A(2)*x^1 + A(3)*x^2 + A(4)*x^3 + A(5)*x^4;
disp(sprintf(' %4.2f %4.2f %3g %4.2f %4.2f', i, x, y))
x = x + 1;

Catatan :
Padasaat display % untuk tempat desimal ada yang menggunakan g dan ada
jugayang menggunakan f. Menggunakangkarena tidak ada pecahan atau
decimal, jika ada decimal maka pakef.
Untuk penulisan matriks harus menggunakan huruf capital kemudian
menggunakankurung siku

end

Latihan 6 :
Sigma dalam matlab menggunakanfor,, karena batasnya telah diketahui.

Integrasi

1. Metode trapezoidal
L=
)+ 2
h = (b-a)/n
tentukan variable sigma, misal sg
contoh ;
Penyelesaian :
Dari soal di atas diketahui :
a = -2
b=2
f(x)=
h=1
Maka program matlabnya adalah :
Fungsinya:
function y = f(x)
y =x*exp(2*x);
programnya:
h = 1;
a = -2;

b = 2;
sg = 0;
x = a;
n = (b-a)/h;
for i = 1:n-1;
x = x + h;
sg = sg + f(x);
end
L = h*(f(a)+f(b)+2*sg)/2;
disp(sprintf('Luas = %7.4f',L))
2. Metode SIMPSON
Untuk simpson ini perbedaan terletak pada rumus luasnya. Dimana ada pemisahan antara
sigma ganjil dengan sigma genap. Jadi ada 2 sigma yang digunakan.
L=
)+ 2
contoh ;
Penyelesaian :
Dari soal di atas diketahui :
a = -2
b=2
f(x)=
h=1
Maka program matlabnya adalah :
Fungsinya:
function y = f(x)
y =x*exp(2*x);
Programnya matlab
h = 1;
a = -2;
b = 2;
sg1 = 0;
sg2 = 0;
x = a;
n = (b-a)/h;
for i = 1:2:n-1;
x = x + h;
sg1 = sg + f(x);
end
for i = 2:2:n-1;
x = x + h;
sg2 = sg + f(x);
end
L = h*(f(a)+f(b)+2*sg1+4*sg2)/3;
disp(sprintf('Luas = %7.4f',L))

untuk membuat grafiknya tambahkan pada program matlab :


xm=linspace(-2,2);
ym=f(xm);
plot(xm,ym)
grid on
Penulisan % padamatlab.. angka depan harus lebih besar dari angka belakang, misal
%8.4f.

Latihan 7
Differensial
1. Metode Selisih Maju
Fungsinya:
function y=f(x)
y=3*x*exp(-1*x);
Program Matlabnya:
clc
clear all
disp(' diferensial selisih maju
')
disp(' i
h
f1 ')
disp('--------------------------- ')
h=0.1;
x=2;
for i=1:10;
f1=(f(x+h)-f(x))/h;
disp(sprintf('%3g %10.8f %10.8f', i,h,f1))
h=h/10;
end

2. Metode Titik Terpusat


Fungsinya :
function y=f(x)
y=3*x*exp(-1*x);
Program Matlabnya :
clc
clear all
disp('
Metode Titik Terpusat ')
disp(' i
h
f1 ')
disp('--------------------------- ')
h=0.1;
x=2;
for i=1:10;
f1=(f(x+h)-f(x-h))/(2*h);
disp(sprintf('%3g %10.8f %10.8f', i,h,f1))
h=h/10;
end
3. Formula Titik Tiga
Fungsinya :
function y=f(x)
y=3*x*exp(-1*x);
Program Matlabnya :
clc
clear all
disp('
Formula Titik Tiga
')
disp(' i
h
f1 ')
disp('--------------------------- ')
h=0.1;
x=2;
for i=1:10;
f1=((-3*f(x))+(4*f(x+h))-(f(x+(2*h))))/(2*h);
disp(sprintf('%3g %10.8f %10.8f', i,h,f1))
h=h/10;
end
4. Formula Titik Lima
Fungsinya :
function y=f(x)
y=3*x*exp(-1*x);
Program Matlabnya 1:
clc
clear all
disp(' Formula Lima Titik Part 1 ')
disp(' i
h
f1 ')
disp('--------------------------- ')
h=0.1;
x=2;

for i=1:10;
f1=((8*f(x+h))-(8*f(x-h))+(f(x-(2*h)))-(f(x+(2*h))))/(12*h);
disp(sprintf('%3g %10.8f %10.8f', i,h,f1))
h=h/10;
end
Program Matlab 2:
clc
clear all
disp(' Formula Lima Titik part 2 ')
disp(' i
h
f1 ')
disp('--------------------------- ')
h=0.1;
x=2;
for i=1:10;
f1=((-25*f(x))+(48*f(x+h))-(36*f(x+(2*h)))+(16*f(x+(3*h)))-(3*f(x+(4*h))))/(12*h);
disp(sprintf('%3g %10.8f %10.8f', i,h,f1))
h=h/10;
end
Grafik
1. Jika fungsinya f(x)=ln x, yang dalam matlab ditulis f(x)=log(x)

Latihan 8
Differensial Tingkat Dua dan Romberg
1. Differensial Tingkat Dua
Program Matlabnya
disp('Diferensial Tingkat Dua')
disp('------------------------------')
disp(' i
h
f11 ')
disp('===================================')
h = 0.1;
x = 2;
for i = 1:3;
f11 = (f(x-2*h)-(2*f(x))+f(x+2*h))/(4*(h^2));

disp(sprintf('%3g %12.3f %15.5f', i,h,f11))


h = h/10;
end
Fungsinya
function y=f(x)
%y =3*x*exp(-1*x);
%y =log(x);
y =x^2*exp(1*x);
Atau
function f2 = turun2(fstr,x,h,r)
disp('Diferensial Tingkat Dua')
disp('------------------------------')
disp(' i
h
f2 ')
disp('================================== ')
f = inline(fstr);
for i = 1:r;
f2 = (f(x-2*h)-(2*f(x))+f(x+2*h))/(4*(h^2));
disp(sprintf('%3g %12.3f %15.5f', i,h,f2))
h = h/10;
end
2. Romberg
function L = rmb(fstr,a,b,n)
f = inline(fstr);
L = 0;
h =(b-a)/n;
for i = 0:n;
if i==0|i==n
L=L+f(a+i*h)*h/2;
else
L=L+f(a+i*h)*h;
end
end
%fprintf('L=%12.9f\n',L)
Atau
clear all
disp('Integral Romberg')
disp('------------------------------')
disp(' j
LL ')
disp('================================== ')
for j=1:10;
LL = rmb('1/(1+x)',0,1,j);
disp(sprintf('%3g %15.9f',j,LL));
end
Latihan 9
Persamaan Differensial
1. Difeuler

Program Matlabnya
function y=difeuler(fs,a,b,y0,n)
f=inline(fs);
h=(b-a)/n;
y(1)=y0;
x(1)=a;
x(2)=x(1)+h;
for i=1:n;
y(i+1)=y(i)+h*(f(x(i)));
x(i+1)=x(i)+h;
end
[x' y']
plot(x,y)
2. Metode Poligon
Programnya
function y=poligon(fs,a,b,y0,n)
f=inline(fs);
h=(b-a)/n;
y(1)=y0;
x(1)=a;
x(2)=x(1)+h;
for i=1:n;
x(i+1)=x(i)+h;
y_st=y(i)+f(x(i),y(i))*h/2;
x_st=f(x(i)+h/2,y_st);
y(i+1)=y(i)+x_st*h;
end
[x' y']
plot(x,y)
3. Metode Raphson
Program matlabnya
function y=raphson(fs,a,b,y0,n)
f=inline(fs,'x','y');
h=(b-a)/n;
y(1)=y0;
x(1)=a;
x(2)=x(1)+h;
for i=1:n;
x(i+1)=x(i)+h;
k1=f(x(i),y(i));
k2=f(x(i)+h*3/4,y(i)+h*k1*3/4);
y(i+1)=y(i)+h*(k1*1/3+2/3*k2);
end
[x' y']
plot(x,y)
4. Metode Rungekutta 4

Program matlabnya
function y=rungekutta(fs,a,b,y0,n)
f=inline(fs,'x','y');
h=(b-a)/n;
y(1)=y0;
x(1)=a;
x(2)=x(1)+h;
for i=1:n;
x(i+1)=x(i)+h;
k1=h*f(x(i),y(i));
k2=h*f(x(i)+h/2,y(i)+k1/2);
k3=h*f(x(i)+h/2,y(i)+k2/2);
k4=h*f(x(i)+h/2,y(i)+k3);
y(i+1)=y(i)+(k1+2*k2+2*k3+k4)/6;
end
[x' y']
plot(x,y)
5. Metode Rungekutte 3
Program Matlabnya
function y=rungekutta3(fs,a,b,y0,n)
f=inline(fs,'x','y');
h=(b-a)/n;
y(1)=y0;
x(1)=a;
x(2)=x(1)+h;
for i=1:n;
x(i+1)=x(i)+h;
k1=f(x(i),y(i));
k2=f(x(i)+h/2,y(i)+k1*h/2);
k3=f(x(i)+h,y(i)-k1*h+2*h*k2);
y(i+1)=y(i)+(k1+4*k2+k3)/6;
end
[x' y']
plot(x,y)
6. Metode Heun
Program Matlabnya
function y=heun(fs,a,b,y0,n)
f=inline(fs,'x','y');
h=(b-a)/n;
y(1)=y0;
x(1)=a;
x(2)=x(1)+h;
for i=1:n;
x(i+1)=x(i)+h;
g(i)=f(x(i),y(i));
y(i+1)=y(i)+h*f(x(i),y(i));

g(i+1)=f(x(i+1),y(i+1));
y(i+1)=y(i)+h*(g(i)+g(i+1))/2;
end
[x' y']
plot(x,y)

Latihan 10
Lanjutan Dari Latihan 9
1. Dengan Metode Euler (PD tingkat 2)
function pd2=feuler(sf,sg,x0,y0,z0,h)
f=inline(sf);
g=inline(sg);
n=11;
x(1)=x0 ;
y(1)=y0 ;
z(1)=z0 ;
disp(' i-1 x(i) y(i)
z(i)')
for i=1:n;
fprintf('%3g |%8.5f |%8.5f |%8.5f\n',i-1,x(i),y(i),z(i))
y(i+1)=y(i)+h*f(z(i));
z(i+1)=z(i)+h*g(x(i),y(i),z(i));
x(i+1)=x(i)+h;
end
pd2=z(n);
Cara memanggil feuler =feuler('z','1-2*x*y-3*z',0,0,0,0.2)
Hasil command window:
i-1
x(i)
y(i)
z(i)
0 | 0.00000 | 0.00000 | 0.00000
1 | 0.20000 | 0.00000 | 0.20000
2 | 0.40000 | 0.04000 | 0.28000
3 | 0.60000 | 0.09600 | 0.30560
4 | 0.80000 | 0.15712 | 0.29920
5 | 1.00000 | 0.21696 | 0.26940
6 | 1.20000 | 0.27084 | 0.22098
7 | 1.40000 | 0.31504 | 0.15839
8 | 1.60000 | 0.34671 | 0.08693
9 | 1.80000 | 0.36410 | 0.01288
10 | 2.00000 | 0.36668 |-0.05700
ans =
-0.0570

2. Dengan Metode Runge Kutta


function pd2=frungekutta2(sf,sg,x0,y0,z0,h)
f=inline(sf);
g=inline(sg);
n=11;
x(1)=x0;
y(1)=y0;
z(1)=z0;
disp(' i-1 x(i) k1
l1
k2
l2
y(i)
z(i)')
k1=0;
l1=0;
k2=0;
l2=0;
for i=1:n;
fprintf('%3g |%8.5f |%8.5f |%8.5f |%8.5f |%8.5f |%8.5f|%8.5f\n',i-1,k1,l1,k2,l2,x(i),y(i),z(i))
l1=h*g(x(i),y(i),z(i));
k1=h*f(z(i));
k2=h*f(z(i)+l1);
l2=h*g(x(i)+h,y(i)+k1,z(i)+l1);
y(i+1)=y(i)+(k1+k2)/2;
z(i+1)=z(i)+(l1+l2)/2;
x(i+1)=x(i)+h;
end
pd2=z(n);
plot(x,y)

hasilnya:
i-1 x(i) k1
l1
k2
l2
y(i) z(i)
0 | 0.00000 | 0.00000 | 0.00000 | 0.00000 | 0.00000 | 0.00000| 0.00000
1 | 0.00000 | 0.20000 | 0.04000 | 0.08000 | 0.20000 | 0.02000| 0.14000
2 | 0.02800 | 0.11440 | 0.05088 | 0.03968 | 0.40000 | 0.05944| 0.21704
3 | 0.04341 | 0.06027 | 0.05546 | 0.00893 | 0.60000 | 0.10887| 0.25164
4 | 0.05033 | 0.02289 | 0.05491 |-0.01566 | 0.80000 | 0.16149| 0.25525
5 | 0.05105 |-0.00483 | 0.05008 |-0.03527 | 1.00000 | 0.21206| 0.23520
6 | 0.04704 |-0.02595 | 0.04185 |-0.04992 | 1.20000 | 0.25650| 0.19727
7 | 0.03945 |-0.04148 | 0.03116 |-0.05921 | 1.40000 | 0.29181| 0.14692
8 | 0.02938 |-0.05157 | 0.01907 |-0.06278 | 1.60000 | 0.31604| 0.08975
9 | 0.01795 |-0.05611 | 0.00673 |-0.06065 | 1.80000 | 0.32838| 0.03137
10 | 0.00627 |-0.05525 |-0.00478 |-0.05339 | 2.00000 | 0.32912|-0.02295
ans =
-0.0230
Grafiknya:

Latihan 11
Regresi Linear
Diperoleh data penelitian adalah sbb:
BB
50
70
56
64
TB

115

130

130

125

66

73

74

78

83

85

135

134

140

138

145

145

Dari data di atas bagaimana jika diketahui BB= 73,5 berapakah TB nya??
Jawaban :
Misalkan X= BB
Y=TB
S= sigma
Program Matlabnya :
X = [50 70 56 64 66 73 74 78 83 85];
Y = [115 130 130 125 135 134 140 138 145 145];
n = 10;
SXY = 0;
SX = 0;
SY = 0;
SX2= 0;
for i= 1:n
SXY = SXY + X(i)*Y(i);
SX = SX +X(i);
SY = SY +Y(i);
SX2= SX2 + X(i)^2;
end

a = (n*SXY-(SX*SY))/(n*SX2-SX^2);
b = SY/n- a*SX/n;
sprintf('Y = %7.4f x+ %7.4f',a,b)
hasilnya
Y = 0.7513 x+ 81.1807
Catatan : tinggal ganti x dengan yang ditanyakan
Atau :
function TB = regresi (BB);
X = [50 70 56 64 66 73 74 78 83 85];
Y = [115 130 130 125 135 134 140 138 145 145];
n = 10;
SXY = 0;
SX = 0;
SY = 0;
SX2= 0;
for i= 1:n
SXY = SXY + X(i)*Y(i);
SX = SX +X(i);
SY = SY +Y(i);
SX2= SX2 + X(i)^2;
end
a = (n*SXY-(SX*SY))/(n*SX2-SX^2);
b = SY/n- a*SX/n;
sprintf('Y = %7.4f x+ %7.4f',a,b)
TB = a*BB+b;
sprintf('berat badan = %3g\n',BB)
sprintf('tinggi badan = %8.4f\n',TB)
catatan: simpan sesuai nama funsi jangan diganti
cara memanggil :
misalkan berapa tinggi badan dari berat badan 40??
Maka ketik pada comand window!
regresi(40)
hasil:
berat badan = 40
tinggi badan = 111.2347

function Yt = regresi2 (d);


t = linspace (0,pi/4,10);
Y = sin (2*t);
n = 10;
StY = 0;
St = 0;
SY = 0;
St2= 0;
for i= 1:n ;

StY = StY + t(i)*Y(i);


St = St +t(i);
SY = SY +Y(i);
St2= St2 + t(i)^2;
end
a = (n*StY-(St*SY))/(n*St2-St^2);
b = SY/n- a*St/n;
sprintf('Y = %7.4f x+ %7.4f',a,b)
Y = a*d+b;
sprintf('t = %3g\n',d)
sprintf('Y = %8.4f\n',Y)
hasil:
regresi2(30)
Y = 1.3117 x+ 0.1064
t = 30
Y = 39.4588
Regresi Eksponensial
function TB= eksponensial (BB)
X = [50 70 56 64 66 73 74 78 83 85];
Y = [115 130 130 125 135 134 140 138 145 145];
n = 10;
SXY = 0;
SX = 0;
SY = 0;
SX2= 0;
for i= 1:n
SXY = SXY + X(i)*Y(i);
SX = SX +X(i);
SY = SY +Y(i);
SX2= SX2 + X(i)^2;
end
a = (n*SXY-(SX*SY))/(n*SX2-SX^2);
b = log(SY/n)-a*SX/n;
sprintf('Y = %7.4f x+ %7.4f',a,b)
TB = a*BB+b;
sprintf('berat badan = %3g\n',BB)
sprintf('tinggi badan = %8.4f\n',TB)
hasil:
eksponensial(65)
Y = 0.7513 x+ -47.6237
berat badan = 65
tinggi badan = 1.2140

1.2140
Latihan 1
Metode bisection
n=0;
err=0.00000001;
b=2;
a=0;
disp('metode bisection')
disp('-------------------------------------------')
disp(' n a
b
m
fx(m)')
disp('-------------------------------------------')
while abs (a-b)>=err
m=(a+b)/2;
if fx(a)*fx(m)<0
b=m;
else
a=m;
end
if fx(a)*fx(b)==0
exit
end
disp(sprintf('%3g %8.5f %8.5f %8.5f %8.5f',n,a,b,m,fx(m)))
n=n+1;
end
disp(sprintf('Hasil Akar=%7.5f',m))
xg=linspace(-0.5,2.5);
yg=fx(xg);
plot(xg,yg)
grid on
Fungsinya
function y=fx(x)
%y=x.^3-x.^2-1;
y=exp(-3*x).*cos(2*x-1)-sin(3*x);
Grafik fungsi bisection

Metode False Position


clear all
clc
n=0;
err=0.00000001;
b=1.05;
a=0.8;
disp('metode false position')
disp('-------------------------------------------')
disp(' n a
b
w
fx(w)')
disp('-------------------------------------------')
selisih=1;
wl=1;
while selisih >=err
w=(abs(f(b)*a)-abs(f(a)*b))/(abs(f(b))-abs(f(a)));
if fx(a)*fx(b)<=0
b=w;
else
a=w;
end
selisih=abs(wl-w);
wl=w;
disp(sprintf('%3g %8.5f %8.5f %8.5f %8.5f',n,a,b,w,fx(w)))
n=n+1;
end
disp(sprintf('Hasil Akar=%7.5f',w))
xg=linspace(-0.5,2.5);
yg=fx(xg);

plot(xg,yg)
grid on
latihan 2
Metode Gauss Pivot
Fungsinya
function x = gauss_pivot(A,b)
[n,l] = size(A);
for i = 1:n-1
[pivot,k] = max(abs(A(i:n,i)));
if k > 1
temp1 = A(i,:);
temp2 = b(i,:);
A(i,:) = A(i+k-1,:);
b(i,:) = b(i+k-1,:);
A(i+k-1,:) = temp1;
b(i+k-1,:) = temp2;
end
for h = i+1:n
m = A(h,i)/A(i,i);
A(h,:) = A(h,:)-m*A(i,:);
b(h,:) = b(h,:)-m*b(i,:);
end
end
x(n,:) = b(n,:)/A(n,n);
for i = n-1:-1:1
x(1,:) = (b(i,:)-A(i,i+1:n)*x(i+1:n,:))/ A(i,i);
end
Program matlaba
clc
clear all
AA = [2 1 3 ; 4 3 10 ; 2 4 17]
bb = [11 ; 28 ; 31]
x = gauss_pivot (AA,bb)
Metode Jacobi
Fungsinya
function x = jacobi(A,b,tol,max_i)
[n m] = size(A);
for i = 1:n
xlama(i) = b(i)/A(i,i);
end
xlama = xlama';
C = -A;
for i = 1:n
C(i,i) = 0.0;
C(i,:) = C(i,:)/A(i,i);
d(i,1) = xlama(i);

end
i = 1;
while (i<=max_i)
xbaru = C*xlama + d;
if norm(xbaru - xlama) <=tol;
x = xbaru;
disp('jacobi method konvergen')
return;
else
x = xbaru;
xlama = xbaru;
end
disp ([i xbaru']);
i = i + 1;
end
disp('jacobi method tidak konvergen')
program matlabnya
clc
clear all
AA = [2 1 3; 4 3 10; 2 4 17]
bb = [11; 28; 31]
err = 0.00000001;
maks = 1000;
xx = jacobi(AA,bb,err,maks)
latihjan 3
Metode Secant
Fungsinya
function y=f(x)
y = x.^3-3*x.^2-x+9;
Program matlabnya
clc
clear all
x(1) = 0;
x(2) =-1;
err = 0.00001;
i = 2;
disp('
metode secant
')
disp('------------------------------------')
disp(' i
x
f(x)
')
disp('------------------------------------')
disp(sprintf('%2g %14.10f %15.10f',i-1,x(i),f(x(i))))
while abs(x(i)-x(i-1))>err
x(i+1) = x(i)-f(x(i))*(x(i)-x(i-1))/(f(x(i))-f(x(i-1)));
disp(sprintf('%2g %14.10f %15.10f',i,x(i+1),f(x(i+1))))
i = i+1;
end

disp(sprintf('Hasil Akar=%7.5f',x(i)+1))
xg=linspace(-2,1);
yg=f(xg);
plot(xg,yg)
grid on
Grafiknya

Latihan 4
Metode Newton Raphson
Fungsinya
function [y,y1]= fungsi_x(x)
y= x^3-3*x-20;
y1=3*x^2-3;
Program matlaba
clc
clear all
x0=5;
err=0.000001;
fprintf('err =%10.8f, x0=%8.6f\n',err,x0);
%y=x^3-3*x-20;
%y1=3*x^2-3;
[y,y1]= fungsi_x(x0);
fprintf('y(x0)=%11.8f, y1(x0)=%10.8f',y,y1);
i=1;
x=x0-y/y1;
fprintf(' maka x%g=%10.8f\n',i,x);
while abs((x-x0)/x)>err
x0=x;
[y,y1]=fungsi_x(x0);
fprintf('y(x%g)=%11.8f, y1(x%g)=%10.8f',i,y,i,y1);
i=i+1;
x=x0-y/y1;
fprintf(' maka x%g=%11.8f\n',i,x);
end

%fprintf('|(x-x0)/x|=%10.8f<=err=%10.8f\n',abs((x-x0)/x),err);
fprintf('akarnya=%8.6f, banyak iterasi=%g\n',x,i);
Newton raphson yang diperbaharui
Fungsinya
function [y,y1,y2]= fs(x)
y= x^3-3*x-20;
y1=3*x^2-3;
y2=6*x;
Program matlaba
clc
clear all
x0=5;
err=0.000001;
fprintf('err =%5.8f, x0=%8.6f\n',err,x0);
%y=x^3-3*x-20;
%y1=3*x^2-3;
%y2=6*x
[y,y1,y2]= fs(x0);
fprintf('y(x0)=%10.8f, y1(x0)=%9.8f y2(x0)=%10.8',y,y1,y2);
i=1;
x=x0-y*y1/(((y1)^2)-(y*y2));
fprintf(' maka x%g=%10.8f\n',i,x);
while abs((x-x0)/x)>err
x0=x;
[y,y1,y2]=fs(x0);
fprintf('y(x%g)=%11.8f, y1(x%g)=%9.8f y2(x%g)=%10.8f',i,y,i,y1,i,y2);
i=i+1;
x=x0-y*y1/(((y1)^2)-(y*y2));
fprintf(' maka x%g=%11.8f\n',i,x);
end
%fprintf('|(x-x0)/x|=%10.8f<=err=%10.8f\n',abs((x-x0)/x),err);
Perbedaan fprintf dengansprintf:
Jika menggunakan fprintf,maka harus menggunakan tanda \n di akhir.
Jika menggunakan sprintfmaka cukup tambahkan dispdi depannya.

fprintf('akarnya=%8.6f, banyak iterasi=%g\n',x,i);

Latihan 5
Interpolasi
1. Interpolasi Linear
clear all

clc
x =15;
y1=12.5;
y2=14;
x2=20;
x1=10;
for i= 1:10;
y=y1 + ((y2-y1)*(x-x1)/(x2-x1));
disp(sprintf('%3g %4g %7.4f', i, x, y))
x=x+10;
end
2. Interpolasi Kuadrik
clear all
clc
x1=1;
x2=2;
x3=3;
y1=5;
y2=2;
y3=3;
x=5;
for i= 1:10;
y=y1 *(x-x2)*(x-x3)/((x1-x2)*(x1-x3))+y2*(x-x1)*(x-x3)/((x2-x1)*(x2-x3))+y3*(x-x1)*(xx2)/((x3-x1)*(x3-x2));
disp(sprintf('%3g %4g %7.4f ', i, x, y))
x=x+10;
end
3. Interpolasi Polinomial
clear all
clc
x1=3.2; x2= 2.7; x3=1; x4=4.8; x5=5.6;
y1=22; y2=17.8; y3=14.2; y4=38.3; y5=51.7;
X = [x1^0 x1^1 x1^2 x1^3 x1^4 ;
x2^0 x2^1 x2^2 x2^3 x2^4 ;
x3^0 x3^1 x3^2 x3^3 x3^4 ;
x4^0 x4^1 x4^2 x4^3 x4^4 ;
x5^0 x5^1 x5^2 x5^3 x5^4 ];
Y = [22; 17.8; 14.2; 38.3; 51.7];
A = X\Y;
x = 3;
for i = 1:10;
y = A(1)*x^0 + A(2)*x^1 + A(3)*x^2 + A(4)*x^3 + A(5)*x^4;
disp(sprintf(' %4.2f %4.2f %3g %4.2f %4.2f', i, x, y))
x = x + 1;

Catatan :
Padasaat display % untuk tempat desimal ada yang menggunakan g dan ada
jugayang menggunakan f. Menggunakangkarena tidak ada pecahan atau
decimal, jika ada decimal maka pakef.
Untuk penulisan matriks harus menggunakan huruf capital kemudian
menggunakankurung siku

end

Latihan 6 :
Sigma dalam matlab menggunakanfor,, karena batasnya telah diketahui.

Integrasi

1. Metode trapezoidal
L=
)+ 2
h = (b-a)/n
tentukan variable sigma, misal sg
contoh ;
Penyelesaian :
Dari soal di atas diketahui :
a = -2
b=2
f(x)=
h=1
Maka program matlabnya adalah :
Fungsinya:
function y = f(x)
y =x*exp(2*x);
programnya:
h = 1;
a = -2;

b = 2;
sg = 0;
x = a;
n = (b-a)/h;
for i = 1:n-1;
x = x + h;
sg = sg + f(x);
end
L = h*(f(a)+f(b)+2*sg)/2;
disp(sprintf('Luas = %7.4f',L))
2. Metode SIMPSON
Untuk simpson ini perbedaan terletak pada rumus luasnya. Dimana ada pemisahan antara
sigma ganjil dengan sigma genap. Jadi ada 2 sigma yang digunakan.
L=
)+ 2
contoh ;
Penyelesaian :
Dari soal di atas diketahui :
a = -2
b=2
f(x)=
h=1
Maka program matlabnya adalah :
Fungsinya:
function y = f(x)
y =x*exp(2*x);
Programnya matlab
h = 1;
a = -2;
b = 2;
sg1 = 0;
sg2 = 0;
x = a;
n = (b-a)/h;
for i = 1:2:n-1;
x = x + h;
sg1 = sg + f(x);
end
for i = 2:2:n-1;
x = x + h;
sg2 = sg + f(x);
end
L = h*(f(a)+f(b)+2*sg1+4*sg2)/3;
disp(sprintf('Luas = %7.4f',L))

untuk membuat grafiknya tambahkan pada program matlab :


xm=linspace(-2,2);
ym=f(xm);
plot(xm,ym)
grid on
Penulisan % padamatlab.. angka depan harus lebih besar dari angka belakang, misal
%8.4f.

Latihan 7
Differensial
1. Metode Selisih Maju
Fungsinya:
function y=f(x)
y=3*x*exp(-1*x);
Program Matlabnya:
clc
clear all
disp(' diferensial selisih maju
')
disp(' i
h
f1 ')
disp('--------------------------- ')
h=0.1;
x=2;
for i=1:10;
f1=(f(x+h)-f(x))/h;
disp(sprintf('%3g %10.8f %10.8f', i,h,f1))
h=h/10;
end

2. Metode Titik Terpusat


Fungsinya :
function y=f(x)
y=3*x*exp(-1*x);
Program Matlabnya :
clc
clear all
disp('
Metode Titik Terpusat ')
disp(' i
h
f1 ')
disp('--------------------------- ')
h=0.1;
x=2;
for i=1:10;
f1=(f(x+h)-f(x-h))/(2*h);
disp(sprintf('%3g %10.8f %10.8f', i,h,f1))
h=h/10;
end
3. Formula Titik Tiga
Fungsinya :
function y=f(x)
y=3*x*exp(-1*x);
Program Matlabnya :
clc
clear all
disp('
Formula Titik Tiga
')
disp(' i
h
f1 ')
disp('--------------------------- ')
h=0.1;
x=2;
for i=1:10;
f1=((-3*f(x))+(4*f(x+h))-(f(x+(2*h))))/(2*h);
disp(sprintf('%3g %10.8f %10.8f', i,h,f1))
h=h/10;
end
4. Formula Titik Lima
Fungsinya :
function y=f(x)
y=3*x*exp(-1*x);
Program Matlabnya 1:
clc
clear all
disp(' Formula Lima Titik Part 1 ')
disp(' i
h
f1 ')
disp('--------------------------- ')
h=0.1;
x=2;

for i=1:10;
f1=((8*f(x+h))-(8*f(x-h))+(f(x-(2*h)))-(f(x+(2*h))))/(12*h);
disp(sprintf('%3g %10.8f %10.8f', i,h,f1))
h=h/10;
end
Program Matlab 2:
clc
clear all
disp(' Formula Lima Titik part 2 ')
disp(' i
h
f1 ')
disp('--------------------------- ')
h=0.1;
x=2;
for i=1:10;
f1=((-25*f(x))+(48*f(x+h))-(36*f(x+(2*h)))+(16*f(x+(3*h)))-(3*f(x+(4*h))))/(12*h);
disp(sprintf('%3g %10.8f %10.8f', i,h,f1))
h=h/10;
end
Grafik
1. Jika fungsinya f(x)=ln x, yang dalam matlab ditulis f(x)=log(x)

Latihan 8
Differensial Tingkat Dua dan Romberg
1. Differensial Tingkat Dua
Program Matlabnya
disp('Diferensial Tingkat Dua')
disp('------------------------------')
disp(' i
h
f11 ')
disp('===================================')
h = 0.1;
x = 2;
for i = 1:3;
f11 = (f(x-2*h)-(2*f(x))+f(x+2*h))/(4*(h^2));

disp(sprintf('%3g %12.3f %15.5f', i,h,f11))


h = h/10;
end
Fungsinya
function y=f(x)
%y =3*x*exp(-1*x);
%y =log(x);
y =x^2*exp(1*x);
Atau
function f2 = turun2(fstr,x,h,r)
disp('Diferensial Tingkat Dua')
disp('------------------------------')
disp(' i
h
f2 ')
disp('================================== ')
f = inline(fstr);
for i = 1:r;
f2 = (f(x-2*h)-(2*f(x))+f(x+2*h))/(4*(h^2));
disp(sprintf('%3g %12.3f %15.5f', i,h,f2))
h = h/10;
end
2. Romberg
function L = rmb(fstr,a,b,n)
f = inline(fstr);
L = 0;
h =(b-a)/n;
for i = 0:n;
if i==0|i==n
L=L+f(a+i*h)*h/2;
else
L=L+f(a+i*h)*h;
end
end
%fprintf('L=%12.9f\n',L)
Atau
clear all
disp('Integral Romberg')
disp('------------------------------')
disp(' j
LL ')
disp('================================== ')
for j=1:10;
LL = rmb('1/(1+x)',0,1,j);
disp(sprintf('%3g %15.9f',j,LL));
end
Latihan 9
Persamaan Differensial
1. Difeuler

Program Matlabnya
function y=difeuler(fs,a,b,y0,n)
f=inline(fs);
h=(b-a)/n;
y(1)=y0;
x(1)=a;
x(2)=x(1)+h;
for i=1:n;
y(i+1)=y(i)+h*(f(x(i)));
x(i+1)=x(i)+h;
end
[x' y']
plot(x,y)
2. Metode Poligon
Programnya
function y=poligon(fs,a,b,y0,n)
f=inline(fs);
h=(b-a)/n;
y(1)=y0;
x(1)=a;
x(2)=x(1)+h;
for i=1:n;
x(i+1)=x(i)+h;
y_st=y(i)+f(x(i),y(i))*h/2;
x_st=f(x(i)+h/2,y_st);
y(i+1)=y(i)+x_st*h;
end
[x' y']
plot(x,y)
3. Metode Raphson
Program matlabnya
function y=raphson(fs,a,b,y0,n)
f=inline(fs,'x','y');
h=(b-a)/n;
y(1)=y0;
x(1)=a;
x(2)=x(1)+h;
for i=1:n;
x(i+1)=x(i)+h;
k1=f(x(i),y(i));
k2=f(x(i)+h*3/4,y(i)+h*k1*3/4);
y(i+1)=y(i)+h*(k1*1/3+2/3*k2);
end
[x' y']
plot(x,y)
4. Metode Rungekutta 4

Program matlabnya
function y=rungekutta(fs,a,b,y0,n)
f=inline(fs,'x','y');
h=(b-a)/n;
y(1)=y0;
x(1)=a;
x(2)=x(1)+h;
for i=1:n;
x(i+1)=x(i)+h;
k1=h*f(x(i),y(i));
k2=h*f(x(i)+h/2,y(i)+k1/2);
k3=h*f(x(i)+h/2,y(i)+k2/2);
k4=h*f(x(i)+h/2,y(i)+k3);
y(i+1)=y(i)+(k1+2*k2+2*k3+k4)/6;
end
[x' y']
plot(x,y)
5. Metode Rungekutte 3
Program Matlabnya
function y=rungekutta3(fs,a,b,y0,n)
f=inline(fs,'x','y');
h=(b-a)/n;
y(1)=y0;
x(1)=a;
x(2)=x(1)+h;
for i=1:n;
x(i+1)=x(i)+h;
k1=f(x(i),y(i));
k2=f(x(i)+h/2,y(i)+k1*h/2);
k3=f(x(i)+h,y(i)-k1*h+2*h*k2);
y(i+1)=y(i)+(k1+4*k2+k3)/6;
end
[x' y']
plot(x,y)
6. Metode Heun
Program Matlabnya
function y=heun(fs,a,b,y0,n)
f=inline(fs,'x','y');
h=(b-a)/n;
y(1)=y0;
x(1)=a;
x(2)=x(1)+h;
for i=1:n;
x(i+1)=x(i)+h;
g(i)=f(x(i),y(i));
y(i+1)=y(i)+h*f(x(i),y(i));

g(i+1)=f(x(i+1),y(i+1));
y(i+1)=y(i)+h*(g(i)+g(i+1))/2;
end
[x' y']
plot(x,y)

Latihan 10
Lanjutan Dari Latihan 9
1. Dengan Metode Euler (PD tingkat 2)
function pd2=feuler(sf,sg,x0,y0,z0,h)
f=inline(sf);
g=inline(sg);
n=11;
x(1)=x0 ;
y(1)=y0 ;
z(1)=z0 ;
disp(' i-1 x(i) y(i)
z(i)')
for i=1:n;
fprintf('%3g |%8.5f |%8.5f |%8.5f\n',i-1,x(i),y(i),z(i))
y(i+1)=y(i)+h*f(z(i));
z(i+1)=z(i)+h*g(x(i),y(i),z(i));
x(i+1)=x(i)+h;
end
pd2=z(n);
Cara memanggil feuler =feuler('z','1-2*x*y-3*z',0,0,0,0.2)
Hasil command window:
i-1
x(i)
y(i)
z(i)
0 | 0.00000 | 0.00000 | 0.00000
1 | 0.20000 | 0.00000 | 0.20000
2 | 0.40000 | 0.04000 | 0.28000
3 | 0.60000 | 0.09600 | 0.30560
4 | 0.80000 | 0.15712 | 0.29920
5 | 1.00000 | 0.21696 | 0.26940
6 | 1.20000 | 0.27084 | 0.22098
7 | 1.40000 | 0.31504 | 0.15839
8 | 1.60000 | 0.34671 | 0.08693
9 | 1.80000 | 0.36410 | 0.01288
10 | 2.00000 | 0.36668 |-0.05700
ans =
-0.0570

2. Dengan Metode Runge Kutta


function pd2=frungekutta2(sf,sg,x0,y0,z0,h)
f=inline(sf);
g=inline(sg);
n=11;
x(1)=x0;
y(1)=y0;
z(1)=z0;
disp(' i-1 x(i) k1
l1
k2
l2
y(i)
z(i)')
k1=0;
l1=0;
k2=0;
l2=0;
for i=1:n;
fprintf('%3g |%8.5f |%8.5f |%8.5f |%8.5f |%8.5f |%8.5f|%8.5f\n',i-1,k1,l1,k2,l2,x(i),y(i),z(i))
l1=h*g(x(i),y(i),z(i));
k1=h*f(z(i));
k2=h*f(z(i)+l1);
l2=h*g(x(i)+h,y(i)+k1,z(i)+l1);
y(i+1)=y(i)+(k1+k2)/2;
z(i+1)=z(i)+(l1+l2)/2;
x(i+1)=x(i)+h;
end
pd2=z(n);
plot(x,y)

hasilnya:
i-1 x(i) k1
l1
k2
l2
y(i) z(i)
0 | 0.00000 | 0.00000 | 0.00000 | 0.00000 | 0.00000 | 0.00000| 0.00000
1 | 0.00000 | 0.20000 | 0.04000 | 0.08000 | 0.20000 | 0.02000| 0.14000
2 | 0.02800 | 0.11440 | 0.05088 | 0.03968 | 0.40000 | 0.05944| 0.21704
3 | 0.04341 | 0.06027 | 0.05546 | 0.00893 | 0.60000 | 0.10887| 0.25164
4 | 0.05033 | 0.02289 | 0.05491 |-0.01566 | 0.80000 | 0.16149| 0.25525
5 | 0.05105 |-0.00483 | 0.05008 |-0.03527 | 1.00000 | 0.21206| 0.23520
6 | 0.04704 |-0.02595 | 0.04185 |-0.04992 | 1.20000 | 0.25650| 0.19727
7 | 0.03945 |-0.04148 | 0.03116 |-0.05921 | 1.40000 | 0.29181| 0.14692
8 | 0.02938 |-0.05157 | 0.01907 |-0.06278 | 1.60000 | 0.31604| 0.08975
9 | 0.01795 |-0.05611 | 0.00673 |-0.06065 | 1.80000 | 0.32838| 0.03137
10 | 0.00627 |-0.05525 |-0.00478 |-0.05339 | 2.00000 | 0.32912|-0.02295
ans =
-0.0230
Grafiknya:

Latihan 11
Regresi Linear
Diperoleh data penelitian adalah sbb:
BB
50
70
56
64
TB

115

130

130

125

66

73

74

78

83

85

135

134

140

138

145

145

Dari data di atas bagaimana jika diketahui BB= 73,5 berapakah TB nya??
Jawaban :
Misalkan X= BB
Y=TB
S= sigma
Program Matlabnya :
X = [50 70 56 64 66 73 74 78 83 85];
Y = [115 130 130 125 135 134 140 138 145 145];
n = 10;
SXY = 0;
SX = 0;
SY = 0;
SX2= 0;
for i= 1:n
SXY = SXY + X(i)*Y(i);
SX = SX +X(i);
SY = SY +Y(i);
SX2= SX2 + X(i)^2;
end

a = (n*SXY-(SX*SY))/(n*SX2-SX^2);
b = SY/n- a*SX/n;
sprintf('Y = %7.4f x+ %7.4f',a,b)
hasilnya
Y = 0.7513 x+ 81.1807
Catatan : tinggal ganti x dengan yang ditanyakan
Atau :
function TB = regresi (BB);
X = [50 70 56 64 66 73 74 78 83 85];
Y = [115 130 130 125 135 134 140 138 145 145];
n = 10;
SXY = 0;
SX = 0;
SY = 0;
SX2= 0;
for i= 1:n
SXY = SXY + X(i)*Y(i);
SX = SX +X(i);
SY = SY +Y(i);
SX2= SX2 + X(i)^2;
end
a = (n*SXY-(SX*SY))/(n*SX2-SX^2);
b = SY/n- a*SX/n;
sprintf('Y = %7.4f x+ %7.4f',a,b)
TB = a*BB+b;
sprintf('berat badan = %3g\n',BB)
sprintf('tinggi badan = %8.4f\n',TB)
catatan: simpan sesuai nama funsi jangan diganti
cara memanggil :
misalkan berapa tinggi badan dari berat badan 40??
Maka ketik pada comand window!
regresi(40)
hasil:
berat badan = 40
tinggi badan = 111.2347

function Yt = regresi2 (d);


t = linspace (0,pi/4,10);
Y = sin (2*t);
n = 10;
StY = 0;
St = 0;
SY = 0;
St2= 0;
for i= 1:n ;

StY = StY + t(i)*Y(i);


St = St +t(i);
SY = SY +Y(i);
St2= St2 + t(i)^2;
end
a = (n*StY-(St*SY))/(n*St2-St^2);
b = SY/n- a*St/n;
sprintf('Y = %7.4f x+ %7.4f',a,b)
Y = a*d+b;
sprintf('t = %3g\n',d)
sprintf('Y = %8.4f\n',Y)
hasil:
regresi2(30)
Y = 1.3117 x+ 0.1064
t = 30
Y = 39.4588
Regresi Eksponensial
function TB= eksponensial (BB)
X = [50 70 56 64 66 73 74 78 83 85];
Y = [115 130 130 125 135 134 140 138 145 145];
n = 10;
SXY = 0;
SX = 0;
SY = 0;
SX2= 0;
for i= 1:n
SXY = SXY + X(i)*Y(i);
SX = SX +X(i);
SY = SY +Y(i);
SX2= SX2 + X(i)^2;
end
a = (n*SXY-(SX*SY))/(n*SX2-SX^2);
b = log(SY/n)-a*SX/n;
sprintf('Y = %7.4f x+ %7.4f',a,b)
TB = a*BB+b;
sprintf('berat badan = %3g\n',BB)
sprintf('tinggi badan = %8.4f\n',TB)
hasil:
eksponensial(65)
Y = 0.7513 x+ -47.6237
berat badan = 65
tinggi badan = 1.2140

1.2140

You might also like