Soal
1. Buat algoritma untuk menyelesaikan turunan dengan 3 metode tersebut.2. Buat program dengan 3 metode kirim nanti source code dan capture hasil programnya.
Jawaban
1. Algoritma Differensiasi Numerik Selisih Maju
• Definisikan fungsi f(x) yang akan dicari nilai turunannya• Definisikan fungsi turunan f’eksak(x) sebenarnya
• Masukkan nilai pendekatan awal : batas bawah a, batas atas b, dan nilai step h
• Untuk x=a sampai dengan b hitung : f'x=(f(x+h)-(f(x)))/h
• Tampilkan nilai x, f(x),f’(x) dan f’eksak(x)
- Algoritma Differensiasi Numerik Selisih Tengah
• Definisikan fungsi f(x) yang akan dicari nilai turunannya
• Definisikan fungsi turunan f’eksak(x) sebenarnya
• Masukkan nilai pendekatan awal : batas bawah a, batas atas b, dan nilai step h
• Untuk x=a sampai dengan b hitung :'x=(f(x+h)-(f(x-h)))/2h
• Tampilkan nilai x, f(x), f’(x) dan f’eksak(x)
- Algoritma Differensiasi Numerik Selisih Mundur
• Definisikan fungsi (fx) yang akan dicari nilai turunannya
• Definisikan fungsi turunan f’eksak(x) sebenarnya
• Masukkan nilai pendekatan awal : batas bawah a, batas atas b, dan nilai step h
• Untuk x=a sampai dengan b hitung f'x=(f(x)-f(x-h))/h
• Tampilkan nilai x, f(x), f’(x) dan f’eksak(x)
2.Source Metode Selisih Maju
#include<stdio.h>#include<math.h>
double a,b,x,ft,fek,fx,error,sigma=0,total_error,h;
int i=0;
char jwb;
double f(double x){
return(exp(-x)*sin(2*x)+1);
}
double f_eksak(double x){
return (exp(-x) * (2*cos(2*x) - sin(2*x)));
}
double fungsi_maju(double x,double h){
return((f(x+h)-f(x))/h);
}
main(){
do{
input();
maju();
fflush(stdin);
printf("\n Coba lagi (Y/y) ? ");
jwb = getchar();
}while(jwb == 'Y' || jwb == 'y');
}
void input(){
puts("~~~~~~~~~~~~~~~~~Nama : Dwi Ratna S~~~~~~~~~~~~~~~~~");
puts("~~~~~~~~~~~~~~~~~Kelas : D3 PJJ 2017~~~~~~~~~~~~~~~~~");
puts("~~~~~~~~~~~~~~~~~NRP : 2103177001~~~~~~~~~~~~~~~~~");
puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\t\t\tMETODE SELISIH MAJU");
printf("\n\t\t+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n\n");
printf(" Masukkan Batas Bawah \t= ");
scanf("%lf",&b);
printf(" Masukkan Batas Atas \t= ");
scanf("%lf",&a);
printf(" Masukkan Nilai h \t= ");
scanf("%lf",&h);
}
void maju(){
printf("\n ======================================================================\n");
printf(" |%3s\t| %8s\t| %8s\t| %8s\t| %8s |\n","x","f(x)","Maju","eksak","error");
printf(" ======================================================================\n");
for(x=b;x<=a;x+=h)
{ i++;
fx=f(x);
ft=fungsi_maju(x,h);
fek=f_eksak(x);
error=fabs(fek-ft);
printf(" | %2lg\t| %8lg\t| %8lg\t| %8lg\t| %11lg |\n",x,fx,ft,fek,error);
sigma=sigma+error;
}
printf(" ======================================================================\n");
total_error=sigma/i;
printf("\n Rata-rata error = %lg\n",total_error);
}
- Capture Hasil Metode Selisih Maju
Source Code Metode Selisih Tengah
#include<stdio.h>
#include<math.h>
double a,b,x,ft,fek,fx,error,sigma=0,total_error,h;
int i=0;
char jwb;
double f(double x){
return(exp(-x)*sin(2*x)+1);
}
double f_eksak(double x){
return (exp(-x) * (2*cos(2*x) - sin(2*x)));
}
double fungsi_tengah(double x,double h){
return((f(x+h)-f(x-h))/(2*h));
}
main(){
do{
input();
tengah();
fflush(stdin);
printf("\n Coba lagi (Y/y) ? ");
jwb = getchar();
}while(jwb == 'Y' || jwb == 'y');
}
void input(){
puts("~~~~~~~~~~~~~~~~~Nama : Dwi Ratna S~~~~~~~~~~~~~~~~~");
puts("~~~~~~~~~~~~~~~~~Kelas : D3 PJJ 2017~~~~~~~~~~~~~~~~~");
puts("~~~~~~~~~~~~~~~~~NRP : 2103177001~~~~~~~~~~~~~~~~~");
puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\t\t\tMETODE SELISIH TENGAH");
printf("\n\t\t+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n\n");
printf(" Masukkan Batas Bawah \t= ");
scanf("%lf",&b);
printf(" Masukkan Batas Atas \t= ");
scanf("%lf",&a);
printf(" Masukkan Nilai h \t= ");
scanf("%lf",&h);
}
void tengah(){
printf("\n ======================================================================\n");
printf(" |%3s\t| %8s\t| %8s\t| %8s\t| %8s |\n","x","f(x)","Tengah","eksak","error");
printf(" ======================================================================\n");
for(x=b;x<=a;x+=h)
{ i++;
fx=f(x);
ft=fungsi_tengah(x,h);
fek=f_eksak(x);
error=fabs(fek-ft);
printf(" | %2lg\t| %8lg\t| %8lg\t| %8lg\t| %11lg |\n",x,fx,ft,fek,error);
sigma=sigma+error;
}
printf(" ======================================================================\n");
total_error=sigma/i;
printf("\n Rata-rata error = %lg\n",total_error);
}
Hasil
Source Code Metode Selisih Mundur
#include<stdio.h>
#include<math.h>
double a,b,x,ft,fek,fx,error,sigma=0,total_error,h;
int i=0;
char jwb;
double f(double x){
return(exp(-x)*sin(2*x)+1);
}
double f_eksak(double x){
return (exp(-x) * (2*cos(2*x) - sin(2*x)));
}
double fungsi_mundur(double x,double h){
return((f(x)-f(x-h))/h);
}
main(){
do{
input();
tengah();
fflush(stdin);
printf("\n Mau Lagi (Y/y) ? ");
jwb = getchar();
}while(jwb == 'Y' || jwb == 'y');
}
void input(){
puts("~~~~~~~~~~~~~~~~~Nama : Dwi Ratna S~~~~~~~~~~~~~~~~~");
puts("~~~~~~~~~~~~~~~~~Kelas : D3 PJJ 2017~~~~~~~~~~~~~~~~~");
puts("~~~~~~~~~~~~~~~~~NRP : 2103177001~~~~~~~~~~~~~~~~~");
puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\t\t\tMETODE SELISIH MUNDUR");
printf("\n\t\t+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n\n");
printf(" Masukkan Batas Bawah \t= ");
scanf("%lf",&b);
printf(" Masukkan Batas Atas \t= ");
scanf("%lf",&a);
printf(" Masukkan Nilai h \t= ");
scanf("%lf",&h);
}
void tengah(){
printf("\n ======================================================================\n");
printf(" |%3s\t| %8s\t| %8s\t| %8s\t| %8s |\n","x","f(x)","Mundur","eksak","error");
printf(" ======================================================================\n");
for(x=b;x<=a;x+=h)
{ i++;
fx=f(x);
ft=fungsi_mundur(x,h);
fek=f_eksak(x);
error=fabs(fek-ft);
printf(" | %2lg\t| %8lg\t| %8lg\t| %8lg\t| %11lg |\n",x,fx,ft,fek,error);
sigma=sigma+error;
}
printf(" ======================================================================\n");
total_error=sigma/i;
printf("\n Rata-rata error = %lg\n",total_error);
}
Hasil
EmoticonEmoticon