Business

Wednesday, 1 July 2015

Pemrograman (Metode Newton Rapshon)

#include<iostream.h>
#include<iomanip.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define epsilon 1E-15
main()
{
int p,it;
char lagi;
long double x, fx, f_x,fs,xl,fxl,f_xl;
awal:
clrscr();

textcolor(10);cprintf("========================================================================");cout<<endl;
textcolor(12);cprintf("                            NEWTON RAPHSON                              ");cout<<endl;
textcolor(10);cprintf("========================================================================");cout<<endl;
cout<<endl;
cout<<endl;
printf("Soal-soal dengan penyelesaian Metode Newton Repson : ");cout<<endl;
cout<<endl;
textcolor(12);cprintf("1. 3x-cos(x)=0");cout<<endl;
textcolor(12);cprintf("2. tan(x)=1-x");cout<<endl;
cout<<endl;
cout<<endl;
textcolor(10);cprintf("Ingin Jawaban Nomer Berapa ? ");cin>>p;cout<<endl;

if(p==1)
{
   clrscr();
    textcolor(2);cprintf("============================================================");cout<<endl;
    textcolor(12);cprintf("                     NEWTON  RAPHSON                       ");cout<<endl;
    textcolor(2);cprintf("============================================================");cout<<endl;
    cout<<endl;
    textcolor(12);cprintf("  Soal No. 1. 3x-cos(x)=0");cout<<endl;
   cout<<endl;
    textcolor(10);cprintf("==============================================================");cout<<endl;
    textcolor(10);cprintf("|");
    printf(" Iterasi ");
    textcolor(10);cprintf("|");
    printf("     x     ");
    textcolor(10);cprintf("|");
    printf("    f(x)   ");
    textcolor(10);cprintf("|");
    printf("   f'(x)   ");
    textcolor(10);cprintf("|");
    printf(" KETERANGAN ");
    textcolor(10);cprintf("|");cout<<endl;
    textcolor(10);cprintf("============================================================");cout<<endl;

    it=0;
    x=1;
    fx=(3*x)-cos(x*3.14/180);
    f_x=(3+sin(x*3.14/180));
    textcolor(10);cprintf("|");
    printf(" %4d    ",it);
    textcolor(10);cprintf("|");
    printf("%10.2Le ",x);
    textcolor(10);cprintf("|");
    printf("%10.2Le ",fx);
    textcolor(10);cprintf("|");
    printf("%10.2Le ",f_x);
    textcolor(10);cprintf("|");
    printf("            ");
    textcolor(10);cprintf("|");cout<<endl;

    do
    {
    it+=1;
    xl=x-(fx/f_x);
    fxl=(3*xl)-cos(xl*3.14/180);
    f_xl=(3+sin(xl*3.14/180));
    fs=fabs(xl-x);

    textcolor(10);cprintf("|");
    printf(" %4d    ",it);
    textcolor(10);cprintf("|");
    printf("%10.2Le ",xl);
    textcolor(10);cprintf("|");
    printf("%10.2Le ",fxl);
    textcolor(10);cprintf("|");
    printf("%10.2Le ",f_xl);
    textcolor(10);cprintf("|");

    x=xl;
    fx=fxl;
    f_x=f_xl;

    if(fs<=epsilon)
    {
    textcolor(12);cprintf("   SELESAI  ");
    }
    else
    {
    printf("   LANJUT   ");
    }
    textcolor(10);cprintf("|");
    cout<<endl;
    }
    while(it<=90 && fs>=epsilon);
    textcolor(10);cprintf("============================================================");cout<<endl;
    cout<<endl<<endl;

    printf("   Nilai Akar : %20.17Le",xl);cout<<endl;
    printf("   Selesai Pada Iterasi Ke- %d",it);cout<<endl;

    textcolor(2);cprintf(" Masih Mau dengan Soal yang Lainnya ? [Y/T] : \n");

    lagi=getche();
    if(lagi=='Y' || lagi=='y')
    goto awal;

    getche();
    }
else if(p==2)
{
clrscr();
textcolor(2);cprintf("============================================================");cout<<endl;
textcolor(12);cprintf("                     NEWTON  RAPHSON                       ");cout<<endl;
textcolor(2);cprintf("============================================================");cout<<endl;
cout<<endl;
textcolor(12);cprintf("  Soal No. 2 : tan(x)=1-x");cout<<endl;
textcolor(10);cprintf("============================================================");cout<<endl;
textcolor(10);cprintf("|");
printf(" Iterasi ");
textcolor(10);cprintf("|");
printf("     x     ");
textcolor(10);cprintf("|");
printf("    f(x)   ");
textcolor(10);cprintf("|");
printf("   f'(x)   ");
textcolor(10);cprintf("|");
printf(" KETERANGAN ");
textcolor(10);cprintf("|");cout<<endl;
textcolor(10);cprintf("============================================================");cout<<endl;
it=0;
x=1;
fx =tan(x*3.14/180)-1+x;
f_x= 1/pow(cos(x*3.14/180),2)+1;
textcolor(10);cprintf("|");
printf(" %4d    ",it);
textcolor(10);cprintf("|");
printf("%10.2Le ",x);
textcolor(10);cprintf("|");
printf("%10.2Le ",fx);
textcolor(10);cprintf("|");
printf("%10.2Le ",f_x);
textcolor(10);cprintf("|");
printf("            ");
textcolor(10);cprintf("|");cout<<endl;

do
{
it+=1;
xl=x-(fx/f_x);
fxl= tan(xl*3.14/180)-1+xl;
f_xl=1/pow(cos(xl*3.14/180),2)+1;

fs=fabs(xl-x);

textcolor(10);cprintf("|");
printf(" %4d    ",it);
textcolor(10);cprintf("|");
printf("%10.2Le ",xl);
textcolor(10);cprintf("|");
printf("%10.2Le ",fxl);
textcolor(10);cprintf("|");
printf("%10.2Le ",f_xl);
textcolor(10);cprintf("|");

x=xl;
fx=fxl;
f_x=f_xl;

if(fs<=epsilon)
{
textcolor(12);cprintf("   SELESAI  ");
}
else
{
printf("   LANJUT   ");
}
textcolor(10);cprintf("|");
cout<<endl;
if(it%10==0)
{
getche();
}

}
while(it<=90 && fs>=epsilon);
textcolor(10);cprintf("============================================================");cout<<endl;
cout<<endl<<endl;

printf("   Nilai Akar : %20.17Le",xl);cout<<endl;
printf("   Selesai Pada Iterasi Ke- %d",it);cout<<endl;

textcolor(2);cprintf(" Masih Mau dengan Soal yang Lainnya ? [Y/T] : \n");

lagi=getche();
if(lagi=='Y' || lagi=='y')
goto awal;

getche();
}
else
{
printf("Nomer Yang Anda Masukkan Tidak Terdaftar dalam Soal");cout<<endl;
textcolor(2);cprintf(" Masih Mau dengan Soal yang Lainnya ? [Y/T] : \n");

lagi=getche();
if(lagi=='Y' || lagi=='y')
goto awal;
}
getche();
}

0 komentar:

Post a Comment