forked from JanisErdmanis/PythonPhysics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.8.py
53 lines (41 loc) · 1018 Bytes
/
6.8.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 12 21:56:30 2013
@author: akels
"""
from __future__ import division, print_function
from os import sys
sys.path.append('cpresources')
from pylab import *
c = 2
accuracy = 1e-6
x1 = 1.0
error = 1.0
iterations = 0
# Standart method
while error>accuracy:
x1,x2 = 1 - exp(-c*x1), x1
error = abs((x1-x2)/(1-1/(c*exp(-c*x1))))
iterations+=1
print('The result is {} it took {} iterations'.format(x1,iterations))
# Overrelaxation
omega = -1
x1 = 1.0
error = 1.0
iterations = 0
while error>accuracy:
x1,x2 = (1 - exp(-c*x1))*(1 + omega) - omega*x1, x1
error = abs((x1-x2)/(1-1/( (1 + omega)*(c*exp(-c*x1)) - omega )))
iterations+=1
print('The result is {} it took {} iterations'.format(x1,iterations))
# Loop until error is small enough
c_list = arange(0.01,3,0.1)
x_list = []
for c in c_list:
x1 = 1.0
error = 1.0
while error>accuracy:
x1,x2 = 1 - exp(-c*x1), x1
error = abs((x1-x2)/(1-1/(c*exp(-c*x1))))
x_list.append(x1)
plot(x_list,c_list)