You are on page 1of 4

Contoh kode program C++ penyelesaian metode selisih tengahan untuk persamaan f = x^x^x.

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

using namespace std;


float tengahan(float x, float h);

int main(){
float x1,h1;
cout << "masukan nilai h :";
cin >> h1;
cout << "masukan nilai x :";
cin >> x1;
cout << "selisih tengahan fungsi x^x^x :";
cout << tengahan(x1,h1);
return 0;
}

float tengahan(float x, float h){


float f,f1,f2,f3,f4,f5,f6;
f = x+h;
f1 =pow(f,f);
f2 =pow(f,f1);
f3= x-h;
f4 =pow(f3,f3);
f5 =pow(f3,f4);
f6= (f2 - f5)/(2*h);
return f6;
}
#include <iostream>>

#include <stdio.h>

#include <math.h>

using namespace std;

double a,b,x,ft,fek,fx,error,sigma=0,total_error,h;

int i=0;

double f(double x)

return(exp(-x)*sin(2*x)+1);

double f_eksak(double x)

return(-exp(-x)*sin(2*x)+exp(-x)*2*cos(2*x));

double fungsi_tengah(double x,double h)

return((f(x+h)-f(x-h))/(2*h));

void tengah()
{

printf("masukan batas atas=");

scanf("%lf",&b);

printf("masukan batas bawah=");

scanf("%lf",&a);

printf("masukan nilai step h= ");

scanf("%lf",&h);

puts("==========================================================================");

printf("%s\t %8s\t %8s\t %8s\t %8s\n\n","x","f(x)","f'(x)","eksak","error");

puts("==========================================================================");

for(x=a;x<=b;x+=h)

{ i++;

fx=f(x);

ft=fungsi_tengah(x,h);

fek=f_eksak(x);

error=fabs(fek-ft);//mencari error

printf(" %g \t %8lf \t %8lf \t %8lf \t %8lf\n",x,fx,ft,fek,error);

sigma=sigma+error;

total_error=sigma/i;//rata-rata error

printf("\nRata-rata error = %lf\n",total_error);


}

main()

{ puts("\t====================");

puts("\t DIFFERENSIAL");

puts("\tMetode selisih tengah");

puts("\t====================\n\n");

tengah();

You might also like