Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with plotting
#1
Hello, I am making a plot to contrast Planks law with Weins and Rayleigh-Jeans` versions. I am currently having an issue with the Rayleigh-Jeans plot (just showing up as an L), this is an assigned problem to refine coding skills in a physics class, I have provided my code thus far.

import matplotlib.pyplot as ply
import numpy as np
import math

#Defining constants
h = 6.6261 * 10 ** -34
k = 1.3807 * 10 ** -23
c = 2.9979 * 10 ** 8
T = 30000
wavelengths = np.linspace(1, 800, 1600)
RJ = wavelengths.copy()
PL = wavelengths.copy()
WN = wavelengths.copy()
 #Deining the functions of interest
def RJFunc(q):

    for n in range(len(q)):

        q[n] = ((2 * k * c * T) / ((q[n] * 10 ** -9) ** 4))

def PLFunc(b):

    for m in range(len(b)):

         b[m] = ((2 * h * c **2) / (((b[m] * 10 ** -9) ** 5) * (math.exp((h * c) / ((b[m] * 10 ** -9) * k * T)) -1)))
         
def WNFunc(d):

    for l in range(len(d)):

        d[l] = ((2 * h * c **2) / (((d[l] * 10 ** -9) ** 5) * (math.exp((h * c) / ((d[l] * 10 ** -9) * k * T)))))

RJFunc(RJ)
PLFunc(PL)
WNFunc(WN)

#Plotting these functions
ply.plot(wavelengths, PL, wavelengths, WN, wavelengths, RJ)
ply.show()
Thank you!
Reply
#2
Is your problem a Python coding problem or a physics problem? If it's a coding problem, can you elaborate further?
Reply
#3
Are you positive that your parentheses are properly nested? I attempted to find corresponding equations and did not succeed.

I made some changes to clean up your code a bit. Just a few notes:
  1. Global variables are no good. If a function needs data from a variable, add a parameter to hold the data.
  2. While functions can alter a list without returning anything, a function should have a return. It's better to generate a list inside the function and return it. Plus, doing this means you don't need to use range(len(x)) and indexing.
  3. Never use "l" as a variable. It looks too much like "1" or "I" and generates confusion.
  4. Parameter names for functions don't need be unique. func1(x, y) and func2(x, y) can have the same parameter names and there's no issue.
  5. When using a function as part of a larger equation, don't put parentheses around it unless you need to alter the order of precedence. Adding parentheses makes the code more difficult to read.
    1 / (sum(1,2,3)) + 2 # bad
    1 / sum(1,2,3) + 2 # good
    1 / (sum(1,2,3) + 2) # good

import matplotlib.pyplot as ply
import numpy as np
import math

#Deining the functions of interest
def RJFunc(wave_values, c, k, T):
    out = []
    for num in wave_values:
        out.append((2 * k * c * T) / (num * 10 ** -9) ** 4)
    return out

def PLFunc(wave_values, c, h, k, T):
    out = []
    for num in wave_values:
        out.append((2 * h * c **2) / ((num * 10 ** -9) ** 5 * math.exp((h * c) / ((num * 10 ** -9) * k * T)) -1))
    return out

def WNFunc(wave_values, c, h, k, T):
    out = []
    for num in wave_values:
        out.append((2 * h * c **2) / ((num * 10 ** -9) ** 5 * math.exp((h * c) / ((num * 10 ** -9) * k * T))))
    return out

#Defining constants
h = 6.6261 * 10 ** -34
k = 1.3807 * 10 ** -23
c = 2.9979 * 10 ** 8
T = 30000
wavelengths = np.linspace(1, 800, 1600)

#Plotting these functions
ply.plot(
    wavelengths, PLFunc(wavelengths, c, k, T), 
    wavelengths, WNFunc(wavelengths, c, h, k, T), 
    wavelengths, RJFunc(wavelengths, c, h, k, T)
)
ply.show()
Reply
#4
The problem is to do with the coding aspect of the question, the section designated RJ is not producing the correct graph, however, the other two sections are, thank you.

@stullis

Thank you for the comments and improvements, Im relatively new to this so anything to improve my codes readability helps. That being said my graph for the Rayleigh-Jeans function is still providing an L shape as opposed to the expected curve. Ive gone through the equations (Plancks Law, Rayleigh-Jeans version, and Wein`s version) a few times with no improvements, to my knowledge they are but they could not be, thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issue with plotting (basic) jmkook 1 1,618 Sep-11-2020, 11:07 PM
Last Post: scidam

Forum Jump:

User Panel Messages

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