What is the semantic error in my code

In summary: Also, I am not sure if taking smaller steps would help, as it seems that the curve is not even close to what it should be. In summary, the conversation is about a problem with numerically solving equations of state for the Chandrasekar limit for a white dwarf star using Python. The code works for a simple harmonic oscillator but not for the two coupled ODEs needed for the project. The equations of state are defined in the comments and data points are formed using a runge kutta algorithm. However, the resulting graph is a straight horizontal line instead of the expected curve. Possible solutions suggested include changing the initial value to avoid a singularity, stopping the array from receiving negative values, and checking the equations of state and
  • #1
Bishop556
37
4
Note that the code I am using is Python. Currently, I am attempting to numerically solve the equations of state that give rise to the Chandrasekar limit for a white dwarf star. My code works for a simple harmonic oscillator given the correct equations of motion, but does not compute the correct curve for my two coupled ODEs that I need for my project. My code is below. Note that the equations of state are defined in the comments.
Python:
import numpy as np
import matplotlib.pyplot as plt
from math import sqrt

#Variable Definitions

N = 500 #number of entries
dur = 25000 #radius taken up to in kilometers
dr = dur / float(N-1) #step size

#creating the array

y = np.zeros([N,2])

y[0,0] = 20 #initial central momentum
y[0,1] = 0  #initial mass

#runge kutta algorithm

def rk4(y, r, dr, deriv):
    k1 = dr * deriv(y,r)
    k2 = dr * deriv(y + 0.5*k1,r + 0.5*dr)
    k3 = dr * deriv(y + 0.5*k2, r + 0.5*dr)
    k4 = dr * deriv(y + k3, r + dr)
    y_next = y + (k1 + 2*(k2+k3)+k4)/6
    return y_next#Equations of state (Note that x[0] is my fermi momentum x and x[1] is my mass. r is my radius

def Eqns(x,r):
    y0 = (-5*x[1]*sqrt(1+x[0]**2))/(3*x[0]*r**2)  #dx/dr = -5/3(M/r^2)*sqrt(1+x^2)/x
    y1 = 3*(r**2)*(x[0]**3)                       #dM/dr = 3*r^2*x^3
    return np.array([y0,y1])

#forming data points

for i in range(N-1):
    y[i+1] = rk4(y[i],10**(-23), dr, Eqns) #started at some arbitrary smalll value to avoid
                                           #singularity
radius = np.linspace(10**(-23),25000, N)

#plotting
plt.plot(y[:,1],radius)

plt.show()

For some reason when I run this all I receive is a straight linear line which is nothing like the curve I expected that is attached to this post. Also, the graph indicates my mass is negative which is ridiculous. I have a feeling the error is a semantic error somewhere in my code, but would I not expect a curve instead of a straight line for ODEs that are obviously not linear. Thanks again.

EDIT: I forgot to mention that what the curve should resemble is the curve in the relativistic fermi gas line.
 

Attachments

  • 800px-WhiteDwarf_mass-radius_en.png
    800px-WhiteDwarf_mass-radius_en.png
    10 KB · Views: 536
  • graph from code.png
    graph from code.png
    2 KB · Views: 483
Last edited by a moderator:
Technology news on Phys.org
  • #2
Your bug is here:
Code:
y[i+1]= rk4(y[i],10**(-23), dr, Eqns)
You have r frozen at 10-23. Change that 10**(-23) to i*r if i else 1e-23

Alternatively, change your loop to
Code:
r = 1e-23 # To avoid singularity at r=0
for i in range(N-1):
    y[i+1]= rk4(y[i],r, dr, Eqns)
    r = r+dr
 
Last edited:
  • #3
After applying this change, I am receiving a straight horizontal line which is odd.
 
  • #4
Bishop556 said:
After applying this change, I am receiving a straight horizontal line which is odd.

What's the y location of this straight horizontal line?
 
  • #5
Code:
import numpy as np
import matplotlib.pyplot as plt
from math import sqrt

#Variable Definitions

N = 500
dur = 2.0
 dr = dur/float(N-1)

#creating the array

y = np.zeros([N,2])

y[0,0] = 15
y[0,1] = 0#runge kutta algorithm

def rk4(y, r, dr, deriv):
    k1 = dr * deriv(y,r)
    k2 = dr * deriv(y + 0.5*k1,r + 0.5*dr)
    k3 = dr * deriv(y + 0.5*k2, r + 0.5*dr)
    k4 = dr * deriv(y + k3, r + dr)
    y_next = y + (k1 + 2*(k2+k3)+k4)/6
    return y_next#Equations of state

def Eqns(x,r):
    y0 = (-5*x[1]*sqrt(1+x[0]**2))/(3*x[0]*r**2)  #dx/dr = -5/3(M/r^2)*sqrt(1+x^2)/x
    y1 = 3*(r**2)*(x[0]**3)                       #dM/dr = 3*r^2*x^3
    return np.array([y0,y1])

#forming data pointsr = 1e-23 #starting value to avoid singularity
for i in range(N-1):
    if y[i,0]>0:
        y[i+1] = rk4(y[i],r,dr,Eqns)
        r = r + dr
      
    else:
        break #meant to stop negative values of x to be in the array

  
  
  
  #started at some arbitrary smalll value to avoid
                                           #singularity
radius = np.linspace(1e-23,r,N)#plotting
plt.xlim([0,2])
plt.ylim([0,.535])
plt.plot(radius, y[:,1])

plt.show()
 
  • #6
My new code is listed above. I added a portion that would stop the array from receiving an negative values of x. The rest of the array will be zeros due to this effect which is expected. When computing the arrays, the data points seem to be somewhat correct, but the graph is wonky. My current abomination is linked here.
 

Attachments

  • graph from code.png
    graph from code.png
    923 bytes · Views: 478
  • #7
SteamKing said:
What's the y location of this straight horizontal line?

I am plotting radius in the x-axis and Mass in y axis.
 
  • #8
Do you have the right equations of state? Do you need to take smaller steps?
 
  • #9
D H said:
Do you have the right equations of state? Do you need to take smaller steps?

I believe I do. The equations of state are mentioned as comments in my code. Do you notice anything I am doing wrong to code them correctly?
 

1. What is a semantic error in code?

A semantic error in code refers to a mistake that affects the meaning or logic of the code, rather than its syntax. This means that the code may run without any errors, but it does not produce the desired outcome.

2. How can I identify a semantic error in my code?

Identifying a semantic error can be tricky as it does not produce any error messages. However, you can look for unexpected or incorrect outputs, or analyze the code's logic to find any mistakes.

3. What causes semantic errors in code?

Semantic errors can be caused by various factors such as incorrect use of variables, incorrect use of control structures, and attempting to perform operations on incompatible data types.

4. How can I prevent semantic errors in my code?

To prevent semantic errors, it is important to have a clear understanding of the code's logic and to double-check for any mistakes. Writing clear and organized code and using proper debugging techniques can also help prevent semantic errors.

5. Can semantic errors be fixed?

Yes, semantic errors can be fixed by identifying and correcting the mistake in the code. It may require making changes to the code's logic or structure to produce the desired outcome.

Similar threads

  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
Replies
2
Views
790
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
622
Replies
56
Views
693
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
11
Views
3K
  • Programming and Computer Science
Replies
2
Views
918
Back
Top