Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- double N,a,b,e,xr;
- int i;
- double f(double x){
- return exp(-x) - x;
- }
- void title(){
- puts("Praktikum Metode Numerik");
- puts("Metode Biseksi\n");
- }
- void input(){
- fflush(stdin);
- printf("Masukkan batas bawah : ");
- scanf("%lf",&a);
- fflush(stdin);
- printf("Masukkan batas atas : ");
- scanf("%lf",&b);
- fflush(stdin);
- printf("Masukkan toleransi : ");
- scanf("%lf",&e);
- fflush(stdin);
- printf("Masukkan jumlah iterasi : ");
- scanf("%lf",&N);
- }
- //Metode Biseksi
- void metode(){
- if(f(a)*f(b)>0){
- puts("Tidak ada akar");
- return;
- }
- i = 0;
- printf("%-4s\t%-8s\t%-8s\t%-8s\t%-8s\t%-8s\t%-8s\n","i","a","b","xr","f(xr)","f(a)","Keterangan");
- do{
- i++;
- xr = (a + b)/2;
- printf("%-4d\t%-8lf\t%-8lf\t%-8lf\t%-8lf\t%-8lf\t%-8s\n",i,a,b,xr,f(xr),f(a),((f(a)*f(xr))<0)?"Berbeda tanda":"");
- if(f(a)*f(xr)<0){
- b=xr;
- }else
- a=xr;
- }while(fabs(b-a)>e && i<N);
- printf("Nilai xr = %lg\ndan f(xr) = %lg\n\n",xr,f(xr));
- printf("Nilai error nya : %lg\n",fabs(f(xr)));
- }
- char ulangi(){
- char opt;
- fflush(stdin);
- printf("Ulangi [y/t] ? :");
- scanf("%c",&opt);
- if(opt=='y'||opt=='Y')
- opt=1;
- else
- opt=0;
- return opt;
- }
- int main()
- {
- title();
- do{
- input();
- metode();
- }while(ulangi());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment