0

I'm having trouble trying to display my results after the program is finished. I'm expecting to see a velocity vs position graph, but for some reason, it's not showing up. Is there something wrong with my code.

import numpy 
from matplotlib import pyplot 
import time,sys


#specifying parameters

nx=41 # number of space steps
nt=25  #number of time steps
dt=0.025 #width between time intervals
dx=2/(nx-1) #width between space intervals 
c=1 # the speed of the initial wave



#boundary conditions
u = numpy.ones(nx) 
u[int(0.5/dx):int(1/(dx+1))] = 2

un = numpy.ones(nx)

#initializing the velocity function
for i in range(nt):
un= u.copy()
for i in range(1,nx):
    u[i]= un[i] -c*(dt/dx)*(u[i]-u[i-1])


pyplot.xlabel('Position')
pyplot.ylabel('Velocity')
pyplot.plot(numpy.linspace(0,2,nx),u)
2
  • 1
    Try adding pyplot.show() at the end Commented Apr 3, 2020 at 1:20
  • Your, un= u.copy isn't properly indented. Commented Apr 3, 2020 at 1:29

2 Answers 2

1

There are a few things going on here

  1. You don't need to write out the full name of the packages you are importing. You can just use aliasing to call those packages and use them with those aliases later on. This, for example.

    import numpy as np
    
  2. Your dx value initalization will give you 0 beause you are dividing 2 by 40 which will give you a zero. You can initialize the value of dx by making one of the values in that expression a float, so something like this.

    dx=float(2)/(nx-1) #width between space intervals 
    

As Meowcolm Law in the comments suggested in the comments, add pyplot.show() to show the graph. This is what the edited version of your code will like

import numpy as np
import matplotlib.pyplot as plt 
import time,sys


#specifying parameters

nx=41 # number of space steps
nt=25  #number of time steps
dt=0.025 #width between time intervals
dx=float(2)/(nx-1) #width between space intervals 
c=1 # the speed of the initial wave



#boundary conditions
u = np.ones(nx) 
u[int(0.5/dx):int(1/(dx+1))] = 2
un = np.ones(nx)

#initializing the velocity function
for i in range(nt):
    un= u.copy()
for i in range(1,nx):
    u[i]= un[i] -c*(dt/dx)*(u[i]-u[i-1])


plt.xlabel('Position')
plt.ylabel('Velocity')
plt.plot(np.linspace(0,2,nx),u)
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

0

You can add

%matplotlib inline

in order to view the plot inside the notebook. I missed this step following the same guide.

Comments

Your Answer

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