forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample8_7.py
35 lines (29 loc) · 1021 Bytes
/
example8_7.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
#!/usr/bin/python
## example8_7
from numpy import zeros,array,arange
from newtonRaphson2 import *
def residual(y): # Residuals of finite diff. Eqs. (8.11)
r = zeros(m + 1)
r[0] = y[0]
r[m] = y[m] - 1.0
for i in range(1,m):
r[i] = y[i-1] - 2.0*y[i] + y[i+1] \
- h*h*F(x[i],y[i],(y[i+1] - y[i-1])/(2.0*h))
return r
def F(x,y,yPrime): # Differential eqn. y" = F(x,y,y')
F = -3.0*y*yPrime
return F
def startSoln(x): # Starting solution y(x)
y = zeros(m + 1)
for i in range(m + 1): y[i] = 0.5*x[i]
return y
xStart = 0.0 # x at left end
xStop = 2.0 # x at right end
m = 10 # Number of mesh intevals
h = (xStop - xStart)/m
x = arange(xStart,xStop + h,h)
y = newtonRaphson2(residual,startSoln(x),1.0e-5)
print "\n x y"
for i in range(m + 1):
print "%14.5e %14.5e" %(x[i],y[i])
raw_input("\nPress return to exit")