Skip to content

Instantly share code, notes, and snippets.

@dukenmarga
Created August 19, 2014 04:42
Show Gist options
  • Save dukenmarga/7312da7bc7c78fd83fe8 to your computer and use it in GitHub Desktop.
Save dukenmarga/7312da7bc7c78fd83fe8 to your computer and use it in GitHub Desktop.
Secant Method
/*
* program untuk mencari solusi persamaan
* dengan menggunakan metode secant
* coder : duken marga
*/
#include <stdio.h>
#include <math.h>
#define PHI 3.141592654
#define TOLERANCE 10E-10
float f(float x);
int main()
{
float x1, x2, x3;
int i, loop;
printf("Masukkan nilai bilangan inisiasi pertama : ");
scanf("%f", &x1);
printf("Masukkan nilai bilangan inisiasi kedua : ");
scanf("%f", &x2);
printf("Masukkan jumlah maksimum perulangan : ");
scanf("%d", &loop);
printf(" x1 \t x2 \t x3\n ");
for(i = 0; i < loop; i++){
x3 = x2 - f(x2) * (x1 - x2) / (f(x1) - f(x2));
printf("%f %f %f\n ", x1, x2, x3);
if( x2 - x3 < TOLERANCE){
break;
}
x1 = x2;
x2 = x3;
}
printf("Solusi persamaan adalah : %20.10f", x3);
}
/*
*
* f(x) = x^2 + x - cos(x)
*/
float f(float x){
//return x*x + x - cos(x * PHI / 180);
return x*x + x - cos(x * PHI / 180);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment