Skip to content

Instantly share code, notes, and snippets.

@dukenmarga
Created August 19, 2014 04:43
Show Gist options
  • Select an option

  • Save dukenmarga/eabb722cec4c504e9ccb to your computer and use it in GitHub Desktop.

Select an option

Save dukenmarga/eabb722cec4c504e9ccb to your computer and use it in GitHub Desktop.
Bisection Method
/*
* metode numerik dengan bisection method
* author : duken marga
*/
#include <stdio.h>
#include <math.h>
#define EPSILON 10E-10
#define PHI 3.141592654
float f(float x);
int main()
{
float left, right, mid;
printf("Masukkan nilai pertama : ");
scanf("%f", &left);
printf("Masukkan nilai kedua : ");
scanf("%f", &right);
do{
mid = (right + left) / 2;
printf("%f %f %f \n", f(left), f(mid), f(right));
if(f(left) * f(mid) < 0){
right = mid;
}
else if(f(right) * f(mid) < 0){
left = mid;
}
else
break;
}while(abs(right-left) < 2 * EPSILON);
printf("Solusi persamaan adalah : %f", mid);
}
/*
* f(x) = x*x - 2*x + 1;
*/
float f(float x){
return x*x - 9;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment