ariestamirra

Metode Iterasi

Apr 1st, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. double N,a,e,xr,x0,yr;
  5. int i;
  6.  
  7. double f(double x){
  8.     return exp(-x) - x;
  9. }
  10.  
  11. double g(double x){
  12.     return exp(-x);
  13. }
  14.  
  15. void title(){
  16.     puts("Praktikum Metode Numerik");
  17.     puts("Metode Iterasi Sederhana\n");
  18. }
  19.  
  20. void input(){
  21.     fflush(stdin);
  22.     printf("Pendekatan awal : ");
  23.     scanf("%lf",&a);
  24.     fflush(stdin);
  25.     printf("Masukkan toleransi : ");
  26.     scanf("%lf",&e);
  27.  
  28.     fflush(stdin);
  29.     printf("Masukkan jumlah iterasi : ");
  30.     scanf("%lf",&N);
  31. }
  32.  
  33. //Metode Iterasi Sederhana
  34. void metode(){
  35.  
  36.     xr = a;
  37.     i = 0;
  38.     printf("%-4s \t%-8s \t%-8s \t%-8s\n","i","xr","f(xr)","g(xr)");
  39.     printf("%-4d \t%-8lg \t%-8lg \t%-8lg\n",i+1,xr,f(xr),g(xr));
  40.     do{
  41.         i++;
  42.         xr = g(xr);
  43.         yr = f(xr);
  44.         printf("%-4d \t%-8lg \t%-8lg \t%-8lg\n",i+1,xr,f(xr),g(xr));
  45.     }while(fabs(f(xr))>e && i<N);
  46.  
  47.     printf("Nilai xr = %lg\ndan f(xr) = %lg\n\n",xr,f(xr));
  48.     printf("Nilai error nya : %lg\n",fabs(f(xr)));
  49. }
  50.  
  51. char ulangi(){
  52.     char opt;
  53.     fflush(stdin);
  54.     printf("Ulangi [y/t] ? :");
  55.     scanf("%c",&opt);
  56.     if(opt=='y'||opt=='Y')
  57.         opt=1;
  58.     else
  59.         opt=0;
  60.     return opt;
  61. }
  62.  
  63. int main()
  64. {
  65.     title();
  66.     do{
  67.         input();
  68.         metode();
  69.     }while(ulangi());
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment