Say that I want to solve a parametric constrained optimization, method. Is there any method I can use to avoid looping over the parameters?
I would use a code (adapted to my problem) such as (credit):
import numpy as np
from scipy.optimize import minimize
import random
import pandas as pd
pd.options.display.float_format = '{:.5f}'.format
def uncovered(x):
## note x[0]--> x ; x[1]--> y
first_prod = x[0]**2-2*x[0]-4*x[1]+1
second_prod_first = x[0]**3*(8*a**2*(b-1)*b-4*a*(b-2)-3)
second_prod_second = x[0]**2*(-8*a**2*(b-1)*b-8*a+5)
second_prod_third = x[0]*(4*x[1]*(4*a*(b-1)+1)+4*a*b - 1) -4*x[1] - 1
denom = 8*(x[0]-1)*(x[0]*(2*a*(b-1)+1)-1)**2
if (x[0]-1==0):
final_func=0
else:
final_func = first_prod*(second_prod_first+second_prod_second+second_prod_third)/denom
return -final_func
def constraint_strict(x):
return x[1]-0.5*a*x[0]*(x[0]-1)*(1-b)-0.0001
def constraint_nonstrict(x):
return 0.25*(1-2*x[0]+x[0]**2) - x[1]
def constraint_1(x):
return 1-x[0]
def constraint_2(x):
return x[0]
def run_uncovered_market_maximization(alpha,xi):
a=alpha
b=xi
# initial guesses
n = 2
x0 = np.zeros(n)
x0[0] = 1
x0[1] = 1.0
success = False
# show initial objective
print("We are maximizing uncovered welfare:")
print('Initial Objective: ' + str(uncovered(x0)))
while success == False:
# optimize
bnds = ((0,1),(-np.inf,np.inf))
con1 = {'type': 'ineq', 'fun': constraint_strict}
con2 = {'type': 'ineq', 'fun': constraint_nonstrict}
cons = ([con1,con2])
solution = minimize(uncovered,x0,method='SLSQP', bounds=bnds,constraints=cons,options={'disp': False})
x = solution.x
success = solution.success
if success == False:
x0[0] = x0[0]-random.uniform(0, 1)
x0[1] = 5.0-random.uniform(0, 1)
indcons=(x[1]+a*(1-x[0])*x[0]*(1-b)/2)/((((1-x[0])**2)/4)+a*(1-x[0])*(1-b)*x[0]/2)
# show final objective
print("Success:"+str(solution.success))
print('Final Objective: ' + str(uncovered(x)))
print('First Constraint: ' + str(constraint_strict(x)))
print('Second Constraint: ' + str(constraint_nonstrict(x)))
# print solution
print('Solution')
print('d = ' + str(x[0]))
print('p = ' + str(x[1]))
print('market demand:'+ str(1-indcons))
return (solution.success, str(x[0]),str(x[1]),str(uncovered(x)),indcons)
I am unsure on how to handle a and b given that the maximization should not be over these and they only represent parameters. I want to have a pair (x,y) for any acceptable value of (a,b). How to set the boundaries? and how to insert a and b if not by looping over them?
For now I do:
optlist =[]
for b in np.linspace(start=0, stop=1, num=2000):
for a in np.linspace(start=0, stop=1, num=2000):
print('-------------Optimization for a:{} and b:{}-------------------'.format(a,b))
print('-------------Uncovered market')
(usuccess, du, pu,ufinal,uindcons) = run_uncovered_market_maximization(a,b)
Related
How do I solve this error?
TypeError: NumPy boolean subtract, the `-` operator, is not supported, use the bitwise_xor, the `^` operator, or the logical_xor function instead.
I have programmed an optimizing program that must minimize the cost of a wall design. The wall is based on 3 parameters, x, k and m. There are constraints to the sizes of x, k and m as shown. Another constraint is that z (or deflection) must be kept under 100mm. The equation for deflection changes based on a certain t (or time) at which the blast wall is experiencing the blast. If t is below a certain time value which is calculated dependent on, x, k and m the equation is as shown. If t is above the same certain time value, the equation for z changes.
Here is the programming... Please help many thanks :)
import numpy as np
from numpy import linspace
from math import cos
from math import sin
from scipy.optimize import minimize
#Function for minimising
def calcCost(c):
k = c[0]
m = c[1]
x = c[2]
Cost = (900 + 825*k**2 - 1725) + (10*m - 200) + ((2400*x**2)/4)
return Cost
#Objective function
def objective(c):
return calcCost(c)
#Defining Variables
def calck(c):
k = c[0]
k=k
k.resize(12,)
return k
def calcm(c):
m = c[1]
m=m
m.resize(12,)
return m
def calcx(c):
x = c[2]
x=x
x.resize(12,)
return x
def calcz(c):
k = c[0]
x = c[1]
m = c[2]
l = linspace(0,140,141)
for t in l:
if t <= ((20 - 0.12*x**2 + 4.2*x)/1000):
deflection = ((((1000+9*x**2-183*x)*1000)/k)*(1-cos(t*((k/m)**0.5))) + (((1000+9*x**2-183*x)*1000)/k*((20 - 0.12*x**2 + 4.2*x)/1000))*((sin(t*((k/m)**0.5))/((k/m)**0.5))-t))*1000
else:
deflection = ((((1000+9*x**2-183*x)*1000)/(k*((k/m)**0.5)*((20 - 0.12*x**2 + 4.2*x)/1000)))*(sin(((k/m)**0.5)*t))-(sin(((k/m)**0.5)*(t-((20 - 0.12*x**2 + 4.2*x)/1000))))-(((1000+9*x**2-183*x)*1000)/k)*cos(((k/m)**0.5)*t))*1000
deflection.resize(12,)
return deflection
#Constraint functions
def kconstraint1(c):
k = c[0]
return k-(1*10**6) >= 0
def kconstraint2(c):
k = c[0]
return k-(7*10**6) <= 0
def mconstraint1(c):
m = c[0]
return m-200 >= 0
def mconstraint2(c):
m = c[0]
return m-1200 <= 0
def xconstraint1(c):
x = c[0]
return x >= 0
def xconstraint2(c):
x = c[0]
return x <= 10
def zconstraint1(c):
k = c[0]
x = c[1]
m = c[2]
l = linspace(0,140,141)
for t in l:
if t <= ((20 - 0.12*x**2 + 4.2*x)/1000):
deflection = ((((1000+9*x**2-183*x)*1000)/k)*(1-cos(t*((k/m)**0.5))) + (((1000+9*x**2-183*x)*1000)/k*((20 - 0.12*x**2 + 4.2*x)/1000))*((sin(t*((k/m)**0.5))/((k/m)**0.5))-t))*1000
else:
deflection = ((((1000+9*x**2-183*x)*1000)/(k*((k/m)**0.5)*((20 - 0.12*x**2 + 4.2*x)/1000)))*(sin(((k/m)**0.5)*t))-(sin(((k/m)**0.5)*(t-((20 - 0.12*x**2 + 4.2*x)/1000))))-(((1000+9*x**2-183*x)*1000)/k)*cos(((k/m)**0.5)*t))*1000
return deflection <= 99.99999999
b = (0.5,1)
be = (0.5,10)
bb = (0.1,2.0)
bnds = (b,be,bb,bb)
con1 = ({'type':'ineq','fun':kconstraint1})
con2 = ({'type':'ineq','fun':kconstraint2})
con3 = ({'type':'ineq','fun':mconstraint1})
con4 = ({'type':'ineq','fun':mconstraint2})
con5 = ({'type':'ineq','fun':xconstraint1})
con6 = ({'type':'ineq','fun':xconstraint2})
con7 = ({'type':'ineq','fun':zconstraint1})
cons = [con1,con2,con3,con4,con5,con6,con7]
xGUESS = 5
kGUESS = 3*10**6
mGUESS = 700
zGUESS = 90
x0 = np.array([xGUESS,kGUESS,mGUESS,zGUESS])
sol = minimize(objective,x0,method='SLSQP',bounds=bnds,constraints=cons,options={'disp':True})
xOpt = sol.x
CostOPT = sol.fun
kOPT = calck(xOpt)
xOPT = calcx(xOpt)
mOPT = calcm(xOpt)
zOPT = calcz(xOpt)
print(str(CostOPT))
print(str(calcx))
print(str(calcm))
print(str(calck))
print(str(calcz))
I need help with getting the temp using Antoine Eq by making use of classes.
My root action is failing and I don't know why.
My code:
from __future__ import division, print_function
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import root
class Chemical(object):
def __init__(self, name_1, balance_1, name_2, balance_2):
self.name = name_1 + ' ' + '+' + ' ' + name_2
self.data_1 = []
self.data_2 = []
self.data_1 = balance_1
self.data_2 = balance_2
def __str__(self):
if (self.name):
return "Mixture: %s" % (self.name)
else:
return None
def bubble_solve(self,a,P = 1.01,T = 300):
A1,B1,C1 = self.data_1
A2,B2,C2 = self.data_2
PA = 10**(A1 - B1/(C1+T))
PB = 10**(A2 - B2/(C2+T))
sol = root(lambda T: P - (a*PA + (1-a)*PB),T)
return sol.x[0]
def dew_solve(self, b, P = 1.01, T = 300):
A1,B1,C1 = self.data_1
A2,B2,C2 = self.data_2
PA = 10**(A1 - B1/(C1+T))
PB = 10**(A2 - B2/(C2+T))
sol = root(lambda T: 1 - (b*P/PA + (1-b)*P/PB), T)
return sol.x[0]
mixture = Chemical('benzene', [ 3.98523 , 1184.24 , -55.578], 'toulene',
[4.05043 , 1327.62 , -55.528])
print(mixture)
print()
print(mixture.bubble_solve(0.5)) #at a = 0.5
print(mixture.bubble_solve(0.5,2)) #at a = 0.5 and P = 2
print(mixture.dew_solve(0.5)) #at b = 0.5
print(mixture.dew_solve(0.5,2)) #at b = 0.5 and P = 2
This is what my code is printing:
Mixture: benzene + toulene
300.0
300.0
300.0
300.0
However, the answers need to be:
365.087, 390.14188, 371.7743, 396.688.
Why does the root action fail?
Welcome to StackOverflow, #Nathaniel!
The issue is in your PA, PB, and sol lines in bubble_solve and dew_solve. You seem to be mixing constants with independent variables! For instance, with your bubble solve, you have the default value of 300 set for T. Thus in the PA line, the value 300 is being used for T! One way to fix this would be the following:
...
def bubble_solve(self,a,P = 1.01,T = 300):
A1,B1,C1 = self.data_1
A2,B2,C2 = self.data_2
PA = lambda x: 10**(A1 - B1/(C1+x))
PB = lambda x: 10**(A2 - B2/(C2+x))
sol = root(lambda x: P - (a*PA(x) + (1-a)*PB(x)),T)
return sol.x[0]
def dew_solve(self, b, P = 1.01, T = 300):
A1,B1,C1 = self.data_1
A2,B2,C2 = self.data_2
PA = lambda x: 10**(A1 - B1/(C1+x))
PB = lambda x: 10**(A2 - B2/(C2+x))
sol = root(lambda x: 1 - (b*P/PA(x) + (1-b)*P/PB(x)), T)
return sol.x[0]
...
This will return the values you have expected. Note that I use x as the independent variable for PA and PB, but that it's arbitrary. However, it's probably best to not reuse T for your anonymous functions if you are passing values to it.
I am trying to run the minimization process found in this publication. The equation is seen on page 6 of the document 16 of the pdf.
I have a dataframe that looks like the below
df = pd.DataFrame({'h_t':[7.06398,6.29948,5.04570,6.20774,4.80106],
'p_atm':[101057.772801,101324.416001,101857.702401,101724.380801,101991.024001],
'q_p':[5.768132,3.825600,2.772215,5.830429,2.619304],
'q_s':[2.684433,3.403679,2.384275,1.008078,2.387106],
'tdg_f':[117.678100,110.131579,108.376963,103.669725,113.594771],
'tdg_tw':[121.052635,119.710907,114.921463,112.156868,115.444900],
'temp_water':[11.92,19.43,16.87,7.45,11.83]})
I have a constraint that says the below function must be positive where b1 and b3 are the coefficients I am optimizing.
def q_ge(q_p,q_s,b1,b3):
return min(q_p,(b1*q_s+b3))
I wrote my constraint below, but I am not sure if it is right.
def constraint_q_ge(x):
b1,b2,b3=x
power_flow = df.apply(lambda x:q_ge(x['q_p'],x['q_s'],b1,b3), axis = 1)
const = power_flow<0
return -const.sum()
Is this correct? I run the function on all rows and check if any are less than 0 and sum this. The negative of that sum should be greater than or equal to 0. If there is even a single value less than 0 this constraint is not met.
EDIT:
Below is the full problem.
from scipy.constants import g as gravity
from sklearn.metrics import mean_squared_error
from math import sqrt
from scipy.optimize import minimize
import warnings
try:
from numpy import any as _any
except ImportError:
def _any(arg):
if arg is True:
return True
if arg is False:
return False
return any(arg)
def water_density(T=None, T0=None, units=None, a=None,
just_return_a=False, warn=True):
if units is None:
K = 1
m = 1
kg = 1
else:
K = units.Kelvin
m = units.meter
kg = units.kilogram
if T is None:
T = 298.15*K
m3 = m**3
if a is None:
a = (-3.983035*K, # C
301.797*K, # C
522528.9*K*K, # C**2
69.34881*K, # C
999.974950*kg/m3)
if just_return_a:
return a
if T0 is None:
T0 = 273.15*K
t = T - T0
if warn and (_any(t < 0*K) or _any(t > 40*K)):
warnings.warn("Temperature is outside range (0-40 degC)")
return a[4]*(1-((t + a[0])**2*(t + a[1]))/(a[2]*(t + a[3])))
def celsius_to_kelvin(t_celsius):
return t_celsius+273.15
def tailwater(h_t, temp_water, p_atm):
t_water_kelvin = celsius_to_kelvin(temp_water)
rho = water_density(t_water_kelvin)
g = gravity
return (1+(rho*g*h_t)/(2*p_atm))
def tailwater_tdg(q_s,q_p,x, h_t,temp_water,p_atm,tdg_f):
b1,b2,b3=x
A = ((q_s+b1*q_s+b3)/(q_s+q_p))
B = tailwater(h_t, temp_water, p_atm)
C = ((q_p-b1*q_s-b3)/(q_s+q_p))
return 100*A*B*b2+tdg_f*C
def q_ge(q_p,q_s,b1,b3):
return min(q_p,(b1*q_s+b3))
def rmse(y, y_hat):
return sqrt(mean_squared_error(y,y_hat))
def objective(x):
y_hat = df.apply(lambda r:tailwater_tdg(q_s=r['q_s'],q_p=r['q_p'],x=x, h_t=r['h_t'],temp_water=r['temp_water'],p_atm=r['p_atm'],tdg_f=r['tdg_f']), axis = 1)
y = df['tdg_tw']
return rmse(y, y_hat)
#constraints and bounds for optimization model. See reference for more information
def constraint_q_ge(x):
b1,b2,b3=x
power_flow = df.apply(lambda x:q_ge(x['q_p'],x['q_s'],b1,b3), axis = 1)
const = power_flow<0
return -const.sum()
constraints = [{'type':'ineq', 'fun':constraint_q_ge}]
bounds = [(-500,10000),(.00001,10000),(-500,10000)]
x0=[1,1,1]
sol = minimize(objective, x0, method = 'SLSQP',constraints = constraints, bounds = bounds,options={'disp':True, 'maxiter':100})
I'm attempting to minimize the function f(x) = x[0] * x[1] over a system of inequality constraints using scipy.minimize and the solver is returning values which do not respect all of the constraints. For example:
import numpy as np
from scipy.optimize import minimize
from scipy.optimize import fmin_slsqp
# small constant for enforcing strict inequalities
c = 0.000001
# Objective Function to Optimize
def objective(x):
return x[0] * x[1]
# Constraints
def con1(x):
return (x[0]*(1/(7**x[2]))) + 6*x[1] - c
def con2(x):
return (x[0]*(1/(2**x[2]))) + x[1] - c
def con3(x):
return (x[0]*(1/(3**x[2]))) + 2*x[1] - c
def con4(x):
return (x[0]*(1/(4**x[2]))) + 3*x[1] - c
def con5(x):
return (x[0]*(1/(7**x[2]))) - (x[0]*(1/(1**x[2]))) + 7*x[1] - c
def con6(x):
return (x[0]*(1/(2**x[2]))) - (x[0]*(1/(1**x[2]))) + 2*x[1] - c
def con7(x):
return (x[0]*(1/(3**x[2]))) - (x[0]*(1/(1**x[2]))) + 3*x[1] - c
def con8(x):
return -(x[0]*(1/(1**x[2]))) + 4*x[1] - c
def con9(x):
return (x[0]*(1/(1**x[2]))) + 2*x[1] - c
def con10(x):
return (x[0]*(1/(7**x[2]))) + 4*x[1] - c
def con11(x):
return (x[0]*(1/(2**x[2]))) - x[1] - c
def con12(x):
return (x[0]*(1/(4**x[2]))) + x[1] - c
def con13(x):
return x[0] - 1
def con14(x):
return x[1] - 1
def con15(x):
return x[2] - c
# Initial Guesses
x0 = [1,1,1]
# Constraint Objects
constr1 = {'type':'ineq', 'fun':con1}
constr2 = {'type':'ineq', 'fun':con2}
constr3 = {'type':'ineq', 'fun':con3}
constr4 = {'type':'ineq', 'fun':con4}
constr5 = {'type':'ineq', 'fun':con5}
constr6 = {'type':'ineq', 'fun':con6}
constr7 = {'type':'ineq', 'fun':con7}
constr8 = {'type':'ineq', 'fun':con8}
constr9 = {'type':'ineq', 'fun':con9}
constr10 = {'type':'ineq', 'fun':con10}
constr11 = {'type':'ineq', 'fun':con11}
constr12 = {'type':'ineq', 'fun':con12}
constr13 = {'type':'ineq', 'fun':con13}
constr14 = {'type':'ineq', 'fun':con14}
constr15 = {'type':'ineq', 'fun':con15}
cons = [constr1,constr2,constr3,constr4,constr5,constr6,constr7,constr8,constr9,constr10,constr11,constr12,constr13,constr14,constr15]
solution = minimize(objective,x0,method='SLSQP',constraints=cons)
for con in cons:
print(str(con) + str(con['fun'](solution.x)))
Looping over the constraints with the solution values shows that some of the constraints are evaluating as negative even though the constraints are of the form >= 0.
Is this due to some error on my part in the specifications? An issue with numerical precision? Or is this an issue with SLSQP? (see, for example: https://github.com/scipy/scipy/issues/7618).
If this isn't an issue with my specifications, I would also accept suggestions for formulations of this constraint solving problem in other frameworks (preferably in Python).
I am currently doing a python exercise for my University studies. I am very stuck at this task:
The taylor polynomial of degree N for the exponential function e^x is given by:
N
p(x) = Sigma x^k/k!
k = 0
Make a program that (i) imports class Polynomial (found under), (ii) reads x and a series of N values from the command line, (iii) creates a Polynomial instance representing the Taylor polynomial, and (iv) prints the values of p(x) for the given N values as well as the exact value e^x. Try the program out with x = 0.5, 3, 10 and N = 2, 5, 10, 15, 25.
Polynomial.py
import numpy
class Polynomial:
def __init__(self, coefficients):
self.coeff = coefficients
def __call__(self, x):
"""Evaluate the polynomial."""
s = 0
for i in range(len(self.coeff)):
s += self.coeff[i]*x**i
return s
def __add__(self, other):
# Start with the longest list and add in the other
if len(self.coeff) > len(other.coeff):
result_coeff = self.coeff[:] # copy!
for i in range(len(other.coeff)):
result_coeff[i] += other.coeff[i]
else:
result_coeff = other.coeff[:] # copy!
for i in range(len(self.coeff)):
result_coeff[i] += self.coeff[i]
return Polynomial(result_coeff)
def __mul__(self, other):
c = self.coeff
d = other.coeff
M = len(c) - 1
N = len(d) - 1
result_coeff = numpy.zeros(M+N+1)
for i in range(0, M+1):
for j in range(0, N+1):
result_coeff[i+j] += c[i]*d[j]
return Polynomial(result_coeff)
def differentiate(self):
"""Differentiate this polynomial in-place."""
for i in range(1, len(self.coeff)):
self.coeff[i-1] = i*self.coeff[i]
del self.coeff[-1]
def derivative(self):
"""Copy this polynomial and return its derivative."""
dpdx = Polynomial(self.coeff[:]) # make a copy
dpdx.differentiate()
return dpdx
def __str__(self):
s = ''
for i in range(0, len(self.coeff)):
if self.coeff[i] != 0:
s += ' + %g*x^%d' % (self.coeff[i], i)
# Fix layout
s = s.replace('+ -', '- ')
s = s.replace('x^0', '1')
s = s.replace(' 1*', ' ')
s = s.replace('x^1 ', 'x ')
#s = s.replace('x^1', 'x') # will replace x^100 by x^00
if s[0:3] == ' + ': # remove initial +
s = s[3:]
if s[0:3] == ' - ': # fix spaces for initial -
s = '-' + s[3:]
return s
def simplestr(self):
s = ''
for i in range(0, len(self.coeff)):
s += ' + %g*x^%d' % (self.coeff[i], i)
return s
def _test():
p1 = Polynomial([1, -1])
p2 = Polynomial([0, 1, 0, 0, -6, -1])
p3 = p1 + p2
print p1, ' + ', p2, ' = ', p3
p4 = p1*p2
print p1, ' * ', p2, ' = ', p4
print 'p2(3) =', p2(3)
p5 = p2.derivative()
print 'd/dx', p2, ' = ', p5
print 'd/dx', p2,
p2.differentiate()
print ' = ', p5
p4 = p2.derivative()
print 'd/dx', p2, ' = ', p4
if __name__ == '__main__':
_test()
Now I'm really stuck at this, and I would love to get an explaination! I am supposed to write my code in a separate file. I'm thinking about making an instance of the Polynomial class, and sending in the list in argv[2:], but that doesn't seem to be working. Do I have to make a def to calculate the taylor polynomial for the different values of N before sending it in to the Polynomial class?
Any help is great, thanks in advance :)
Not quite finished, but this answers your main question I believe. Put class Polynomial in poly.p and import it.
from poly import Polynomial as p
from math import exp,factorial
def get_input(n):
''' get n numbers from stdin '''
entered = list()
for i in range(n):
print 'input number '
entered.append(raw_input())
return entered
def some_input():
return [[2,3,4],[4,3,2]]
get input from cmd line
n = 3
a = get_input(n)
b = get_input(n)
#a,b = some_input()
ap = p(a)
bp = p(b)
print 'entered : ',a,b
c = ap+bp
print 'a + b = ',c
print exp(3)
x = ap
print x
sum = p([0])
for k in range(1,5):
el = x
for j in range(1,k):
el el * x
print 'el: ',el
if el!=None and sum!=None:
sum = sum + el
print 'sum ',sum
output
entered : [2, 3, 4] [4, 3, 2]
a + b = 6*1 + 6*x + 6*x^2
20.0855369232
2*1 + 3*x + 4*x^2
sum 2*1 + 3*x + 4*x^2
el: 4*1 + 12*x + 25*x^2 + 24*x^3 + 16*x^4
sum 6*1 + 15*x + 29*x^2 + 24*x^3 + 16*x^4
el: 4*1 + 12*x + 25*x^2 + 24*x^3 + 16*x^4
el: 8*1 + 36*x + 102*x^2 + 171*x^3 + 204*x^4 + 144*x^5 + 64*x^6
sum 14*1 + 51*x + 131*x^2 + 195*x^3 + 220*x^4 + 144*x^5 + 64*x^6
el: 4*1 + 12*x + 25*x^2 + 24*x^3 + 16*x^4
el: 8*1 + 36*x + 102*x^2 + 171*x^3 + 204*x^4 + 144*x^5 + 64*x^6
el: 16*1 + 96*x + 344*x^2 + 792*x^3 + 1329*x^4 + 1584*x^5 + 1376*x^6 + 768*x^7 + 256*x^8
sum 30*1 + 147*x + 475*x^2 + 987*x^3 + 1549*x^4 + 1728*x^5 + 1440*x^6 + 768*x^7 + 256*x^8
I solved the task in the following way, though im not sure if it answers question (iv).
The output just compares the exact value of e**x to the calculated value from module Polynomial.
from math import factorial, exp
from Polynomial import *
from sys import *
#Reads x and N from the command line on the form [filename.py, x-value, N-value]
x = eval(argv[1])
N = eval(argv[2])
#Creating list of coefficients on the form [1 / i!]
list_coeff = [1./factorial(i) for i in range(N)]
print list_coeff
#Creating an instance of class Polynomial
p1 = Polynomial(list_coeff)
print 'Calculated value of e**%f = %f ' %(x, p1.__call__(x))
print 'Exact value of e**%f = %f'% (x, exp(x))
"""Test Execution
Terminal > python Polynomial_exp.py 0.5 5
[1.0, 1.0, 0.5, 0.16666666666666666, 0.041666666666666664]
Calculated value of e**0.500000 = 1.648438
Exact value of e**0.500000 = 1.648721
"""