Teknik Komputasi >> Interpolasi DIFFERENSIAL

/* Contoh soal Metode Runge-Kutta orde 4 untuk penyelesaian Diferensiasi Numerik Bab
VII Hal. 67-68 Diktat Metode Komputasi 2004 */
/* Nama Program : runge-kutta4.cpp */
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#define MaksData 100
main()
{
FILE *runge_kutta4;
double k1[MaksData], k2[MaksData],
k3[MaksData], k4[MaksData],
x, x0, y0, yn1, ytemp,
h;
int i;
char lagi;
do
{
clrscr();
runge_kutta4 = fopen("runge_kutta4.txt", "w+");
cout << "\n\nMetode Runge-Kutta Orde 4 untuk Diferensiasi Numerik\n";
cout << "====================================================\n\n";
cout << "Contoh pada Diktat Metode Komputasi\n\n";
cout << "Persamaan Diferensial : f(x,y) = dy/dx = y + x\n";
cout << "----------------------------------------------\n\n";
/***************************************************/
/* Setiap perubahan persamaan matematika, YAKINKAN */
/* bahwa Anda telah merubah persamaan matematika */
/* di dalam program ini -------------------------- */
/***************************************************/
/* Pemasukan Data yang diberikan */
cout << "\nMasukkan x[0] ? ";
cin >> x0;
cout << "\nMasukkan y[0] ? ";
cin >> y0;
cout << "\nNilai titik x yang dicari ? ";
cin >> x;
cout << "\nInterval h ? ";
cin >> h;
printf("\n\n\tx[0] = %.2f", x0);
printf("\n\n\ty[0] = %.2f", y0);
printf("\n\n\tYang dicari adalah y[%.2f]", x);
printf("\n\n\th = %.2f", h);
/* Cetak x[i], y[i] dan f[i] serta penghitungan Runge-Kutta Orde 4 */
printf("\n\n\t-------------------------------------------------------\n");
printf("\tIterasi\t\tk1\tk2\tk3\tk4\ty[i]\n");
printf("\t-------------------------------------------------------\n");
fprintf(runge_kutta4,"\n\t-------------------------------------------------------\n");
fprintf(runge_kutta4,"\tIterasi\t\tk1\tk2\tk3\tk4\ty[i]\n");
fprintf(runge_kutta4,"\t-------------------------------------------------------\n");
i = 0;
/* Pemasukan dan Penghitungan Data */
while(x0 != x)//for(i = 0; i < n; i++)
{
k1[i] = y0 + x0;
k2[i] = (y0 + h*k1[i]/2) + (x0 + h/2);
k3[i] = (y0 + h*k2[i]/2) + (x0 + h/2);
k4[i] = (y0 + h*k3[i]) + (x0 + h);
/* Rumus Diferensiasi Runge-Kutta Orde 4 */
yn1 = y0 + h*(k1[i] + 2*k2[i] + 2*k3[i] + k4[i])/6;
x0 += h;
printf("\t %d\t\t%.5f\t%.5f\t%.5f\t%.5f\t%.5f\n", i+1, k1[i], k2[i],
k3[i], k4[i], yn1);
fprintf(runge_kutta4,"\t %d\t\t%.5f\t%.5f\t%.5f\t%.5f\t%.5f\n", i+1,
k1[i], k2[i], k3[i], k4[i], yn1);
}
printf("\t-------------------------------------------------------\n");
fprintf(runge_kutta4,"\t-------------------------------------------------------\n");
fprintf(runge_kutta4,"\n\nx[0] = %.2f", x0);
fprintf(runge_kutta4,"\n\ny[0] = %.2f", y0);
fprintf(runge_kutta4,"\n\nYang dicari adalah y[%.2f]", x);
fprintf(runge_kutta4,"\n\nh = %.2f", h);
printf("\n\nMaka y[%.2f] = %.5f", x, y0);
fprintf(runge_kutta4,"\n\nMaka y[%.2f] = %.5f", x, y0);
fclose(runge_kutta4);
cout << "\n\nCoba lagi dengan data awal atau rumus yang berbeda (y/t) ? ";
cin >> lagi;
}while(lagi != 't');
return 0;
}

Berkomentar yang sopan ya, Terimakasih sahabat..
EmoticonEmoticon