#! /usr/bin/env python import numpy as np import matplotlib.pyplot as plt import time, sys from IPython.core.display import clear_output nx = 141 # num dx = 2./(nx -1) # grid spacing nt = 25 # number of time steps dt = 0.005 # time step c = 1.0 # wave speed # Set up the inital conditions u = np.ones(nx) u[.5/dx:1/dx+1]=2 print u plt.plot(np.linspace(0,2,nx),u) un = np.ones(nx) for n in range(nt): un[:] = u[:] for i in range(1,nx): u[i] = un[i]-un[i]*dt/dx*(un[i]-un[i-1]) plt.plot(np.linspace(0,2,nx),u) plt.show()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question