Sie sind auf Seite 1von 12

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: Info@stmik-im.ac.id
Metode Setengah Selang Hasil Run f(x) = 5x3 - 3x5

#include <stdio.h> 1 -1.250000


#include <math.h> 2 -1.875000
3 -1.562500
double mutlak(double xk){ 4 -1.406250
if(xk<0) return xk * -1; 5 -1.328125
else return xk; 6 -1.289063
} 7 -1.308594
8 -1.298828
double hasilFungsi(double x){ 9 -1.293945
return 5*x*x*x - 3*x*x*x*x*x; 10 -1.291504
} 11 -1.290283
12 -1.290894
void setengahSelang(double epsilon, double x1, 13 -1.291199
double x2){ 14 -1.291046
double xk; 15 -1.290970
int k = 1; 16 -1.291008
xk = (x1+x2)/2; 17 -1.290989
18 -1.290998
printf("Metode Setengah Selang\n\n"); 19 -1.290994
20 -1.290996
while(mutlak(xk)>epsilon && k<=50 && 21 -1.290995
hasilFungsi(xk) != 0){ 22 -1.290994
printf("%d\t%lf\n", k, xk); 23 -1.290995
if( hasilFungsi(x1) * hasilFungsi(xk) 24 -1.290994
< 0 ) x2 = xk; 25 -1.290995
else x1 = xk; 26 -1.290994
k = k + 1;
xk = (x1+x2)/2;
}
}

main(){
setengahSelang(0.0001,0.0,-2.5);

getch();
return 0;
}
Metode Setengah Selang Hasil Run f(x) = ex - 5x2

#include <stdio.h> 1 -1.250000


#include <math.h> 2 -0.625000
3 -0.312500
double mutlak(double xk){ 4 -0.468750
if(xk<0) return xk * -1; 5 -0.390625
else return xk; 6 -0.351563
} 7 -0.371094
8 -0.380859
double hasilFungsi(double x){ 9 -0.375977
return exp(x) - 5*x*x; 10 -0.373535
} 11 -0.372314
12 -0.371704
void setengahSelang(double epsilon, double x1, 13 -0.371399
double x2){ 14 -0.371552
double xk; 15 -0.371475
int k = 1; 16 -0.371437
xk = (x1+x2)/2; 17 -0.371418
18 -0.371408
printf("Metode Setengah Selang\n\n"); 19 -0.371413
20 -0.371416
while(mutlak(xk)>epsilon && k<=50 && 21 -0.371417
hasilFungsi(xk) != 0){ 22 -0.371417
printf("%d\t%lf\n", k, xk); 23 -0.371418
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;
}
Metode REGULA FALSI Hasil Run f(x) = 5x3 - 3x5

#include <stdio.h> 2 -2.500000


#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;
}
Metode REGULA FALSI Hasil Run f(x) = ex - 5x2

#include <stdio.h> 2 -2.500000


#include <math.h> 3 -0.077717
4 -0.145334
double mutlak(double xk){ 5 -0.523027
if(xk<0) return xk * -1; 6 -0.316366
else return xk; 7 -0.363397
} 8 -0.371917
9 -0.371387
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;
}
Metode NEWTON RAPHSON Hasil Run f(x) = 5x3 - 3x5

#include <stdio.h> 2 -0.311111


#include <math.h> 3 -0.202962
4 -0.134145
double mutlak(double xk){ 5 -0.089103
if(xk<0) return xk * -1; 6 -0.059307
else return xk; 7 -0.039510
} 8 -0.026332
9 -0.017552
// fungsi asli 10 -0.011701
double hasilFungsi(double x){ 11 -0.007800
return 5*x*x*x - 3*x*x*x*x*x; 12 -0.005200
} 13 -0.003467
14 -0.002311
// fungsi turunannya 15 -0.001541
double hasilFungsi1(double x){ 16 -0.001027
return 15*x*x - 15*x*x*x*x; 17 -0.000685
} 18 -0.000457
19 -0.000304
void newtonRaphson(double epsilon, double x1){ 20 -0.000203
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;
}
Metode NEWTON RAPHSON Hasil Run f(x) = ex - 5x2

#include <stdio.h> 2 -0.385229


#include <math.h> 3 -0.371614
4 -0.371418
double mutlak(double xk){
if(xk<0) return xk * -1;
else return xk;
}

// fungsi asli
double hasilFungsi(double x){
return exp(x) - 5*x*x;
}

// fungsi turunannya
double 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;
}
Metode JACOBI Hasil Run JACOBI

#include <stdio.h> 1 1.000000 1.000000 1.000000


#include <math.h> 2 1.750000 3.250000 3.200000
3 1.762500 3.900000 3.050000
void jacobi(double x1,doub le x2,double x3){ 4 1.962500 3.887500 2.925000
int a[3][3] = {{4,-1,1},{4,-8,1},{-2,1,5}}; 5 1.990625 3.971875 3.007500
int b[3] = {7,-21,15}; 6 1.991094 3.996250 3.001875
int k = 1; 7 1.998594 3.995781 2.997187
double x,y,z; 8 1.999648 3.998945 3.000281
printf("Metode JACOBI\n\n"); 9 1.999666 3.999859 3.000070
while(k<=50){ 10 1.999947 3.999842 2.999895
x = x1;y = x2;z = x3; 11 1.999987 3.999960 3.000011
printf("%d\t%lf\t%lf\t%lf\n", k,x,y,z); 12 1.999987 3.999995 3.000003
x1 = (b[0] - a[0][1]*y - a[0][2]*z) / a[0][0]; 13 1.999998 3.999994 2.999996
x2 = (b[1] - a[1][0]*x - a[1][2]*z) / a[1][1]; 14 2.000000 3.999999 3.000000
x3 = (b[2] - a[2][0]*x - a[2][1]*y) / a[2][2]; 15 2.000000 4.000000 3.000000
k++;
}
}

main(){
jacobi(1.0,1.0,1.0);

getch();
return 0;
}

TRAPEZOID RULES Hasil Run


#include <iostream.h> Integralnya adalah 0.693771
#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();
}

EULER n xn yn err
#include <iostream.h> 0 0.00000000 1.00000000 0.00000000
#include <iomanip.h> 1 0.20000000 1.24000000 -0.00280552
#include <math.h> 2 0.40000000 1.56800000 -0.01564940
#include <stdio.h> 3 0.60000000 2.00160000 -0.04263760
#include <conio.h> 4 0.80000000 2.56192000 -0.08916186
double f(double x, double y) 5 1.00000000 3.27430400 -0.16225966
{
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();
}

SIMPSON Hasil Run


#include <iostream.h> integralnya adalah 0.69315
#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();
}

Polinom Newton Masukan banyaknya data yang akan di input : 4


#include<stdio.h> x f(x)
8 2.079442
double polinom(double xx,double x[100],int n){ 9 2.197225
if(n){ 9.5 2.251292
return (xx-x[n-1])*polinom(xx,x,n-1); 11 2.397895
}
else return 1; Masukan data yang ingin dicari : 9.2
}
f(9.200000) = 2.219208
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;
}