listing program

24
LISTING PROGRAM 1. Metoda Bisection Untuk x 3 + 4x 2 - 10 // Metoda Bisection abdul #include<stdio.h> #include<math.h> #include<conio.h> float f(float x) { float g; g=(x*x*x) + (4*x*x) - 10; return g; } main() { clrscr(); float x1, x2, x3, e, d,c,a,b; int i=0; printf("Silakan input nilai x1 ="); scanf("%f", &x1); printf("Silakan input nilai x2 ="); scanf("%f", &x2); printf("Silakan input nilai error ="); scanf("%f", &e); printf("iterasi nilaix1 nilaix2 nilaix3 f(x1) f(x3) e\n"); do { i=i+1; x3=(x1+x2)/2; d=(fabs(x1-x2))/2; printf("%d %.4f %.4f %.4f %.4f %.4f %.4f\n",i, x1, x2, x3, f(x1), f(x3),d); if((f(x1)*f(x3)) < 0) { x2=x3; } else { x1=x3; } } while(d>e); printf("Input banyaknya Iterasi = %d\n", i); printf("Nilai akar pendekatannya adalah = %f\n",x2); getch(); return 0; } 2. Metoda Bisection Untuk 3x – e x // Metoda Bisection abdul #include<stdio.h> #include<math.h> #include<conio.h> float f(float x) { float g; g=3*x-exp(x); 1

Upload: ragelok

Post on 21-Nov-2014

197 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Listing Program

LISTING PROGRAM1. Metoda Bisection Untuk x3 + 4x2 - 10// Metoda Bisection abdul#include<stdio.h>#include<math.h>#include<conio.h>float f(float x){float g;g=(x*x*x) + (4*x*x) - 10;return g;}main(){clrscr();float x1, x2, x3, e, d,c,a,b;int i=0;

printf("Silakan input nilai x1 =");scanf("%f", &x1);printf("Silakan input nilai x2 =");scanf("%f", &x2);printf("Silakan input nilai error =");scanf("%f", &e);printf("iterasi nilaix1 nilaix2 nilaix3 f(x1) f(x3) e\n");

do{ i=i+1; x3=(x1+x2)/2; d=(fabs(x1-x2))/2;printf("%d %.4f %.4f %.4f %.4f %.4f %.4f\n",i, x1, x2, x3, f(x1), f(x3),d);if((f(x1)*f(x3)) < 0) { x2=x3; } else { x1=x3; }}while(d>e);printf("Input banyaknya Iterasi = %d\n", i);printf("Nilai akar pendekatannya adalah = %f\n",x2);

getch();return 0;

}

2. Metoda Bisection Untuk 3x – ex

// Metoda Bisection abdul

#include<stdio.h>#include<math.h>#include<conio.h>float f(float x){float g;g=3*x-exp(x);return g;}

main(){clrscr();float x1, x2, x3, e, d,c,a,b;int i=0;

printf("Silakan input nilai x1 =");scanf("%f", &x1);printf("Silakan input nilai x2 =");scanf("%f", &x2);printf("Silakan input nilai error =");

1

Page 2: Listing Program

scanf("%f", &e);printf("iterasi nilaix1 nilaix2 nilaix3 f(x1) f(x3) e\n");

do

{ i=i+1; x3=(x1+x2)/2; d=(fabs(x1-x2))/2;printf("%d %.4f %.4f %.4f %.4f %.4f %.4f\n",i, x1, x2, x3, f(x1), f(x3),d);if((f(x1)*f(x3)) < 0) { x2=x3; } else { x1=x3; }}

while(d>e);printf("Input banyaknya Iterasi = %d\n", i);printf("Nilai akar pendekatannya adalah = %f\n",x2);

getch();return 0; }

3. Metoda Iterasi Titik Tetap// Program Iterasi abdul#include<stdio.h>#include<math.h>#include<conio.h>float x1, d;int imax;float g (float x){

float z;z=(sqrt(10-(x*x*x)))/2;return z;

}void main(){

float x2, e2;int i;printf("Input nilai tebakan awal =");scanf("%f", &x1);printf("e1="); scanf("%f", &d);printf("Input Iterasi max="); scanf("%d", &imax);i=0;printf("i, g(x) f(x) \n");

do{

i=i+1;x2=g(x1);e2=fabs(x2-x1);printf("%2d\t%f\t%f\t%f\n", i,x1, x2, e2);x1=x2;

}while(i<=imax&&e2>d);if(i<=imax){

printf("Terpenuhi\n");printf("Nilai pendekatannya =%g\n", x2);

}elseprintf("Tidak terpenuhi\n");getch(); }

