Mathematic, Programming, Python

Implementasi metode simpson menggunakan bahasa pemrograman python

import math
import decimal

a = 0; b = 1; n = 10
h = float (b-a) / n
r = 0
start = 0

print ("  Metode Numerik   ")
print ("Algoritma Simpson  ")
print

def power(x,y):
	return x ** y;

def fx( x ):
	return ( 0.25 * power(x,5)) + (3 * power(x,2));

def real (a,b):
	return ( float(1)/float(24) * power(b,6) + power(b,3) ) - ( float(1)/float(24) * power(a,6) + power(a,3) )

def Simpson( h, r ):
	return  h / 3 * r

for i in range(0,11):
	if i == 0:
		r+=fx(start);
		print "f(",start,")\t: ", fx(start), "\t\tR        : ", r;
	elif i == 10:
		r+=fx(start);
		print "f(",start,")\t: ", fx(start), "\t\tR        : ", r;
	elif i % 2 == 0:
		r+=2*fx(start);
		print "f(",start,")\t: ", 2*fx(start), " \tR        : ", r;
	elif i % 2 == 1:
		r+=4*fx(start);
		print "f(",start,")\t: ", 4*fx(start), " \tR        : ", r;
	start+=h

print

print ("a  : ", a);
print ("b  : ", b);
print ("h  : ", h);
print ("n  : ", n);

print

real = round(real( a,b ), 6)
numeric = Simpson( h,r )

print ("Simpson Real     : ", real)
print ("Simpson Numeric  : ", numeric)
print ("Simpson Error    : ", numeric - real)
print

Contoh kode diatas adalah bentuk penyelesaian metode simpson dalam mencari suatu nilai pendekatan terhadap persamaan f(x) = 1/4 x5 + 3 x2

Hasil perhitungan menggunakan kode diatas adalah seperti berikut :

screen-shot-2016-12-07-at-12-23-56-pm

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s