Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<math.h>
- double x1,x2,er,e;
- int N;
- double f1(double x)
- {
- return (1+exp(-x));
- }
- double f(double y)
- {
- return y-exp(-y);
- }
- void title(){
- puts("Praktikum Metode Numerik");
- puts("Metode Newton Raphson\n");
- }
- void input(){
- fflush(stdin);
- printf("Pendekatan awal : ");
- scanf("%lf",&x1);
- fflush(stdin);
- printf("Masukkan toleransi : ");
- scanf("%lf",&e);
- fflush(stdin);
- printf("Masukkan jumlah iterasi : ");
- scanf("%d",&N);
- }
- //Newton Raphson
- void metode()
- {
- int i=0;
- printf("%-4s %-20s %-20s %-8s\n","i","x","f(x)","f'(x)");
- do
- {
- x2=x1-(f(x1)/f1(x1));
- printf("%-4d %-20lg %-20lg %-8lg \n",i,x1,f(x1),f1(x1));
- er=fabs(f(x1));
- x1=x2;
- i++;
- }
- while(i<N && er>e);
- printf("akar : %lg\n",x1);
- }
- 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