Teknik Komputasi >> SIMPSON 1/3

/* Contoh soal Metode Simpson 1/3 untuk penyelesaian Integrasi Numerik. Bab VI Hal.
52-53 Diktat Metode Komputasi 2004 */
/* Nama Program : simpson1_3.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 *simpson1_3;
double u[MaksU], x[MaksU],
h, x1, x2,
L, L1, L2;
int i, j, k, opsi;
char lagi;
do
{
clrscr();
simpson1_3 = fopen("simpson1_3.txt", "w+");
cout << "\n\nMetode Simpson 1/3 untuk Integrasi Numerik\n";
cout << "==========================================\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) x^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\n\n";
j = 0;
while(x1 != x2+h)
{
u[j] = 2*x1 + 5 e-2;
x[j] = x1;
x1 += h;
printf("\nu[%d] = %.2f", j, u[j]);
}
break;
}
case 2 :
{
cout << "\n\nPemasukan Data Berdasarkan Tabel Data\n";
cout << "-------------------------------------\n";
while(x1 != x2+h)
{
printf("\nx[%.1f] = ", x1);
cin >> u[j];
j++;
}
break;
}
default : cout << "\nTry Again .....\n\n";
}
printf("\n\nu[%d] = %.2f", j-j, u[j-j]);
printf("\tu[%d] = %.2f", j-1, u[j-1]);
printf("\th = %.2f", h);
printf("\n\n\t-------------------------------\n");
printf("\tdata ke-\tx\tU(x)\n");
printf("\t--------------------------------\n\n");
fprintf(simpson1_3,"\n\t-------------------------------\n");
fprintf(simpson1_3,"\tdata ke-\tx\tU(x)\n");
fprintf(simpson1_3,"\t--------------------------------\n\n");
//Tampilkan data x dan U(x)
for(i = 0; i < j; i++)
{
printf("\t %d\t\t%.1f\t%.2f\n", i, x[i], u[i]);
fprintf(simpson1_3,"\t %d\t\t%.1f\t%.2f\n", i, x[i], u[i]);
}
printf("\n\t--------------------------------\n\n");
fprintf(simpson1_3,"\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(simpson1_3,"\n\t---------------------------------------\n");
fprintf(simpson1_3,"\titerasi ke-\tU(x)\tL1(x)\tL2(x)\n");
fprintf(simpson1_3,"\t----------------------------------------\n\n");
for(i = 1; i <= j-2; i++)
{
k = fmod(i,2);
if(k != 0)
{
L1 += u[i];
printf("\t\t%d\t%.2f\t%.2f\t%.2f\n", i, u[i], L1, L2);
fprintf(simpson1_3,"\t\t%d\t%.2f\t%.2f\t%.2f\n", i, u[i], L1,L2);
}
else
{
L2 += u[i];
printf("\t\t%d\t%.2f\t%.2f\t%.2f\n", i, u[i], L1, L2);
fprintf(simpson1_3,"\t\t%d\t%.2f\t%.2f\t%.2f\n", i, u[i], L1,L2);
}
/* Rumus Integrasi Simpson 1/3 */
L = h*(u[j-j] + 4*L1 + 2*L2 + u[j-1])/3;
}
printf("\n\t----------------------------------------\n\n");
fprintf(simpson1_3,"\n\t----------------------------------------\n\n");
printf("\nMaka Luas daerah kurva L adalah %.2f", L);
fprintf(simpson1_3,"\nMaka Luas daerah kurva L adalah %.2f", L);
fclose(simpson1_3);
cout << "\n\nCoba lagi dengan data awal yang berbeda (y/t) ? ";
}while(lagi != 't');
return 0;
}

Berkomentar yang sopan ya, Terimakasih sahabat..
EmoticonEmoticon