ariestamirra

Metode Newton Raphson

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