# Program to evaluate the integration using Simpson's (1/3) rule for f(x)=1-exp(-x):
f1=open("le.d","w")
from math import*
def f(x):
y=1-exp(-x)
return y
a=0
Y=[]
B=[]
V=[]
h=0.001
for n in range(0,5001,50):
b=a+n*h
v=b+exp(-b)-1
p=0
q=0
for j in range(1,n,2):
p=p+f(a+j*h)
for k in range(2,n-1,2):
q=q+f(a+k*h)
y=h/3*(f(a)+f(b)+4*p+2*q)
print(b,y,file=f1)
Y.append(y)
B.append(b)
V.append(v)
f1.close()
import matplotlib.pyplot as plt
plt.plot(B,Y,'r',B,V,'g')
#plt.show()
f1=open("le.d","w")
from math import*
def f(x):
y=1-exp(-x)
return y
a=0
Y=[]
B=[]
V=[]
h=0.001
for n in range(0,5001,50):
b=a+n*h
v=b+exp(-b)-1
p=0
q=0
for j in range(1,n,2):
p=p+f(a+j*h)
for k in range(2,n-1,2):
q=q+f(a+k*h)
y=h/3*(f(a)+f(b)+4*p+2*q)
print(b,y,file=f1)
Y.append(y)
B.append(b)
V.append(v)
f1.close()
import matplotlib.pyplot as plt
plt.plot(B,Y,'r',B,V,'g')
#plt.show()
Comments
Post a Comment