4. Metoda Newton-Rhapson Untuk x3 + 4x2 - 10// Program Newton-Rapshon Abdul

2

Page 3: Listing Program

#include<stdio.h>#include<math.h>#include<conio.h>float x1,d;int imax;float f(float x){

return (x*x*x)+4*x*x-10;}float g(float x){

return (3*x*x)+(8*x);}void main(){

clrscr();float x2,e;int i;

printf("Masukkan tebakan awal=");scanf("%f", &x1);printf("e2=");scanf("%f", &d);printf("Masukkan iterasi maximum=");scanf("%d", &imax);i=0;printf(" i\t x1\t x2\t f(x)\t e\n");do{

i=i+1;x2=x1-f(x1)/g(x1);e=fabs(x2-x1);printf("%2d\t %.4f\t %.4f\t %.4f\t %.4f\t\n", i,x1,x2,g(x2),e);x1=x2;

}while (i<=imax && e>d);if(i<=imax){printf("Terpenuhi\n");printf("Nilai pendekatannya:%g\n",x2);}elseprintf("Tidak terpenuhi");getch(); }

5. Metoda Newton-Rhapson Untuk 3x – ex

// Program Newton-Rapshon Abdul#include<stdio.h>#include<math.h>#include<conio.h>float x1,d;int imax;float f(float x){

return 3*x-exp(x);}float g(float x){

return 3-exp(x);}void main(){

clrscr();float x2,e;int i;

printf("Masukkan tebakan awal=");scanf("%f", &x1);printf("e2=");scanf("%f", &d);printf("Masukkan iterasi maximum=");scanf("%d", &imax);

3

Page 4: Listing Program

i=0;printf(" i\t x1\t x2\t f(x)\t e\n");do{

i=i+1;x2=x1-f(x1)/g(x1);e=fabs(x2-x1);printf("%2d\t %.4f\t %.4f\t %.4f\t %.4f\t\n", i,x1,x2,g(x2),e);x1=x2;

}while (i<=imax && e>d);if(i<=imax){printf("Terpenuhi\n");printf("Nilai pendekatannya:%g\n",x2);}elseprintf("Tidak terpenuhi");getch();}

1. LISTING PROGRAM /*POWER METHODE*/#include<stdio.h>#include<math.h>#define NMAX 31typedef double MAT[NMAX][NMAX];int i,it,a,b;double dta,eps,gamma;MAT A;double X[NMAX];void PWM(double eps,double dta,int a,int b, MAT A, int *it,double *gamma, double *X1){int i,j,l;double phi,s;double X0[NMAX];for (i=1;i<=b;i++) X0[i]=1.0/sqrt(i);*it=-1; l=1;while (*it==-1 && l<=m)

{*gamma=0.0;for (i=1;i<=b;i++){X1[i]=0.0;for (j=1;j<=b;j++) X1[i] += A[i][j]*X0[j];if (fabs(X1[i])>fabs(*gamma)) *gamma=X1[i];

}if (fabs(*gamma) < eps) *it=0;

else{for (i=1; i<=b; i++) X1[i] /= *gamma;phi=0.0;for (i=1; i<=b; i++)

{s=fabs(X1[i]-X0[i]);if (s>phi) phi=s;}if (phi<dta) *it=1;else {

4

Page 5: Listing Program

for (i=1; i<=b; i++) X0[i]=X1[i];l++;} } } }void Read_data() {int i,j;printf("\nMaTRIKS nxn (max%d): ",NMAX-1); scanf("%d", &n);for (i=1; i<=n; i++) {printf("\nBARIS KE-%d\n", i);for (j=1; j<=n; j++) {printf("ELEMEN KE-%d: ", j); scanf("%lf", &A[i][j]);} }printf("\nTOLERANSI: ");scanf("%lf", &dta);printf("ERROR: ");scanf("%lf", &eps);printf("MAX ITERASI: ");scanf("%d", &m);}void main(){Read_data();PWM(eps,dta,a,b,A,&it,&gamma,X);switch(it+1) {case 0: printf("NOT KONVERGEN !!!!\n");break;case 1: printf("NOT AVALIABLE TO NEXT!!!!.\n"); break;case 2: printf("\nEIGEN VALUE: %f\n\n", gamma);printf("EIGEN VECTOR:\n");for (i=1; i<=n; i++) printf(" %f\n", X[i]);} }

LISTING PROGRAM1. METODA ITERASI JACOBI

// iterasi jacobi abdul#include<stdio.h>#include<math.h>#include<conio.h>float A[5][5]={{-3, -1, 1},

{3, 6, 2}, {3, 3, 7}};

float B[5]={1, 0, 4};float x[5];float x_1[5];float Eps = 0.00001;int i, j, n=3, TRUE, iter;void main(){

TRUE = 0;iter = 0;// inisialisasi

5

Page 6: Listing Program

for (i=0;i<n;i++) x[i]=0;// lakukan iterasi sampai konvergenwhile (!TRUE){

iter = iter + 1;TRUE = 1; // asumsi sudah konvergenfor(i=0;i<n;i++){

x_1[i]=B[i];for(j=0;j<n;j++); if(i!=j) x_1[i]=x_1[i]-A[i][j]*x[j];x_1[i]=x_1[i]/A[i][i];if(fabs(x_1[i]-x[i])>Eps) TRUE = 0; // belum konvergen

}// tulis hasil iterasiprintf("Iterasi ke %2d : ",iter);for(i=0;i<n;i++) printf(" %10.7f",x_1[i]);printf("\n");for(i=0;i<n;i++) x[i]=x_1[i];}

printf("hasil akhir\n");for(i=0;i<n;i++) printf("x[%2d]=%g\n",i,x[i]);getch(); }

2. METODA ITERASI GAUSS SEIDEL// iterasi Gauss Seidel abdul#include<stdio.h>#include<math.h>#include<conio.h>float A[5][5]={{-3, -1, 1},

{3, 6, 2}, {3, 3, 7}};

float B[5]={1, 0, 4};float x[5];float xb;float Eps = 0.00001;int i, j, n=3, TRUE, iter;void main(){

TRUE = 0;iter = 0;// inisialisasifor (i=0;i<n;i++) x[i]=0;// lakukan iterasi sampai konvergenwhile (!TRUE){

iter = iter + 1;TRUE = 1; // asumsi sudah konvergenprintf("Iterasi ke %2d : ",iter);for(i=0;i<n;i++){

xb=B[i];for(j=0;j<n;j++); if(i!=j) xb=xb-A[i][j]*x[j];xb=xb/A[i][i];if(fabs(xb-x[i])>Eps) TRUE = 0; // belum konvergen

6

Page 7: Listing Program

x[i]=xb;printf(" %10.7f", xb);

}printf("\n");

}// tulis hasil iterasiprintf("hasil akhir\n");for(i=0;i<n;i++) printf("x[%2d]=%g\n",i,x[i]);

getch(); }

3. METODA SOR// iterasi SOR abdul#include<stdio.h>#include<math.h>#include<conio.h>float A[5][5]={{-3, -1, 1},

{3, 6, 2}, {3, 3, 7}};

float B[5]={1, 0, 4};float x[5];float xb;float xc;float w=1;float Eps = 0.005;int i, j, n=3, TRUE, iter;void main(){

TRUE = 0;iter = 0;// inisialisasifor (i=0;i<n;i++) x[i]=0;// lakukan iterasi sampai konvergenwhile (!TRUE){

iter = iter + 1;TRUE = 1; // asumsi sudah konvergenprintf("Iterasi ke %2d : ",iter);for(i=0;i<n;i++){

xb=B[i];for(j=0;j<n;j++); if(i!=j) xb=xb-A[i][j]*x[j];

xc=(1-w)*(xb-A[i][j]*x[j]); xc=xb;

xc=xc/A[i][i];if(fabs(xc-x[i])>Eps) TRUE = 0; // belum konvergenx[i]=xc;printf(" %10.7f", xc);

}printf("\n");

}// tulis hasil iterasiprintf("hasil akhir\n");for(i=0;i<n;i++) printf("x[%2d]=%g\n",i,x[i]);

getch(); }

7

Page 8: Listing Program

2. LISTING PROGRAM /*interpolasi linier*/#include <stdio.h>#include <math.h>main (){float x0, x1;float y0, y1;float x;float P;float E;printf("\n");printf("INTERPOLASI LINIER");printf("input nilai x0 = ");scanf("%f", &x0);printf("input nilai f(0) = ");scanf("%f", &y0);printf("input nilai x1 = ");scanf("%f", &x1);printf("input nilai f(1) = ");scanf("%f", &y1);printf("input nilai terkaannya= ");scanf("%f", &x);P = y0 +((x-x0)*(y1-y0)/(x1-x0));printf("Hasil perhitungan P(x) = %f\n",P);E = (abs(x-P));printf("error= %f\n", E);return 0; }

/*interpolasi kuadratik*/#include <stdio.h>#include <math.h>main (){float x0, x1, x2, x3;float y0, y1, y2, y3;float x;float P, L1, L2, L;float E;printf("\n");printf("INTERPOLASI KUADRATIK");printf("input nilai x0 = ");scanf("%f", &x0);printf("input nilai f(0) = ");scanf("%f", &y0);printf("input nilai x1 = ");scanf("%f", &x1);printf("input nilai f(1) = ");scanf("%f", &y1);printf("input nilai x2 = ");scanf("%f", &x2);printf("input nilai f(2) = ");scanf("%f", &y2);printf("input nilai terkaan= ");scanf("%f", &x);L1 = ((y2-y1)/(x2-x1));L2 = ((y3-y2)/(x3-x2));L = ((L2-L1)/(x3-x1));P = y0 + ((x-x1) * L1 + (x+x0) * (x-x1) * L);printf("Hasil perhitungan P(x) = %g\n",P);E = (abs(x-P));printf("error= %f\n", E);

8

Page 9: Listing Program

return 0; }

//program interpolasi polinom//dengan fungsi cos (x)#include <stdio.h> #include <math.h> float x,x0,y0,x1,y1,x2,y2; float f1,f2,f3,p,l,e; void main () { printf ("*************************************************" printf ("input nilai x[0],y[0]=");scanf("%f,%f",&x0,&y0); printf ("input nilai x[1],y[1]=");scanf("%f,%f",&x1,&y1); printf ("input nilai x[2],y[2]=");scanf("%f,%f",&x2,&y2); printf ("masukan niali x =");scanf ("%f",&x)*; printf ("************************************************); f1=(y1-y0)/(x1-x0); f2=(y2-y1)/(y2-y1); f3=(f2-f1)/(x2-x0); p=y0+(x-x0)*f1+(x-x0)*(x-x1)*f3; printf ("hasil nilai polinom p(x)=%g\n",p); l=0.900; e=l-p; printf ("nilai errornya adalah= %g\n",e); }

//program metodlagrange#include<stdio.h>#include<math.h>int n;float x[20],y[20],h,X;float p;float lagrange (float X){ float sum,product; int i,j; sum=0; for (i=0;i<n;i++) { product=y[i]; for(j=0;j<n;j++) if (i!=j) product=product*(X-x[j])/(x[i]-x[j]); sum=sum+product; } return sum; } void main () { int i; printf ("jumlah data n="); scanf ("%d",&n); printf("\n"); for (i=0;i<n;i++) { printf ("input data x[%2d],y[%2d]=",i,i); scanf("%f,%f",&x[i],&y[i]); } printf("\ninput x=");scanf ("%f",&X);

9

Page 10: Listing Program

p = lagrange(X); printf("hasil perhitungan p(x)=%g\n",p); }

1. METODA LEAST SQUARE Regresi linier

// Program abdul// Metoda Least Square regresi linear#include<stdio.h>#include<math.h>#include<conio.h>float x[12], y[12], xp, yp;int n;float sum_x, sum_y, sum_x2, sum_xy, x_rata, y_rata, at, bt;int i;void main(){

printf("masukkan jumlah data : "); scanf("%d", &n); for(i=0; i<n; i++) {

printf("x[%2d] = ",i+1);scanf("%f",&x[i]);printf("y[%2d] = ",i+1);scanf("%f",&y[i]);printf("\n");

}sum_x=0; sum_y=0; sum_x2=0; sum_xy=0; for(i=0; i<n; i++) {

sum_x =sum_x + x[i];sum_y =sum_y + y[i];sum_x2 =sum_x2 + x[i]*x[i];sum_xy =sum_xy + x[i]*y[i];

}x_rata=sum_x / n;y_rata=sum_y / n;at = ((n*sum_xy) - (sum_x*sum_y)) / ((n*sum_x2) - (sum_x*sum_x));bt = (y_rata - at*x_rata);

printf("Fungsi regresi linier :\n");printf("y' = %10.5f + %10.5f x \n", at, bt);printf("Masukkan harga x : ");scanf("%f",&xp);yp = atx + bt;printf("Nilai y' = %10.5f \n", yp);

getch (); }

2. METODA LEAST SQUARE Regresi Kuadratik

// Program abdul least square// regresi polinomial#include<stdio.h>#include<math.h>#include<conio.h>float x[10], y[10], xp, yp;int n, M;float sum_x[20], sum[10][10], a[10];int i, j;void simultan(int n, float A[10][10], float b[10]){ float G[10][11], dummy, factor; int i, j, k;

10

Page 11: Listing Program

// bentuk matriks gabungan for(i=0; i<n; i++) for(j=0; j<n; j++) G[i][j] = A[i][j]; for(j=0; j<n; j++) G[j][n]=b[j];// proses eliminasi for(i=0; i<n; i++) { // pivoting

dummy = fabs(G[i][i]); k = i; for(j=i+1; j<n; j++)

if(fabs(G[j][i])>dummy);{ dummy = fabs(G[j][i]); k = j;}

if (k!=i)for(j=i; j<=n; j++){ dummy = G[k][j]; G[k][j]= G[i][j]; G[i][j]= dummy;}

for (j=0; j<n; j++){ if (i!=j) { factor = G[j][i]/G[i][i]; for (k=i;k<=n;k++) G[j][k]= G[j][k]-factor*G[i][k]; }}

} for(i=0; i<n; i++) b[i] = G[i][n]/G[i][i];}void main(){ printf("Masukkan Jumlah data = "); scanf("%d", &n); printf("\n"); for(i=0; i<n; i++) { printf("x[%2d] = ", i+1); scanf("%f", &x[i]); printf("y[%2d] = ", i+1); scanf("%f", &y[i]); printf("\n"); }printf("Orde polinomial= "); scanf("%d", &M);for (i=0; i<=2*M; i++) sum_x[i]=0;sum_x[0]= n;for(i=0; i<n; i++) for(j=1; j<=2*M; j++) sum_x[j] = sum_x[j] + pow(x[i], j);for(i=0; i<=M; i++) for(j=0; j<=M; j++) sum[i][j]= sum_x[i+j];a[0]=0; for(j=0; j<n; j++) a[0] = a[0] + y[j];for(i=1; i<=M; i++) { a[i] = 0; for(j=0; j<n; j++) a[i] = a[i] + pow(x[j],i)*y[j]; }simultan (M+1, sum, a);

11

Page 12: Listing Program

printf("Koefisien Fungsi regresi polinomial = \n"); for(i=0; i<=M; i++) printf("a[%2d] = %10.5f\n",i,a[i]); printf("\n");printf("Masukkn harga x = "); scanf("%f", &xp); yp = a[0]; for(i=1; i<=M; i++) yp = yp + a[i]*pow(xp,i);printf("Nilai y = %10.5f\n", yp); getch (); }

1. Fungsi tg (x)// Program Abdul// Differensiasi numerik fungsi tg(x)#include<stdio.h>#include<math.h>#include<conio.h>f(float x){ float a; a = sin(x)/cos(x); return a;}df(float x){ float b; b= 1/(cos(x)*cos(x)); return b;}d2f(float x){ float c; c= 2*sin(x)/((cos(x)*cos(x)*cos(x))); return c;}main(){float f1, f2, f3;float er1, er2, er3, x, h; printf("Differensiasi Numerik :\n"); printf("masukkan x: "); scanf("%f", &x); printf("masukkan h: "); scanf("%f", &h);f1 = ((f(x+h)-f(x))/h);er1= (h/2)*d2f(x);f2= ((f(x)-f(x-h))/h);er2= -(h/2)*d2f(x);f3 = ((f(x+h)-f(x-h))/2*h);er3= -(h/6)*d2f(x);printf("hasil dari selisih maju = %f\t", f1);printf("dengan error = %f\n", er1);printf("hasil dari selisih mundur = %f\t", f2);printf("dengan error = %f\n", er2);printf("hasil dari selisih pusat = %f\t", f3);printf("dengan error = %f\n", er3); getch (); return 0; }

12

Page 13: Listing Program

2. Nilai shear Strees// Program abdul// Analisis Numerik Differensial dengan Taylor#include<stdio.h>#include<conio.h>float f[5], h, f1, f2;int i, pilih;void main(){ printf("Differensial dengan Rumusan Taylor\n"); printf("Pilih [1] Beda ke depan, [2] Beda ke Belakang, [3] Beda tengah :"); scanf("%d", &pilih); printf("\n");if (pilih == 1) {// rumusan Taylor dengan beda kedepan printf("Input h = "); scanf("%f", &h); for(i=0; i<4; i++) {

printf("Input f[%2d] = ",i);scanf("%f", &f[i]);

} printf("\n");

f1 = (-f[2] + 4*f[1] - 3*f[0]) / (2*h);f2 = (-f[3] + 4*f[2] - 5*f[1] + 2*f[0]) / (h*h);

printf("Turunan pertama f'(x) = %g\n",f1);printf("Turunan kedua f''(x) = %g\n", f2); }else if(pilih == 2) {// Rumusan Taylor dengan beda kedepan printf("Input h = "); scanf("%f", &h); for(i=0; i<4; i++) {

printf("Input f[%2d] = ",i-3);scanf("%f", &f[i]);

} printf("\n");

f1 = (3*f[3] - 4*f[2] + f[1]) / (2*h);f2 = (2*f[3] - 5*f[2] + 4*f[1] - f[0]) / (h*h);

printf("Turunan pertama f'(x) = %g\n",f1);printf("Turunan kedua f''(x) = %g\n", f2); }else if(pilih == 3) {// Rumusan tylor dengan beda tengah printf("Input h = "); scanf("%f", &h); for(i=0; i<5; i++) {

printf("Input f[%2d] = ",i-2);scanf("%f", &f[i]);

} printf("\n");

f1 = (-f[4] + 8*f[3] - 8*f[1] + f[0]) / (12*h);f2 = (-f[4] + 16*f[3] - 30*f[2] + 16*f[1] - f[0]) / (12*h*h);

13

Page 14: Listing Program

printf("Turunan pertama f'(x) = %g\n",f1);printf("Turunan kedua f''(x) = %g\n", f2); }else printf("Pilihan salah"); getch ();}

/* TUGAS AKHIR METNUM 6 FUNGSI (tan x) RACHMAT SYAWAL IBRAHIM 140310070040 */#include <stdio.h>#include <math.h>f (float x){

float a;a= sin(x)/cos(x);return a;

}df1(float x){

float b;b= 1/(cos(x)*cos(x));

return b;}df2(float x){

float c;c= 2*(1/(cos(x)*cos(x)*cos(x)))*sin(x);return c;

}main(){

float forw,back,cen;float ef, eb, ec;float h, x;printf("input h: "); scanf("%f",&h);printf("input x0: "); scanf("%f",&x);forw = (f(x+h)-f(x))/h;ef= (h/2)*df2(x);back= (f(x)-f(x-h))/h;eb= -(h/2)*df2(x);cen = (f(x+h)-f(x-h))/2*h;ec= -(h/6)*df2(x);printf("hasil dari forward= %f\n",forw);printf("error dari forward= %f\n",ef);printf("hasil dari backward= %f\n",back);printf("error dari backward= %f\n",eb);printf("hasil dari central= %f\n",cen);printf("error dari central= %f\n",ec);

return 0;}

/* TUGAS AKHIR METNUM 6 FUNGSI (sin x) RACHMAT SYAWAL IBRAHIM 140310070040 */#include <stdio.h>

14

Page 15: Listing Program

#include <math.h>f (float x){

float a;a= sin(x);return a;

}df1(float x){

float b;b= cos(x);return b;

}df2(float x){

float c;c= -sin(x);return c;

}main(){

float forw,back,cen;float ef, eb, ec;float h, x;printf("masukkan nilai h: ");scanf("%f",&h);printf("masukkan nilai x0: ");scanf("%f",&x);forw = (f(x+h)-f(x))/h;ef= (h/2)*df2(x);back= (f(x)-f(x-h))/h;eb= -(h/2)*df2(x);centr = (f(x+h)-f(x-h))/2*h;ec= -(h/6)*df2(x);printf("hasil dari forward= %f\n",forw);printf("error dari forward= %f\n",ef);printf("hasil dari backward= %f\n",back);printf("error dari backward= %f\n",eb);printf("hasil dari central= %f\n",cen);printf("error dari central= %f\n",ec);

return 0;}

1. LISTING PROGRAM /*metoda trapesium*/#include <stdio.h>float F (float x){return x*x*x*x;}main (){float a, b, h, n, j, sum=0, I;printf("input nilai batas a: ");scanf("%f",&a);printf("input nilai batas b: ");scanf("%f",&b);printf("input nilai n: ");scanf("%f",&n);

h= (b-a)/n;sum=0;

15

Page 16: Listing Program

for (j=1;j<n;j++)sum = sum + 2 * F(a + j * h);I= (h/2) * (F(a)+ sum + F(b));printf("Integral dgn metoda trapesium: %g\n",I);return 0;}

/*simpson 1/3*/#include <stdio.h>#include <math.h>float a,b,h;float sum,I;int n,i,TRUE = 1;

float F(float x){return x*sin(x);}

main(){printf("Input batas a,b = ");scanf("%f,%f",&a,&b);

while (TRUE){printf("Input jumlah segmen n = ");scanf("%d",&n);if(fmod(n,2) == 0) break;printf("Jumlah segmen harus genap !\n");}

printf("\n");h = (b - a)/n;sum = F(a) + 4 * F(a + h);for(i=2;i<n;i+=2) sum = sum + 2*F(a + i*h) + 4*F(a + (i+1)*h);I = h/3 * (sum + F(b));printf("Nilai Integral = %g\n",I);return 0;}

/*simpson 3/8*/#include <stdio.h>#include <math.h>float a,b,h;float sum,I;int n,i,TRUE = 1;float F(float x){return x*sin(x);}main(){printf("Input batas a,b = "); scanf("%f,%f",&a,&b);while (TRUE)

{printf("Input jumlah segmen n = ");scanf("%d",&n);if(fmod(n,3) == 0) break;printf("Jumlah segmen harus kelipatan 3 !\n");}printf("\n");

h = (b - a)/n;

16

Page 17: Listing Program

sum = F(a) + 3 * F(a + h) + 3 * F(a + 2*h);for(i=3;i<n;i+=3)sum = sum + 2*F(a + i*h) + 3*F(a + (i+1)*h) + 3*F(a + (i+2)*h);I = 3*h/8 * (sum + F(b));printf("Nilai Integral = %g\n",I);return 0; }

LISTING PROGGRAM1. Metoda Euler y’=-20y+7exp(-0.5t)

// Program Abdul Euler#include<stdio.h>#include<math.h>#include<conio.h>float dy(float y, float t){ float a; a = (-20*y) + (7*exp(-0.5*t)); return a;}main (){ int i; float h, y0, n, t0; float yr; printf("Masukkan nilai t =");scanf("%f\n",&t0); printf("Masukkan nilai h =");scanf("%f\n",&h); printf("Masukkan nilai y(0) =");scanf("%f\n",&y0); printf("Masukkan banyaknya ierasi n =");scanf("%f\n",&n); i=-1; while(t0<=n) { yr = y0 + (h*dy(y0,t0)); y0 = yr; t0 = t0+h; i++; printf("Hasil dari Euler adalah = %d %f \n",i,yr); } getch (); return 0; }

2. Meoda Euler y’=1-ty// Program Abdul Euler2#include<stdio.h>#include<math.h>#include<conio.h>float dy(float y, float t){ float b; b = 1-(t*y); return b;}main (){ int i; float h, y0, n, t0; float yr;

17

Page 18: Listing Program

printf("Masukkan nilai t0 =");scanf("%f\n",&t0); printf("Masukkan nilai h =");scanf("%f\n",&h); printf("Masukkan nilai y(0) =");scanf("%f\n",&y0); printf("Masukkan banyaknya ierasi n =");scanf("%f\n",&n); i=-1; while(t0<=n) { yr = y0 + (h*dy(y0,t0)); y0 = yr; t0 = t0+h; i++; printf("Hasil dari Euler adalah = %d %f \n",i,yr); } getch (); return 0; }

3. Metoda Euler y’=e-t-3y// Program Abdul Euler3#include<stdio.h>#include<math.h>#include<conio.h>float dy(float y, float t){ float c; c = (exp(-t))-3*y; return c;}main (){ int i; float h, y0, n, t0; float yr; printf("Masukkan nilai t =");scanf("%f\n",&t0); printf("Masukkan nilai h =");scanf("%f\n",&h); printf("Masukkan nilai y(0) =");scanf("%f\n",&y0); printf("Masukkan banyaknya ierasi n =");scanf("%f\n",&n); i=-1; while(t0<=n) { yr = y0 + (h*dy(y0,t0)); y0 = yr; t0 = t0+h; i++; printf("Hasil dari Euler adalah = %d %f \n",i,yr); } getch (); return 0; }

4. Runge-Kutta y’=-20y+7exp(-0.5t)// Program Abdul Runge-Kutta#include<stdio.h>#include<math.h>#include<conio.h>float dy(float y, float t){ float a;

18

Page 19: Listing Program

a = (-20*y)+(7*exp(-0.5*t)); return a;}main (){ int i; float h, y0, n, t0; float yr, k1, k2, k3, k4; printf("Masukkan nilai t =");scanf("%f\n",&t0); printf("Masukkan nilai h =");scanf("%f\n",&h); printf("Masukkan nilai y(0) =");scanf("%f\n",&y0); printf("Masukkan banyaknya ierasi n =");scanf("%f\n",&n); i=-1; while(t0<=n) { k1 = h*dy(y0,t0); k2 = h*dy(y0+(k1 / 2), t0+(h/2)); k3 = h*dy(y0+(k2 / 2), t0+(h/2)); k4 = h*dy(y0+k3, t0+h); yr = y0 + (i/6*(k1+(2*k2)+(2*k3)+k4)); y0 = yr; t0 = t0+h; i++; printf("Hasil dari Runge-Kutta adalah = %d %f \n",i,yr); } getch (); return 0; }

5. Runge kutta y’=1-ty// Program Abdul Runge-Kutta2#include<stdio.h>#include<math.h>#include<conio.h>float dy(float y, float t){ float b; b = 1-(t*y); return b;}main (){ int i; float h, y0, n, t0; float yr, k1, k2, k3, k4; printf("Masukkan nilai t =");scanf("%f\n",&t0); printf("Masukkan nilai h =");scanf("%f\n",&h); printf("Masukkan nilai y(0) =");scanf("%f\n",&y0); printf("Masukkan banyaknya ierasi n =");scanf("%f\n",&n); i=-1; while(t0<=n) { k1 = h*dy(y0,t0); k2 = h*dy(y0+(k1 / 2), t0+(h/2)); k3 = h*dy(y0+(k2 / 2), t0+(h/2)); k4 = h*dy(y0+k3, t0+h); yr = y0 + (i/6*(k1+(2*k2)+(2*k3)+k4));

19

Page 20: Listing Program

y0 = yr; t0 = t0+h; i++; printf("Hasil dari Runge-Kutta adalah = %d %f \n",i,yr); } getch (); return 0; }

6. Runge kutta y’=e-t-3y// Program Abdul Runge-Kuta3#include<stdio.h>#include<math.h>#include<conio.h>float dy(float y, float t){ float c; c = (exp(-t))-3*y; return c;}main (){ int i; float h, y0, n, t0; float yr, k1, k2, k3, k4; printf("Masukkan nilai t =");scanf("%f\n",&t0); printf("Masukkan nilai h =");scanf("%f\n",&h); printf("Masukkan nilai y(0) =");scanf("%f\n",&y0); printf("Masukkan banyaknya ierasi n =");scanf("%f\n",&n); i=-1; while(t0<=n) { k1 = h*dy(y0,t0); k2 = h*dy(y0+(k1 / 2), t0+(h/2)); k3 = h*dy(y0+(k2 / 2), t0+(h/2)); k4 = h*dy(y0+k3, t0+h); yr = y0 + (i/6*(k1+(2*k2)+(2*k3)+k4)); y0 = yr; t0 = t0+h; i++; printf("Hasil dari Runge-Kutta adalah = %d %f \n",i,yr); } getch (); return 0; }

20