Skip to content

Instantly share code, notes, and snippets.

@thider
Created May 4, 2011 03:56
Show Gist options
  • Save thider/954729 to your computer and use it in GitHub Desktop.
Save thider/954729 to your computer and use it in GitHub Desktop.
Starting Crank Nicholson
from numpy import *
from matplotlib.pyplot import *
from time import sleep
import numpy as np
import networkx as nx
dim = [2,2]
dt = .2 #time step
G = nx.generators.classic.grid_graph(dim, periodic=False, create_using=None)
L = nx.laplacian(G)
#print L.shape[0] #gets the length of the zeroth entry of the laplacian
n = L.shape[0]
t = 100
T = 60*ones( (n,t) ) #setting initial temperature
i=0
#for i in range(t):
for i in range(t-2):
T[:,0] = 60
T[:,i] = 60
T[:,i+2] = dot(linalg.inv( ones( (n,n) ) - dt/2*L )*(ones( (n,n) ) + dt/2*L),T[:,i+1]) #note dot(a,v) treats v as a column
#vector while dot(v,a) treats v like
#print T # a row vector
print T
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment