1

I'm taking my first course in programming, the course is meant for physics applications and the like. We have an exam coming up, my professor published a practice exam with the following question.

The Maxwell distribution in speed v for an ideal gas consisting of particles of mass m at Kelvin temperature T is given by:

Stackoverflow doesn't use MathJax for formula's, and I can't quite figure out how to write a formula on this site. So, here is a link to WolframAlpha:

where k is Boltzmann's constant, k = 1.3806503 x 10-23 J/K.

Write a Python script called maxwell.py which prints v and f(v) to standard output in two column format. For the particle mass, choose the mass of a proton, m = 1.67262158 10-27 kg. For the gas temperature, choose the temperature at the surface of the sun, T = 5778 K.

Your output should consist of 300 data points, ranging from v = 100 m/s to v = 30,000 m/s in steps of size dv = 100 m/s.

So, here is my attempt at the code.

import math as m
import sys

def f(v):
    n = 1.67262158e-27 #kg
    k = 1.3806503e-23 #J/K 
    T = 5778 #Kelvin
    return (4*m.pi)*((n/(2*m.pi*k*T))**(3/2))*(v**2)*m.exp((-n*v**2)/(2*k*T))

v = 100 #m/s
for i in range(300):
   a = float(f(v))
   print (v, a)
   v = v + 100

But, my professors solution is:

import numpy as np
def f(v):     
    m = 1.67262158e-27  # kg     
    T = 5778.           # K     
    k = 1.3806503e-23   # J/K    
    return 4.*np.pi * (m/(2.*np.pi*k*T))**1.5 * v**2 * np.exp(-m*v**2/(2.*k*T))

v = np.linspace(100.,30000.,300) 
fv = f(v) 
vfv = zip(v,fv) 
for x in vfv:     
    print "%5.0f %.3e"%x
    # print np.sum(fv*100.)

So, these are very different codes. From what I can tell, they produce the same result. I guess my question is, simply, why is my code incorrect?

Thank you!


EDIT:

So, I asked my professor about it and this was his response.

I think your code is fine. It would run much faster using numpy, but the problem didn't specify that you needed numpy. I might have docked one point for not looping through a list of v's (your variable i doesn't do anything). Also, you should have used v += 100. Almost, these two things together would have been one point out of 10.

1st: Is there any better syntax for doing the range in my code, since my variable i doesn't do anything?

2nd: What is the purpose of v += 100?

10
  • 5
    You should ask your professor to explain. Commented Oct 14, 2016 at 20:32
  • what makes you think your code is incorrect if you have the same results? Did you test over multiple inputs? Are they all the same ? Commented Oct 14, 2016 at 20:35
  • @MooingRawr, I guess I should ask if one is more efficient than the other. Commented Oct 14, 2016 at 20:37
  • If your code works as intended maybe you will get more help to optimize it here: codereview.stackexchange.com Commented Oct 14, 2016 at 20:38
  • They're pretty much the same. There's not likely to be much difference between numpy.pi and math.pi, or mumpy.exp and math.exp. Commented Oct 14, 2016 at 21:15

1 Answer 1

1

Things to be careful about when dealing with numbers is implicit type conversion from floats to ints. One instance I could figure in your code is that you use (3/2) which evaluates to 1, while the other code uses 1.5 directly.

Sign up to request clarification or add additional context in comments.

3 Comments

He must be using Python 3, where / converts to float, and // performs integer division.
@Barmar I'm not sure which version it is, I'm working off of my universities computers.
@Kosta python --version will tell you what version. But it must be Python 3 or you'd be getting different results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.