-
Notifications
You must be signed in to change notification settings - Fork 25
/
9.2.py
38 lines (34 loc) · 945 Bytes
/
9.2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 19 11:34:51 2013
@author: akels
"""
from __future__ import division, print_function
from pylab import *
# Constants
M = 100 # Grid squares on a side
V = 1.0 # Voltage at top wall
target = 1e-6 # Target accuracy
# Create arrays to hold potential values
phi = zeros([M+1,M+1],float)
phi[0,:] = V
#phiprime = empty([M+1,M+1],float)
# Main loop
delta = 1.0
omega = 1.1
while delta>target:
delta = 0
# Calculate new values of the potential
for i in range(1,M):
for j in range(1,M):
#if i==0 or i==M or j==0 or j==M:
# phiprime[i,j] = phi[i,j]
#else:
difference = (phi[i+1,j] + phi[i-1,j] \
+ phi[i,j+1] + phi[i,j-1])/4 - phi[i,j]
phi[i,j] = phi[i,j] + (1+omega)*difference
if difference>delta: delta = difference
# Make a plot
imshow(phi)
gray()
show()