Python Forum
Coding a summation formula
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding a summation formula
#1
I need to make a summation formula currently under the name "num_approx"
I keep getting some errors I cant pinpoint how to fix.
This is my code:

 

import numpy as np
import math

T = 5777
h = 6.626e-34 #J/s
c = 2.998e17 #nm/s
k = 1.384e-23 #J/K
lambda_nm = 2500 #nm
C2nm= h*c/k
n = np.arange(1.0,11.0,1.0)
x = C2nm/(lambda_nm*T)

num_approx= 15/((math.pi)**4) * (exp(- n * x)/n)(x**3 + (3* x**2)/n + (6 * x^2)/(n**2) + (6)/(n**3))      


print("C2nm =", C2nm)
print("n =", n)
print("Numerical approximation is =", num_approx)
###########################

I get the following errors I cannot resolve. Please help.

Error:
TypeError Traceback (most recent call last) <ipython-input-13-0fa7a7e31d3e> in <module> 10 n = np.arange(1.0,11.0,1.0) 11 x = C2nm/(lambda_nm*T) ---> 12 num_approx= 15/((math.pi)**4) * (exp(- n * x)/n)(x**3 + (3* x**2)/n + (6 * x^2)/(n**2) + (6)/(n**3)) 13 14 ~\Anaconda3\lib\site-packages\mpmath\ctx_mp_python.py in f(x, **kwargs) 1001 def f(x, **kwargs): 1002 if type(x) not in ctx.types: -> 1003 x = ctx.convert(x) 1004 prec, rounding = ctx._prec_rounding 1005 if kwargs: ~\Anaconda3\lib\site-packages\mpmath\ctx_mp_python.py in convert(ctx, x, strings) 646 if isinstance(x, complex): 647 return ctx.make_mpc((from_float(x.real), from_float(x.imag))) --> 648 if type(x).__module__ == 'numpy': return ctx.npconvert(x) 649 if isinstance(x, numbers.Rational): # e.g. Fraction 650 try: x = rational.mpq(int(x.numerator), int(x.denominator)) ~\Anaconda3\lib\site-packages\mpmath\ctx_mp_python.py in npconvert(ctx, x) 679 if isinstance(x, np.complexfloating): 680 return ctx.make_mpc((from_npfloat(x.real), from_npfloat(x.imag))) --> 681 raise TypeError("cannot create mpf from " + repr(x)) 682 683 def isnan(ctx, x): TypeError: cannot create mpf from array([-0.99381282, -1.98762564, -2.98143845, -3.97525127, -4.96906409, -5.96287691, -6.95668972, -7.95050254, -8.94431536, -9.93812818])
Reply
#2
something is missing between (exp(- n * x)/n) and (x**3 + (3* x**2)/n + (6 * x^2)/(n**2) + (6)/(n**3))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks for the catch, but I still get the same error.


import numpy as np
import math

T = 5777
h = 6.626e-34 #J/s
c = 2.998e17 #nm/s
k = 1.384e-23 #J/K
lambda_nm = 2500 #nm
C2nm= h*c/k
n = np.arange(1.0,11.0,1.0)
x = C2nm/(lambda_nm*T)
num_approx= 15/((math.pi)**4) * (exp(- n * x)/n) * (x**3 + (3* x**2)/n + (6 * x^2)/(n**2) + (6)/(n**3))      


print("C2nm =", C2nm)
print("n =", n)
print("Numerical approximation is =", num_approx)
---------------------------------------------------------------------------
Error:
TypeError Traceback (most recent call last) <ipython-input-15-4fc67e105602> in <module> 10 n = np.arange(1.0,11.0,1.0) 11 x = C2nm/(lambda_nm*T) ---> 12 num_approx= 15/((math.pi)**4) * (exp(- n * x)/n) * (x**3 + (3* x**2)/n + (6 * x^2)/(n**2) + (6)/(n**3)) 13 14 ~\Anaconda3\lib\site-packages\mpmath\ctx_mp_python.py in f(x, **kwargs) 1001 def f(x, **kwargs): 1002 if type(x) not in ctx.types: -> 1003 x = ctx.convert(x) 1004 prec, rounding = ctx._prec_rounding 1005 if kwargs: ~\Anaconda3\lib\site-packages\mpmath\ctx_mp_python.py in convert(ctx, x, strings) 646 if isinstance(x, complex): 647 return ctx.make_mpc((from_float(x.real), from_float(x.imag))) --> 648 if type(x).__module__ == 'numpy': return ctx.npconvert(x) 649 if isinstance(x, numbers.Rational): # e.g. Fraction 650 try: x = rational.mpq(int(x.numerator), int(x.denominator)) ~\Anaconda3\lib\site-packages\mpmath\ctx_mp_python.py in npconvert(ctx, x) 679 if isinstance(x, np.complexfloating): 680 return ctx.make_mpc((from_npfloat(x.real), from_npfloat(x.imag))) --> 681 raise TypeError("cannot create mpf from " + repr(x)) 682 683 def isnan(ctx, x): TypeError: cannot create mpf from array([-0.99381282, -1.98762564, -2.98143845, -3.97525127, -4.96906409, -5.96287691, -6.95668972, -7.95050254, -8.94431536, -9.93812818])
Reply
#4
Did you really mean to write x^2 on line 12? Also, don't you need to qualify exp by its module name?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  summation of series student8 6 5,336 Oct-15-2017, 06:13 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020