PERSAMAAN DIFERENSIAL NUMERIK

  1. Soal
  • Buatlah program dengan menggunakan metode :
    1. Metode Runge-Kutta 2
    2. Metode Runge-Kutta 4
    3. Metode Implisit
    4. Metode Eksplisit

2. Sources Code

Sricpt Function

function dy = fungsi_baru(y)

a=y(1);

b=y(2);

dy1=a+(2*b);

dy2=(3*a)+(2*b);  %hasil manual

dy=[dy1  dy2];%hasil manual

end

  1. Dengan Metode RK 2

%script Metode RK 2

clear all;

clc;

h=0.1;               %selang

t=0:h:2;             %interval

N=length(t)-1;

a(1)=1;

b(1)=-1;

y(1,:)=[a(1) b(1)];

 

for n=1:N

k1=h*fungsi_baru(y(n,:));

k2=h*fungsi_baru(y(n,:)+k1);

y(n+1,:)=y(n,:)+(k1+k2)/2;

a_eksak=exp(-t);

b_eksak=-exp(-t);

end

figure(1);

subplot(2,1,1)

plot(t,y(:,1),’*m’,t,a_eksak,’-g’);

legend(‘RK2x’,’a_eksak’);

grid on;

 

subplot(2,1,2)

plot(t,y(:,2),’*b’,t,b_eksak,’-r’);

legend(‘RK2y’,’b_eksak’);

grid on;

 

 

  1. Dengan Metode RK 4

%Script Metode RK 4

clear all;

clc;

h=0.1;

t=0:h:2;

N=length(t)-1;

a(1)=1;

b(1)=-1;

 

y(1,:)=[a(1) b(1)];

for n=1:N

k1=h*fungsi_baru(y(n,:));

k2=h*fungsi_baru(y(n,:)+0.5*k1);

k3=h*fungsi_baru(y(n,:)+0.5*k2);

k4=h*fungsi_baru(y(n,:)+k3);

y(n+1,:)=y(n,:)+(k1+2*k2+2*k3+k4)/6;

a_eksak=exp(-t);

b_eksak=-exp(-t);

 

 

end

figure(2);

subplot(2,1,1)

plot(t,y(:,1),’*m’,t,a_eksak,’-g’);

legend(‘RK4x’,’a_eksak’);

grid on;

 

subplot(2,1,2)

plot(t,y(:,2),’*b’,t,b_eksak,’-r’);

legend(‘RK4y’,’b_eksak’);

grid on;

 

 

  1. Metode Implisit

%script Metode Implisit

clear all;

clc;

h=0.1;

t=0:h:2;

N=length(t)-1;

a(1)=1;

b(1)=-1;

y(1,:)=[a(1) b(1)];

g(1,:)=[a(1) b(1)];

 

for n=1:N

y(n+1,:)=y(n,:)+h*fungsi_baru(y(n,:));

g(n+1,:)=g(n,:)+h*fungsi_baru(y(n+1,:));

a_eksak=exp(-t);

b_eksak=-exp(-t);

 

end

 

figure(4)

subplot(2,1,1)

plot(t,g(:,1),’*m’,t,a_eksak,’-g’);

legend(‘Implisit’,’a_eksak’);

grid on;

 

subplot(2,1,2)

plot(t,g(:,2),’*b’,t,b_eksak,’-r’);

legend(‘Implisit’,’b_eksak’);

grid on;

 

 

  1. Metode Eksplisit

%script Metode Eksplisit

clc;

h=0.1;

t=0:h:2;

N=length(t)-1;

a(1)=1;

b(1)=-1;

y(1,:)=[a(1) b(1)];

for n=1:N

y(n+1,:)=y(n,:)+h*fungsi_baru(y(n,:));

a_eksak=exp(-t);

b_eksak=-exp(-t);

 

end

figure(3);

subplot(2,1,1)

plot(t,y(:,1),’*m’,t,a_eksak,’-g’);

legend(‘Eksplisit’,’a_eksak’);

grid on;

 

subplot(2,1,2)

plot(t,y(:,2),’*b’,t,b_eksak,’-r’);

legend(‘Eksplisit’,’b_eksak’);

grid on;