Di cky J Si l i t onga

CONTOH PENYELESAIAN PERSAMAAN DIFERENSIAL BIASA DENGAN METODE NUMERIK
MENGGUNAKAN BAHASA PEMROGRAMAN C

Persamaan yang akan diselesaikan:

= -2x
3
+ 12x
2
- 20x + 8.5
Penyelesaian akan dilakukan dengan metode Euler, Heun, Polygon, Ralston, dan Runge-Kutta order
3.
Dari hasil perhitungan metode numerik dapat dibandingkan hasil dan error masing-masing metode ter
Program berikut akan menyelesaikan persamaan diatas dari x=0 sampai x=4 dengan kondisi awal pa
da x=0 adalah y=1. Interval x/step x (x) dapat ditentukan di awal (min. 0.08, sesuai array yang didefi
Di cky J Si l i t onga
KODE PROGRAM

#include <stdio.h>
#include <math.h>

main()
{
int i, j;
char exit;
float step, x[50], x1[50], x2[50], x3[50], xk3[50], k3[50], dx,
yE[50], fx[50], fx1[50], fxr[50], fx2[50], fx3[50],fxr2[50], phi3[50],
yEuler[50], yHeun[50], yPolygon[50], yRalston[50], yRungeKutta3[50],
EEuler[50], EHeun[50], EPolygon[50], ERalston[50], ERungeKutta3[50];

printf("\nNUMERICAL METHOD FOR SOLVING ODE by DICKY SILITONGA\n\n");
printf("Equation to be solved: dy/dx = f(x,y) = -2x^3 + 12x^2 - 20x + 8.5 \n\n");
printf("step = "); scanf("%f",&step);
printf("---------------------------------------------------------------------------------------------");
printf("\n x yExc yEul yHeun yPoly yRals yRuKu3 EEul EHeun EPoly ERals ERuKu3\
n");
printf("---------------------------------------------------------------------------------------------\n");

dx=step;
i=1;
do
{
x[i] = (i-1)*dx;
x1[i] = x[i] + dx;
x2[i] = x[i] + (dx/2);
x3[i] = x[i] + (dx*0.75);
yE[0] = 1;
yE[i] = (-0.5*x1[i]*x1[i]*x1[i]*x1[i]) + (4*x1[i]*x1[i]*x1[i]) - (10*x1[i]*x1[i]) + (8.5*x1[i]) + 1;

//EULER METHOD
fx[i] = (-2*x[i]*x[i]*x[i]) + (12*x[i]*x[i]) - (20*x[i]) + 8.5;
yEuler[0] = 1;
yEuler[i] = (yEuler[i-1]) + (fx[i]*dx);
EEuler[i] = (yEuler[i-1] - yE[i-1])/(yE[i-1]) * 100;

//HEUN METHOD
fx1[i] = (-2*x1[i]*x1[i]*x1[i]) + (12*x1[i]*x1[i]) - (20*x1[i]) + 8.5;
fxr[i] = (fx[i] + fx1[i])/2;
yHeun[0] = 1;
yHeun[i] = (yHeun[i-1]) + (fxr[i]*dx);
EHeun[i] = (yHeun[i-1] - yE[i-1])/(yE[i-1]) * 100;

//POLYGON METHOD
fx2[i] = (-2*x2[i]*x2[i]*x2[i]) + (12*x2[i]*x2[i]) - (20*x2[i]) + 8.5;
yPolygon[0] = 1;
yPolygon[i] = (yPolygon[i-1]) + (fx2[i]*dx);
EPolygon[i] = (yPolygon[i-1] - yE[i-1])/(yE[i-1]) * 100;

//RALSTON METHOD
fx3[i] = (-2*x3[i]*x3[i]*x3[i]) + (12*x3[i]*x3[i]) - (20*x3[i]) + 8.5;
Di cky J Si l i t onga
fxr2[i] = (fx[i]/3) + (2*fx3[i])/3;
yRalston[0] = 1;
yRalston[i] = (yRalston[i-1]) + (fxr2[i]*dx);
ERalston[i] = (yRalston[i-1] - yE[i-1])/(yE[i-1]) * 100;

//RUNGE KUTTA ORDER 3 METHOD
for (j=1; j<=3; j++)
{
xk3[j] = x[i] + (j-1)*0.5*dx;
k3[j] = (-2*xk3[j]*xk3[j]*xk3[j]) + (12*xk3[j]*xk3[j]) - (20*xk3[j]) + 8.5;
};
phi3[i] = (k3[1] + 4*k3[2] + k3[3])/6;
yRungeKutta3[0] = 1;
yRungeKutta3[i] = (yRungeKutta3[i-1]) + (phi3[i]*dx);
ERungeKutta3[i] = (yRungeKutta3[i-1] - yE[i-1])/(yE[i-1]) * 100;

printf("%.2f %.3f %.3f %7.3f %7.3f %7.3f %7.3f %7.2f %7.2f %7.2f%7.2f %7.2f\n",
x[i], yE[i-1], yEuler[i-1], yHeun[i-1], yPolygon[i-1], yRalston[i-1], yRungeKutta3[i-1], EEuler[i],
EHeun[i], EPolygon[i], ERalston[i], ERungeKutta3[i-1]);
i++;
}
while ((i-1)*dx<=4.000);

printf("---------------------------------------------------------------------------------------------\n");
printf("\n");
printf("yE = Exact value\n");
printf("yEul = Euler method value\n");
printf("yHeun = Heun method value\n");
printf("yPoly = Polygon method value\n");
printf("yRals = Ralston method value\n");
printf("yRuKu3= Runge Kutta order 3 method value\n");
printf("EEul = Euler method error \n");
printf("EHeun = Heun method error\n");
printf("EPoly = Polygon method error\n");
printf("ERals = Ralston method error\n");
printf("ERuKu3= Runge Kutta order 3 method error\n\n\n");
printf("type any key then enter to exit\n");
scanf("%s",&exit);
}

Di cky J Si l i t onga
OUTPUT

Berikut ini adalah output dari program untuk step x = 0.5

Di cky J Si l i t onga
untuk x = 0.25,

Dari hasil diatas, dapat dilihat perbedaan masing-masing metode berdasarkan nilai Errornya. Metode
Euler adalah yang paling sederhana namun Errornya besar, sementara metode Runge-Kutta mengha
silkan error yang paling kecil (bahkan 0 % ditampilkan di layar).