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