Teknik Komputasi >> SIMPSON 3/8

/* Contoh soal Metode Simpson 3/8 untuk penyelesaian Integrasi Numerik Bab VI Hal. 54-
55 Diktat Metode Komputasi 2004 */
/* Nama Program : simpson3_8.cpp */
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
#define MaksU 100
#define e  2.718281828
main()
{
FILE *simpson3_8;
double u[MaksU], x[MaksU],
h, x1, x2,
L, L1, L2;
int i, j, k, n, opsi;
char lagi;
do
{
clrscr();
simpson3_8 = fopen("simpson3_8.txt", "w+");
cout << "\n\nMetode Simpson 3/8 untuk Integrasi Numerik\n";
cout << "==========================================\n\n";
/***************************************************/
/* Setiap perubahan persamaan matematika, YAKINKAN */
/* bahwa Anda telah merubah persamaan matematika */
/* di dalam program ini -------------------------- */
/***************************************************/
cout << "\nMasukkan Batas Bawah (x1) ? ";
cin >> x1;
cout << "\nMasukkan Batas Atas (x2) ? ";
cin >> x2;
cout << "\nMasukkan Besar Interval (h) ? ";
cin >> h;
cout << "\n\nCara pemasukan data : \n\n";
cout << "1. Rumus Integral (rubah dulu kode program ssi rumus)\n";
cout << "Contoh dalam program ini adalah : integral(0-10) 2x + 5*(e^-2) dx\n\n";
cout << "2. Tabel Data\n\n";
cout <<"Masukkan pilihan : ";
cin >> opsi;
switch(opsi)
{
case 1 :
{
cout << "\n\nPersamaan Integral : integral(0-10) 2x + 5*(e^-2) dx";
cout << "\n ------------------------------------------\n\n";
j = 0;
while(x1 != x2+h)
{
u[j] = ((2*x1) + 5*(pow(e,-2)));
x[j] = x1;
x1 += h;
printf("\nu[%d] = %.2f", j, u[j]);
j++;
}
break;
}
case 2 :
{
cout << "\n\nPemasukan Data Berdasarkan Tabel Data\n";
cout << "-------------------------------------\n";
cout << "\nBerapa Jumlah Data yang akan diproses (lihat tabel) ?";
cin >> n;
for(i = 0, j = 0; i < n; i++, j++)
{
printf("\nx[%.2f] = ", x1);
}
break;
}
default : cout << "\nNo Good ....... Try Again, please ...\n\n";
}
/* Cetak Data Awal */
printf("\n\nu[%d] = %.4f", j-j, u[j-j]);
printf("\tu[%d] = %.4f", j-1, u[j-1]);
printf("\th = %.2f", h);
cout << "\n\nTekan Enter untuk melihat hasil perhitungan ..... \n";
getch();
printf("\n\n\t-------------------------------\n");
printf("\tdata ke-\tx\tU(x)\n");
printf("\t--------------------------------\n\n");
fprintf(simpson3_8,"\n\t-------------------------------\n");
fprintf(simpson3_8,"\tdata ke-\tx\tU(x)\n");
fprintf(simpson3_8,"\t--------------------------------\n\n");
//Tampilkan data x dan U(x)
for(i = 0; i < j; i++)
{
printf("\t %d\t\t%.2f\t%.4f\n", i, x[i], u[i]);
fprintf(simpson3_8,"\t %d\t\t%.2f\t%.4f\n", i, x[i], u[i]);
}
printf("\n\t--------------------------------\n\n");
fprintf(simpson3_8,"\n\t--------------------------------\n\n");
cout << "\nTekan Enter untuk melihat hasil perhitungan ......\n";
getch();
printf("\n\n\t----------------------------------------\n");
printf("\titerasi ke-\tU(x)\tL1(x)\tL2(x)\n");
printf("\t----------------------------------------\n\n");
fprintf(simpson3_8,"\n\t---------------------------------------\n");
fprintf(simpson3_8,"\titerasi ke-\tU(x)\tL1(x)\tL2(x)\n");
fprintf(simpson3_8,"\t----------------------------------------\n\n");
//Perhitungan Simpson 3/8
for(i = 1; i <= j-2; i++)
{
k = fmod(i,3);
if(k != 0)
{
L1 += u[i];
printf("\t\t%d\t%.4f\t%.4f\t%.4f\n", i, u[i], L1, L2);
fprintf(simpson3_8,"\t\t%d\t%.4f\t%.4f\t%.4f\n", i, u[i], L1,
L2);
}
else
{
printf("\t\t%d\t%.4f\t%.4f\t%.4f\n", i, u[i], L1, L2);
fprintf(simpson3_8,"\t\t%d\t%.4f\t%.4f\t%.4f\n", i, u[i], L1,
L2);
}
/* Rumus Integrasi Simpson 3/8 */
L = 3*h*(u[j-j] + 3*L1 + 2*L2 + u[j-1])/8;
}
printf("\n\t----------------------------------------\n\n");
fprintf(simpson3_8,"\n\t----------------------------------------\n\n");
printf("\nMaka Luas daerah kurva L adalah %.4f", L);
fprintf(simpson3_8,"\nMaka Luas daerah kurva L adalah %.4f", L);
fclose(simpson3_8);
cout << "\n\nCoba lagi dengan data awal yang berbeda (y/t) ? ";
cin >> lagi;
}while(lagi != 't');
return 0;
}

Berkomentar yang sopan ya, Terimakasih sahabat..
EmoticonEmoticon