Upload
ogetsincan
View
81
Download
5
Embed Size (px)
Citation preview
TUGAS
METODE NUMERIK
DIKERJAKAN OLEH :
Nama : Sigit Rossandi Utomo
Nim : 360763013
Jurusan : Teknik Informatika
SEKOLAH TINGGI MANAJEMEN INFORMATIKA DAN KOMPUTER
INDONESIA MANDIRI
BANDUNG 2010
ALAMAT KAMPUS STMIK-IM Jl. Jakarta No. 79 Bandung 40272.
Telp. 022-7272672, 022-7208180. Fax. 022-7271693.
E-mail: [email protected]
Metode Setengah Selang
#include <stdio.h>#include <math.h>
double mutlak(double xk){if(xk<0) return xk * -1;else return xk;
}
double hasilFungsi(double x){return 5*x*x*x - 3*x*x*x*x*x;
}
void setengahSelang(double epsilon, double x1, double x2){
double xk;int k = 1;xk = (x1+x2)/2;
printf("Metode Setengah Selang\n\n");
while(mutlak(xk)>epsilon && k<=50 && hasilFungsi(xk) != 0){ printf("%d\t%lf\n", k, xk);
if( hasilFungsi(x1) * hasilFungsi(xk) < 0 ) x2 = xk;
else x1 = xk;k = k + 1;xk = (x1+x2)/2;
} }
main(){setengahSelang(0.0001,0.0,-2.5);
getch();return 0;
}
Hasil Run f(x) = 5x3 - 3x5
1 -1.2500002 -1.8750003 -1.5625004 -1.4062505 -1.3281256 -1.2890637 -1.3085948 -1.2988289 -1.29394510 -1.29150411 -1.29028312 -1.29089413 -1.29119914 -1.29104615 -1.29097016 -1.29100817 -1.29098918 -1.29099819 -1.29099420 -1.29099621 -1.29099522 -1.29099423 -1.29099524 -1.29099425 -1.29099526 -1.290994
Metode Setengah Selang
#include <stdio.h>#include <math.h>
double mutlak(double xk){if(xk<0) return xk * -1;else return xk;
}
double hasilFungsi(double x){return exp(x) - 5*x*x;
}
void setengahSelang(double epsilon, double x1, double x2){
double xk;int k = 1;xk = (x1+x2)/2;
printf("Metode Setengah Selang\n\n");
while(mutlak(xk)>epsilon && k<=50 && hasilFungsi(xk) != 0){ printf("%d\t%lf\n", k, xk);
if( hasilFungsi(x1) * hasilFungsi(xk) < 0 ) x2 = xk;
else x1 = xk;k = k + 1;xk = (x1+x2)/2;
} }
main(){setengahSelang(0.0001,0.0,-2.5);
getch();return 0;
}
Hasil Run f(x) = ex - 5x2
1 -1.2500002 -0.6250003 -0.3125004 -0.4687505 -0.3906256 -0.3515637 -0.3710948 -0.3808599 -0.37597710 -0.37353511 -0.37231412 -0.37170413 -0.37139914 -0.37155215 -0.37147516 -0.37143717 -0.37141818 -0.37140819 -0.37141320 -0.37141621 -0.37141722 -0.37141723 -0.371418
Metode REGULA FALSI
#include <stdio.h>#include <math.h>
double mutlak(double xk){if(xk<0) return xk * -1;else return xk;
}
double hasilFungsi(double x){return 5*x*x*x - 3*x*x*x*x*x;
}
void regulaFalsi(double epsilon, double a, double b){
double x1,x2,x3;int k = 2;x1 = a;x2 = b;
printf("Metode REGULA FALSI\n\n");
while(mutlak(x2 - x1)>epsilon && k<=50 && hasilFungsi(x2) != 0){ printf("%d\t%lf\n", k, x2); x3 = (a*hasilFungsi(b) - b*hasilFungsi(a)) / (hasilFungsi(b) - hasilFungsi(a));
if( hasilFungsi(x1) * hasilFungsi(x3) < 0 ) b = x3;
else a = x3;k = k + 1;x1 = x2;x2 = x3;
} }
main(){ regulaFalsi(0.0001,0.0,-2.5);
getch();return 0;
}
Hasil Run f(x) = 5x3 - 3x5
2 -2.500000
Metode REGULA FALSI
#include <stdio.h>#include <math.h>
double mutlak(double xk){if(xk<0) return xk * -1;else return xk;
}
double hasilFungsi(double x){return exp(x) - 5*x*x;
}
void regulaFalsi(double epsilon, double a, double b){
double x1,x2,x3;int k = 2;x1 = a;x2 = b;
printf("Metode REGULA FALSI\n\n");
while(mutlak(x2 - x1)>epsilon && k<=50 && hasilFungsi(x2) != 0){ printf("%d\t%lf\n", k, x2); x3 = (a*hasilFungsi(b) - b*hasilFungsi(a)) / (hasilFungsi(b) - hasilFungsi(a));
if( hasilFungsi(x1) * hasilFungsi(x3) < 0 ) b = x3;
else a = x3;k = k + 1;x1 = x2;x2 = x3;
} }
main(){ regulaFalsi(0.0001,0.0,-2.5);
getch();return 0;
}
Hasil Run f(x) = ex - 5x2
2 -2.5000003 -0.0777174 -0.1453345 -0.5230276 -0.3163667 -0.3633978 -0.3719179 -0.371387
Metode NEWTON RAPHSON
#include <stdio.h>#include <math.h>
double mutlak(double xk){if(xk<0) return xk * -1;else return xk;
}
// fungsi aslidouble hasilFungsi(double x){
return 5*x*x*x - 3*x*x*x*x*x;}
// fungsi turunannyadouble hasilFungsi1(double x){
return 15*x*x - 15*x*x*x*x;}
void newtonRaphson(double epsilon, double x1){double x2,xk;int k = 2;x2 = x1 - hasilFungsi(x1) /
hasilFungsi1(x1);
printf("Metode NEWTON RAPHSON \n\n");
while(mutlak(x2 - x1)>epsilon && k<=50 && hasilFungsi(x2) != 0){ printf("%d\t%lf\n", k, x2); xk = x2 - hasilFungsi(x2) / hasilFungsi1(x2); k = k + 1; x1 = x2; x2 = xk; } } main(){ newtonRaphson(0.0001,-0.5);
getch();return 0;
}
Hasil Run f(x) = 5x3 - 3x5
2 -0.3111113 -0.2029624 -0.1341455 -0.0891036 -0.0593077 -0.0395108 -0.0263329 -0.01755210 -0.01170111 -0.00780012 -0.00520013 -0.00346714 -0.00231115 -0.00154116 -0.00102717 -0.00068518 -0.00045719 -0.00030420 -0.000203
Metode NEWTON RAPHSON
#include <stdio.h>#include <math.h>
double mutlak(double xk){if(xk<0) return xk * -1;else return xk;
}
// fungsi aslidouble hasilFungsi(double x){
return exp(x) - 5*x*x;}
// fungsi turunannyadouble hasilFungsi1(double x){
return exp(x) - 10*x;}
void newtonRaphson(double epsilon, double x1){double x2,xk;int k = 2;x2 = x1 - hasilFungsi(x1) /
hasilFungsi1(x1);
printf("Metode NEWTON RAPHSON \n\n");
while(mutlak(x2 - x1)>epsilon && k<=50 && hasilFungsi(x2) != 0){ printf("%d\t%lf\n", k, x2); xk = x2 - hasilFungsi(x2) / hasilFungsi1(x2); k = k + 1; x1 = x2; x2 = xk; } } main(){ newtonRaphson(0.0001,-0.5);
getch();return 0;
}
Hasil Run f(x) = ex - 5x2
2 -0.3852293 -0.3716144 -0.371418
Metode JACOBI
#include <stdio.h>#include <math.h>
void jacobi(double x1,doub le x2,double x3){int a[3][3] = {{4,-1,1},{4,-8,1},{-2,1,5}};
int b[3] = {7,-21,15};int k = 1;double x,y,z;printf("Metode JACOBI\n\n");
while(k<=50){ x = x1;y = x2;z = x3; printf("%d\t%lf\t%lf\t%lf\n", k,x,y,z); x1 = (b[0] - a[0][1]*y - a[0][2]*z) / a[0][0]; x2 = (b[1] - a[1][0]*x - a[1][2]*z) / a[1][1]; x3 = (b[2] - a[2][0]*x - a[2][1]*y) / a[2][2]; k++; } }
main(){ jacobi(1.0,1.0,1.0);
getch();return 0;
}
Hasil Run JACOBI
1 1.000000 1.000000 1.0000002 1.750000 3.250000 3.2000003 1.762500 3.900000 3.0500004 1.962500 3.887500 2.9250005 1.990625 3.971875 3.0075006 1.991094 3.996250 3.0018757 1.998594 3.995781 2.9971878 1.999648 3.998945 3.0002819 1.999666 3.999859 3.00007010 1.999947 3.999842 2.99989511 1.999987 3.999960 3.00001112 1.999987 3.999995 3.00000313 1.999998 3.999994 2.99999614 2.000000 3.999999 3.00000015 2.000000 4.000000 3.000000
TRAPEZOID RULES Hasil Run
#include <iostream.h>#include <math.h>double f(double x){ return 1/(1+x);}main(){ double a, b, h, x, jumlah, integral; int N; a=0; b=1; N=10; h=(b-a)/N; jumlah=0; for ( int i=1; i<=N-1; i++) { x=a+i*h; jumlah = jumlah+2*f(x); } integral = h*(f(a)+jumlah+f(b))/2; cout << "Integralnya adalah " << integral << endl; getch();}
Integralnya adalah 0.693771
EULER n xn yn err
#include <iostream.h>#include <iomanip.h>#include <math.h>#include <stdio.h>#include <conio.h>double f(double x, double y){ return x+y;}main(){ double x, y, h; int n; x=0.0; y=1.0; h=0.2; n=0; cout<<" n xn yn err"<<endl; do { cout<<setw (4)<<n<< setiosflags ( ios ::showpoint |ios::fixed) <<setprecision(8)<<setw(15)<<x <<setw(15)<<y<<setw(15)<<(y-(2*exp(x)-x-1))<<endl; x=x+h; y=y+h*f(x,y); n++; } while (x<=1.0001); getch();}
0 0.00000000 1.00000000 0.00000000 1 0.20000000 1.24000000 -0.00280552 2 0.40000000 1.56800000 -0.01564940 3 0.60000000 2.00160000 -0.04263760 4 0.80000000 2.56192000 -0.08916186 5 1.00000000 3.27430400 -0.16225966
SIMPSON Hasil Run
#include <iostream.h>#include <math.h>#include <stdio.h>#include <conio.h>double f(double x){ return 1/(1+x);}main(){ double a, b, h, x, jumlah, integral; int N; a=0; b=1; N=10; h=(b-a)/N; jumlah = 0; for (int i=1; i<=N-1; i++) { x=a+i*h; if (i%2==1) { jumlah=jumlah+4*f(x); } else { jumlah=jumlah+2*f(x); } } integral=h*(f(a)+jumlah+f(b))/3; cout<<"integralnya adalah "<<integral<<endl; getch();}
integralnya adalah 0.69315
Polinom Newton Masukan banyaknya data yang akan di input : 4
#include<stdio.h>
double polinom(double xx,double x[100],int n){ if(n){ return (xx-x[n-1])*polinom(xx,x,n-1); } else return 1;}
double hasil(double xx,double y[100][100],double x[100],int n){ if(n){ return y[0][n-1]*polinom(xx,x,n-1)+hasil(xx,y,x,n-1); } else return 0; }
main(){ int i,n,j; printf("Masukan banyaknya data yang akan di input : "); scanf("%d", &n); double x[n],y[n][n],xx; printf("\nx\tf(x)\n"); for(i=0;i<n;i++){ scanf("%lf %lf", &x[i],&y[i][0]); } for(j=1;j<n;j++){ for(i=0;i<n;i++){ y[i][j] = (y[i+1][j-1]-y[i][j-1])/(x[i+j]-x[i]); } } printf("\nMasukan data yang ingin dicari : "); scanf("%lf", &xx); printf("\nf(%lf) = %lf\n", xx,hasil(xx,y,x,n)); getch(); return 0;}
x f(x)8 2.0794429 2.1972259.5 2.25129211 2.397895
Masukan data yang ingin dicari : 9.2
f(9.200000) = 2.219208